//汽车父类
public abstract class Car {
// 基本变量
int number; // 汽车序号
String name; // 汽车名称
double cargoCap;// 载货量
int peopleCap; // 载人量
int rent; // 租金
//基本方法
public String getName() {
//获得名称
return name;
}
public int getRent() {
//获得租金
return rent;
}
public double getCargo(){
//获得载货量
return cargoCap;
}
public int getPeople() {
//获得载人量
return peopleCap;
}
public int getNumber() {
//获得车辆序号
return number;
}
public abstract String getType(); //获得车辆类型的抽象方法
public String carInfo(){
// 获得可阅读的车辆信息
String result = this.getNumber() + ". 汽车名称: " + this.getName() + ", 汽车类型: " + this.getType() +
", 租金: " + this.getRent() +"元/天, ";
if (this.getType() == "轿车") {
result += "载人数: " + this.getPeople() + "人";
} else if (this.getType() == "货车") {
result += "载货量: " + this.getCargo() + "顿";
} else {
result += "载人数: " + this.getPeople() + "人, 载货量: " + this.getCargo() + "顿";
}
return result;
}
public String toString() {
//以车辆名称覆盖默认print方式
return this.name;
}
}
//货车子类
public class Truck extends Car {
public Truck(int number, String name, int rent, double cargoCap) {
this.number = number;
this.name = name;
this.rent = rent;
this.cargoCap = cargoCap;
}
@Override
public String getType() {
// TODO Auto-generated method stub
return "货车";
}
//轿车子类
public class Normal extends Car {
public Normal(int number, String name, int rent, int peopleCap) {
this.number = number;
this.name = name;
this.rent = rent;
this.peopleCap = peopleCap;
}
@Override
public String getType() {
// TODO Auto-generated method stub
return "轿车";
}
}
//皮卡子类
public class PickUp extends Car {
public PickUp(int number, String name, int rent, int peopleCap, double cargoCap){
this.number = number;
this.name = name;
this.rent = rent;
this.peopleCap = peopleCap;
this.cargoCap = cargoCap;
}
@Override
public String getType() {
// TODO Auto-generated method stub
return "皮卡";
}
}
import java.util.Scanner;
// main方法
public class init {
public static void main(String[] args) {
// 所有可租车辆
Car[] allRent = {new Normal(1, "奥迪A4", 500, 4),
new Normal(2, "马自达6", 400, 4),
new PickUp(3, "皮卡雪6", 450, 4, 2.0),
new Normal(4, "金龙", 800, 20),
new Truck(5, "松花江", 400, 4.0),
new Truck(6, "依维河", 1000, 20.0)};
System.out.println("欢迎光临didi打车");
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("是否继续? 0:退出, 1:继续");
choice = input.nextInt();
}
while (choice != 0 && choice != 1);
if (choice == 0) {
System.out.println("很遗憾, 期待您的下次光临");
return;
}
System.out.println("我们提供如下车辆:");
for (int i = 0; i < allRent.length; i++) {
System.out.println(allRent[i].carInfo());
}
System.out.println("请问您一共需要几辆汽车? (请输入数字)");
int numberCar = input.nextInt();
Car[] picking = new Car[numberCar];
for (int i = 0; i < numberCar; i++) {
do {
System.out.println("请输入您的第 " + (i + 1) + " 辆车的序号? "
+ "(请输入1-" + allRent.length + ")");
choice = input.nextInt();
}
while (choice < 1 || choice > 6);
picking[i] = allRent[choice - 1];
}
System.out.println("您需要使用的天数: ");
int days = input.nextInt();
int totalRent = 0, totalCargo = 0, totalPeople = 0;
System.out.println("========您的租赁信息如下: ========");
System.out.println("您的租赁天数为: " + days + "天");
System.out.print("租赁的车辆: ");
for (int i = 0; i < numberCar; i++) {
System.out.print(picking[i] + " ");
totalRent += days * picking[i].getRent();
totalCargo += picking[i].getCargo();
totalPeople += picking[i].getPeople();
}
System.out.println("");
System.out.println("总计租金为: " + totalRent + "元");
System.out.println("总计载人数为: " + totalPeople + "人");
System.out.println("总计载货量为: " + totalCargo + "顿");
System.out.println("谢谢使用租车系统, 欢迎下次光临!");
return;
}
}