这里感谢下幕友-作者:Confused_wz,链接:https://www.imooc.com/article/301274#comment,最终才能完成。
期初对系统的展示部分具体不太知道怎么样实现,看完比较有帮助,对系统的整体脉络考虑的比较周到。
先看效果:
上代码
Car类代码以及其他子类代码
``` package Demo; public abstract class Car { int no; //序号 String name; //车辆名称 double price; //车辆租金 int capacity; //载人量 int cargocapacity; //载货量 int inventory; //库存量 int salessum; //销售量 //定义构造函数 public Car(int no,String name,double price,int capacity, int cargocapacity,int inventory){ this.no = no; this.name = name; this.price = price; this.capacity = capacity; this.cargocapacity = cargocapacity; this.inventory = inventory; this.salessum = 0; //销售初始量为0 } //打印菜单列表输出 public void print(){ if(capacity == 0){//载人量 System.out.println(no+".\t"+name+"\t"+price+"\t"+cargocapacity+"吨"); }else if(cargocapacity == 0){//载货量 System.out.println(no+".\t"+name+"\t"+price+"\t"+"载人:"+capacity+"人"); }else{ System.out.println(no+".\t"+name+"\t"+price+"\t"+"载人:"+capacity+"人|载货"+cargocapacity+"吨"); } } } package Demo; //载人车辆 public class Sedan extends Car { public Sedan(int no, String name, double price, int capacity, int inventory) { super(no, name, price, capacity, 0,inventory); // TODO 自动生成的构造函数存根 } } package Demo; //载货车辆 public class Truck extends Car { public Truck(int no, String name, double price, int cargocapacity, int inventory) { super(no, name, price, 0, cargocapacity, inventory); // TODO 自动生成的构造函数存根 } } package Demo; //皮卡车辆 public class Pikup extends Car { public Pikup(int no, String name, double price, int capacity, int cargocapacity, int inventory) { super(no, name, price, capacity, cargocapacity, inventory); // TODO 自动生成的构造函数存根 } } ```
主函数
package Demo;
import java.util.Scanner;
public class CarRentalSystem {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Car cars[] ={new Sedan(1,"奥迪Aa",500.0,4,50),new Sedan(2,"马自达",400.0,4,50),
new Pikup(3,"皮卡雪6",450.0,4,2,50),new Sedan(4,"金龙",800.0,20,50),
new Truck(5,"松花江",400.0,4,50),new Truck(6,"依维柯",1000.0,20,50)};
System.out.println("***欢迎使用达达租车系统***");
System.out.println("您是否要租车? 1:是 [输入其他任意键退出]");
System.out.print("请输出:");
Scanner input = new Scanner(System.in);//创建Scanner对象
//判断用户输入是否为数字,并且为1,否则退出
if(!input.hasNextInt() ||input.nextInt() !=1){
System.out.println("再见! 欢迎下次使用!");
return;
}
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
//遍历数组,并打印清单
for(Car car :cars){
car.print();
}
System.out.println("请输入租车总数量:");
int sum = input.nextInt();//租车总数
int totalinventory = 0; //总库存总量
for(Car car:cars){
totalinventory += car.inventory;
}//获得总库存量
while(sum<1 || sum>=totalinventory){
if(sum<1){
System.out.println("输入有误,请重新输入:");
}
else {
System.out.println("库存不足哦!!!");
System.out.println("请输入小于"+totalinventory+"的数量!!!");
}
sum = input.nextInt();
}//输入数量不合理或输入大于库存量,让客户重新输入
//总租金,总载客量,总载货量
int Totalrent = 0; int Totalcapacity =0; int Totalcargo = 0;
for(int i=1;i<=sum;i++){
System.out.println("请输入第"+i+"辆车的序号:");
int no = input.nextInt();
while(no<1 || no>6){
System.out.println("输入有误,请重新输入1~6之间的序号:");
no = input.nextInt();
}//获取车辆输入序号,有误并重新输入
//获取总金额,总载客量,总载货量
Totalrent += cars[no-1].price;
Totalcapacity += cars[no-1].capacity;
Totalcargo += cars[no-1].cargocapacity;
//每租出一辆车,对应的车型销量加一,库存减一
cars[no-1].salessum ++;
cars[no-1].inventory --;
}
System.out.println("请输入租车天数:");
int n =input.nextInt();
while(n<1){
System.out.println("输入有误,请重新输入:");
n =input.nextInt();
}
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
Totalrent *=n; //总租金=单日的租金*天数;
for(int i=0;i<6;i++){
if(cars[i].capacity>0){
if(cars[i].salessum>0){
System.out.print(cars[i].name+"*"+cars[i].salessum+"\t");
}
}
}//遍历所有车型,载客量和销售车辆》0;并打印车的名称和车数量清单
System.out.println("共载人:"+Totalcapacity+"人\n");
System.out.println("***载货的车有:");
for(int i=0;i<6;i++){
if(cars[i].cargocapacity>0){
if(cars[i].salessum>0){
System.out.print(cars[i].name+"*"+cars[i].salessum+"\t");
}
}
}//遍历所有车型,载货量和销售车辆》0;并打印车的名称和车数量清单
System.out.println("共载货:"+Totalcargo+"吨");
System.out.println("租车总价格为:"+Totalrent);
}
}