首页 > 开发 > JAVA > 正文

答答租车系统(代码还能更简洁更容易吗?还能分别计算每辆车的租赁天数)

2016-06-14 20:40:44  来源:慕课网
  做项目要的就是最少的代码实现最多的功能,而且用最简单的知识来实现它。
-------玄鉴
//父类----车,省略了,get/set方法。
public class Che {
private String name; private int daymoney; private int day; public void show() { System.out.print("\t"+this.name+ "\t"+this.daymoney+"元/天\t"); }}   //子类——客车.省略了,get/set方法。
public class Car extends Che {
private int personNum; Car(String name, int daymoney,int personNum) { super(name, daymoney); this.personNum=personNum; } @Override public void show() { //super.show(); System.out.print("\t"+this.getName()+ "\t"+this.getDaymoney()+"元/天\t"); System.out.print("载人:"+personNum+"人"); }   ————————————————————
//子类——货车 省略了,get/set方法。
  public class Bus extends Che {
private int cargoNum; Bus(String name, int daymoney,int cargoNum) { super(name, daymoney); this.cargoNum=cargoNum; } @Override public void show() { super.show(); System.out.print("载货:"+cargoNum+"吨"); }   }
  //子类——载货载人两用车。 省略了,get/set方法。
public class Pika extends Che {
private int personNum; private int cargoNum; Pika(String name, int daymoney,int personNum,int cargoNum) { super(name, daymoney); this.personNum=personNum; this.cargoNum=cargoNum; } @Override public void show() { super.show(); System.out.print("载货:"+cargoNum+"吨"+ "载人:"+personNum+"人"); }   ————————————————————
//流程测试类
  import java.util.Scanner;
  public class Liucheng {
public static void main(String[] args) { System.out.println("欢迎使用租车系统"); System.out.println("您是否要租车(1.是 2.否)"); Scanner in = new Scanner(System.in); Che[] ches = { new Car("奥迪A4", 500, 4), new Car("马自达", 400, 4), new Pika("皮卡雪6", 450, 4, 2), new Bus("金龙", 800, 20), new Bus("松花江", 400, 4), new Bus("依维柯", 1000, 20) }; if (in.nextInt() == 1) { System.out.println("序号" + "\t汽车名称" + "\t租金" + "\t容量"); for (int i = 0; i < ches.length; i++) { System.out.print(i); ches[i].show(); System.out.println(""); } }else{ System.out.println("程序退出。"); return; } System.out.println("请问你要租车的数量:"); int a = in.nextInt(); int sum = 0; int d = 0; for (int i = 0; i < a; i++) { System.out.println("请问你租赁的第" + (i + 1) + "辆车的序号是:"); int b = in.nextInt();   if (b>=0&&b<6) {
System.out.println("请问你要租的天数:");
int c = in.nextInt();
ches[b].setDay(c);
d = ches[b].getDaymoney() * ches[b].getDay();
sum = sum + d; System.out.println("第" + (i + 1) + "辆车" + ches[b].getName() + "的价钱为:" + d); }else { System.out.println("输入有误,请重新输入"); continue;   }
}
System.out.println("总价钱为:" + sum);
}
}