汽车租赁系统1.0版本比较简陋,以后还会有2.0、3.0……就像《我爱发明》里面的一代机器二代机器,三代机器一样,是一个迭代更新的过程(最近比较忙,可能会很久),这个1.0版本很简陋,也请大家提提意见。
下面是代码区:
首先是文件Car.java
package Car;public class Car {String brand;String type;String vehicleid;int rent;public Car() {// 默认构造函数}public Car(String tempbrand, String temptype, String tempvehicleid, int temprent) {brand = tempbrand;type = temptype;vehicleid = tempvehicleid;rent = temprent;}public double getTotalRent(int tempday) {if (tempday <= 7)return tempday * rent;else if (tempday > 7 && tempday <= 30)return tempday * rent * 0.9;else if (tempday > 30 && tempday <= 150)return tempday * rent * 0.8;elsereturn tempday * rent * 0.7;}
}
然后是 文件Main.java
我简单弄了个while循环,如果你输入了系统里面没有的型号或者输错了,它会一直提醒你重新输入,直到你正确输入,然后输入租车天数才会退出。
package Car;import java.util.Scanner;
public class Main {public static void main(String[] args) {// 数组初始化Car[] cars = new Car[4]; // 假设我们需要四个元素cars[0] = new Car("长安", "X", "京CNY3284", 800);cars[1] = new Car("长安", "Y", "京NY28588", 300);cars[2] = new Car("吉利", "G", "京NT37465",300);cars[3] = new Car("吉利", "H", "京NT96968",500);// 示例:计算一个车的总租金System.out.println("请输入你想租车的型号");Scanner sc = new Scanner(System.in);while (true){String type = sc.next();int index=-1;for(int i=0;i<cars.length;i++) {if(cars[i].type.equals(type))index = i;}if(index<0) {System.out.println("对不起,没有您想要的型号");System.out.println("请重新输入:");}else{System.out.println("请输入你想租车的天数:");int days = sc.nextInt();System.out.println("该型号租金单价" + cars[index].rent + "元/天");double totalRent = cars[index].getTotalRent(days);System.out.println("您共需支付租金:" + totalRent + "元");break;}}}
}