学霸的自我修养
2019-12-19 09:51
package systemofcarrent; import java.util.Scanner; public abstract class Car { private String carName; private int rentMoney; public Car(String carName, int rentMoney){ this.carName = carName; this.rentMoney = rentMoney; } public void setCarName(String carName) { this.carName = carName; } public void setRentMoney(int rentMoney) { this.rentMoney = rentMoney; } public String getCarName() { return carName; } public int getRentMoney() { return rentMoney; } public abstract String printInfo(); } class FamilyCar extends Car{ private int peopleCap; public FamilyCar(String carName, int rentMoney, int peopleCap){ super(carName, rentMoney); this.peopleCap = peopleCap; } public void setPeopleCap(int peopleCap) { this.peopleCap = peopleCap; } public int getPeopleCap() { return peopleCap; } public String printInfo(){ return super.getCarName() + "\t" + super.getRentMoney() + "元/天 "+ "\t载人:" + peopleCap + "人"; } } class Trunk extends Car{ private int cargoCap; public Trunk(String carName, int rentMoney, int cargoCap){ super(carName, rentMoney); this.cargoCap = cargoCap; } public void setCargoCap(int cargoCap) { this.cargoCap = cargoCap; } public int getCargoCap() { return cargoCap; } public String printInfo(){ return super.getCarName() + "\t" + super.getRentMoney() + "元/天 "+ "\t载货:" + cargoCap + "吨"; } } class Pickup extends Car{ private int cargoCap; private int peopleCap; public Pickup(String carName, int rentMoney, int peopleCap, int cargoCap){ super(carName, rentMoney); this.cargoCap = cargoCap; this.peopleCap = peopleCap; } public void setCargoCap(int cargoCap) { this.cargoCap = cargoCap; } public void setPeopleCap(int peopleCap) { this.peopleCap = peopleCap; } public int getCargoCap() { return cargoCap; } public int getPeopleCap() { return peopleCap; } public String printInfo(){ return super.getCarName() + "\t" + super.getRentMoney() + "元/天 "+ "\t载人:" + peopleCap + "人" + " 载物:" + cargoCap + "吨"; } } class Demo{ public static void main(String[] args) { final int[] totalBill = {0}; int rentDay = 0; int finalBill = 0; Car []cars = new Car[]{ new FamilyCar("奥迪A4", 500, 4), new FamilyCar("马自达6", 400, 4), new Pickup("皮卡雪6", 450, 4, 2 ), new FamilyCar("金龙", 800, 20), new Trunk("松花江", 400, 4), new Trunk("依维柯", 1000, 20) }; System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车:1是 0否"); Scanner s = new Scanner(System.in); int input = s.nextInt(); if(input == 1){ System.out.println("序号 汽车名称 租金 容量"); int count = 1; for(int i = 0; i < cars.length; i++){ System.out.println(count + "\t" + cars[i].printInfo()); count++; } }else{ System.out.println("感谢您的支持,欢迎下次使用"); return; } System.out.println("请输入您要租车的数量:"); Scanner s1 = new Scanner(System.in); int input1 = s1.nextInt(); class A { public void wrongHandle(){ // System.out.println("请输入您要租车的数量:"); // Scanner s1 = new Scanner(System.in); // int input1 = s1.nextInt(); for(int i = 1; i <= input1; i++ ){ System.out.println("请输入第" + i + "辆车的序号:"); Scanner s2 = new Scanner(System.in); int input2 = s2.nextInt(); totalBill[0] += cars[input2 - 1].getRentMoney(); } } } // for(int i = 1; i <= input1; i++ ){ // System.out.println("请输入第" + i + "辆车的序号:"); // Scanner s2 = new Scanner(System.in); // int input2 = s2.nextInt(); // totalBill += cars[input2 - 1].getRentMoney(); // } A a = new A(); a.wrongHandle(); if(input1 == 0){ System.out.println("你的输入有误,请您重新输入。"); a.wrongHandle(); } System.out.println("请输入租车天数:"); Scanner s3 = new Scanner(System.in); int input3 = s3.nextInt(); rentDay = input3; finalBill = totalBill[0] * rentDay; System.out.println("您的账单:\n" + finalBill); } }
66666
前面30行是什么意思?
有点小问题:totalBill[
0]这个定义成常量final了,那后面的操作应该改变不了它的值,所以运行结果,finalBill为0
朋友,你这行代码中
totalBill[
0
] += cars[input2 -
1
].getRentMoney(),“
cars[input2 -
1
]”是什么意思,是指车的单价吗,怎么解释?
mark一下,一会儿对照自己的改一下,感觉你写的封装性比较好
厉害?,hh让我参考参考
Java入门第二季 升级版
530652 学习 · 6091 问题
相似问题