提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、汽车租赁系统
- 1.汽车类 抽象父类
- 2.客车类 子类
- 3.轿车类 子类
- 4.业务类
- 5.功能测试类(运行)
- 总结
前言
使用数组,面向对象的知识实现一个汽车租赁系统
制作属于你自己的秋名山租赁公司!!!
1.汽车租赁信息表如下
2.类和属性
一、汽车租赁系统
1.汽车类 抽象父类
package week_test;public abstract class Car {private String carNumber;//车牌号private String brand;//品牌private int money;//租金public Car() {}public Car(String brand) {this.brand = brand;}public Car(String carNumber, String brand, int money) {this.carNumber = carNumber;this.brand = brand;this.money = money;}public String getCarNumber() {return carNumber;}public void setCarNumber(String carNumber) {this.carNumber = carNumber;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public int getMoney() {return money;}public void setMoney(int money) {this.money = money;}//租赁费用public abstract double allMoney(int days,double dayMoney);
}
2.客车类 子类
package week_test;public class PassengerCar extends Car{private String seating;//座位数public PassengerCar() {}public PassengerCar(String brand, String seating) {super(brand);this.seating = seating;}public PassengerCar(String carNumber, String brand, int money, String seating) {super(carNumber, brand, money);this.seating = seating;}public String getSeating() {return seating;}public void setSeating(String seating) {this.seating = seating;}//重写租赁费用@Overridepublic double allMoney(int days, double dayMoney) {if (days>=150){return days*dayMoney*0.6;}else if (days>=30){return days*dayMoney*0.7;}else if (days>=7){return days*dayMoney*0.8;}else if (days>=3){return days*dayMoney*0.9;}elsereturn days*dayMoney;}
}
3.轿车类 子类
package week_test;public class SaloonCar extends Car{private String type;//型号public SaloonCar() {}public SaloonCar(String brand, String type) {super(brand);this.type = type;}public SaloonCar(String carNumber, String brand, int money, String type) {super(carNumber, brand, money);this.type = type;}public String getType() {return type;}public void setType(String type) {this.type = type;}//重写租赁费用@Overridepublic double allMoney(int days, double dayMoney) {if (days>150){return days*dayMoney*0.7;}else if (days>30){return days*dayMoney*0.8;}else if (days>7){return days*dayMoney*0.9;}elsereturn days*dayMoney;}
}
4.业务类
package week_test;public class CarBusiness {//筛选车辆public static Car searchCar(Car car,Car[] cars){//遍历cars对象数组for (Car c : cars) {if (c instanceof SaloonCar) {//将客户选择的品牌和型号做比较if (c.getBrand().equals(car.getBrand())&&((SaloonCar) c).getType().equals(((SaloonCar) car).getType())){//相同 则返回 对应的对象 方便后续输出车牌号return c;}}if (c instanceof PassengerCar){if (c.getBrand().equals(car.getBrand())&&((PassengerCar) c).getSeating().equals(((PassengerCar) car).getSeating())){return c;}}}return null;}
}
5.功能测试类(运行)
package week_test;import java.util.Scanner;import static week_test.CarBusiness.searchCar;//导入静态方法searchCarpublic class TestCar {public static void main(String[] args) {//将汽车信息装入对象数组carsSaloonCar sc1 = new SaloonCar("京NY28588","宝马",800,"X6");SaloonCar sc2 = new SaloonCar("京CNY3284","宝马",600,"550i");SaloonCar sc3 = new SaloonCar("京NT37465","别克",300,"林荫大道");SaloonCar sc4 = new SaloonCar("京NT96968","别克",600,"GL8");PassengerCar pc1 = new PassengerCar("京6566754","金杯",800,"16座");PassengerCar pc2 = new PassengerCar("京8696997","金龙",800,"16座");PassengerCar pc3 = new PassengerCar("京9696996","金杯",1500,"34座");PassengerCar pc4 = new PassengerCar("京8696998","金龙",1500,"34座");Car [] cars ={sc1,sc2,sc3,sc4,pc1,pc2,pc3,pc4};//测试功能Scanner sc = new Scanner(System.in);System.out.println("***********欢迎光临秋名山守望者汽车租赁公司***********");String brand=null;//品牌String type=null;//型号Car car2=null;//创建父类对象,接受品牌和型号//租赁的汽车类型System.out.println("1、轿车 2、客车");System.out.println("请选择你要租赁的汽车类型:");int i = sc.nextInt();if (i==1){//租赁的汽车品牌System.out.println("请选择你要租赁的汽车品牌: 1、别克 2、宝马");brand=sc.nextInt()==1?"别克":"宝马";if (brand.equals("别克")){//租赁的汽车类型System.out.println("请选择你要租赁的汽车类型: 1、林荫大道 2、GL8");type=sc.nextInt()==1?"林荫大道":"GL8";}else {//租赁的汽车类型System.out.println("请选择你要租赁的汽车类型: 1、X6 2、550i");type=sc.nextInt()==1?"X6":"550i";}car2 = new SaloonCar(brand,type);}if (i==2){//租赁的汽车品牌System.out.println("请选择你要租赁的汽车品牌: 1、金龙 2、金杯");brand=sc.nextInt()==1?"金龙":"金杯";//租赁的汽车座位数System.out.println("请选择你要租赁的汽车座位数: 1、16座 2、34座");type=sc.nextInt()==1?"16座":"34座";car2 = new PassengerCar(brand,type);}Car car1;//接受searchCar传过来的对象car1=searchCar(car2,cars);//租赁的天数System.out.println("请输入你要租赁的天数: ");int i1 = sc.nextInt();//分配给您的汽车牌号System.out.println("分配给您的汽车牌号是: "+car1.getCarNumber());//支付的费用System.out.println("您需要支付的租赁费用是: "+car1.allMoney(i1,car1.getMoney())+"元。");}
}
轿车运行截图如下:
客车运行截图如下:
总结
难点:获取客户输入的汽车品牌和类型(座位数),遍历对象数组,if语句判断是否相等,输出你想要的对象,获取到绑定的车牌号。
详解:
brand和type用条件(三元)运算符讲客户输入的int类型的值转化为字符串
car2 向上转型存入brand和type接收到的客户输入的值
//部分代码片段brand=sc.nextInt()==1?"别克":"宝马";type=sc.nextInt()==1?"林荫大道":"GL8";car2 = new SaloonCar(brand,type);car1=searchCar(car2,cars);
//部分代码片段//筛选车辆public static Car searchCar(Car car,Car[] cars){//遍历cars对象数组for (Car c : cars) {if (c instanceof SaloonCar) {//将客户选择的品牌和型号做比较if (c.getBrand().equals(car.getBrand())&&((SaloonCar) c).getType().equals(((SaloonCar) car).getType())){//相同 则返回 对应的对象 方便后续输出车牌号return c;}}if (c instanceof PassengerCar){if (c.getBrand().equals(car.getBrand())&&((PassengerCar) c).getSeating().equals(((PassengerCar) car).getSeating())){return c;}}}return null;}
希望能与您共进步。