定义不同的汽车类
主类:
package com.imooc; public class Car { private int carNumber; //汽车的序号 private String carName; //汽车的名字 private int carRent;//租车的费用 public int getCarNumber() { return carNumber; } public void setCarNumber(int carNumber) { this.carNumber = carNumber; } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getCarRent() { return carRent; } public void setCarRent(int carRent) { this.carRent = carRent; } }
三个不同的汽车类
package com.imooc; public class PassengerCar extends Car { private int passengers;//载客量 public PassengerCar(int carNumber,String carName,int carRent,int passengers){ //定义有参函数 this.setCarName(carName); this.setCarNumber(carNumber); this.setPassengers(passengers); this.setCarRent(carRent); } public int getPassengers() { return passengers; } public void setPassengers(int passengers) { this.passengers = passengers; } }
package com.imooc; public class Truck extends Car { private int cargo;//载货量 public Truck(int carNumber,String carName,int carRent,int cargo){ //定义一个有参的构造方法 this.setCarName(carName); this.setCarNumber(carNumber); this.setCarRent(carRent); this.setCargo(cargo); } public int getCargo() { return cargo; } public void setCargo(int cargo) { this.cargo = cargo; } }
package com.imooc; public class PickUp extends Car { private int cargo;//载货量 private int passengers;//载客量 public PickUp(int carNumber,String carName,int carRent,int cargo,int passengers){ this.setCargo(cargo); this.setCarName(carName); this.setCarNumber(carNumber); this.setCarRent(carRent); this.setPassengers(passengers); } public int getCargo() { return cargo; } public void setCargo(int cargo) { this.cargo = cargo; } public int getPassengers() { return passengers; } public void setPassengers(int passengers) { this.passengers = passengers; } }
最后是主代码
package com.imooc; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class CarSystem { /** * @param args */ public static void main(String[] args) { System.out.println("欢迎使用答答汽车系统:"); System.out.println("你是否要租车:1是 0否"); Scanner sc = new Scanner(System.in); int input = sc.nextInt(); // new Sacnner(System.in).nextInt() 才是有效的数 if(input == 1){ System.out.println("您可以租车的类型及价目表如下: "); System.out.println("序号\t"+"汽车名称\t"+"租金\t"+"容量\t"); Car car1 = new PassengerCar(1, "奥迪A4", 500, 4); Car car2 = new PassengerCar(2, "马自达6", 400, 4); Car car3 = new PickUp(3, "皮卡雪6", 450, 2, 4); Car car4 = new Truck(4, "金龙", 800, 20); Car car5 = new Truck(5, "松花江", 400, 4); Car car6 = new Truck(6, "超大车", 1000, 20); Car[] rentcars = {car1,car2,car3,car4,car5,car6}; for (Car rentcar : rentcars){//列出所有的车 if (rentcar instanceof PassengerCar){ System.out.println(rentcar.getCarNumber()+".\t"+rentcar.getCarName()+"\t"+rentcar.getCarRent()+"元/天\t载人:"+((PassengerCar)rentcar).getPassengers()); }else if(rentcar instanceof PickUp){ System.out.println(rentcar.getCarNumber()+".\t"+rentcar.getCarName()+"\t"+rentcar.getCarRent()+"元/天\t载人:"+((PickUp)rentcar).getPassengers()+"\t载货:"+((PickUp)rentcar).getCargo()+"吨"); }else if(rentcar instanceof Truck){ System.out.println(rentcar.getCarNumber()+".\t"+rentcar.getCarName()+"\t"+rentcar.getCarRent()+"元/天\t载货:"+((Truck)rentcar).getCargo()+"吨"); } } System.out.println("请你输入要租车的数量"); int counts = sc.nextInt(); List<Car> bookcars = new ArrayList<>();//定义一个list去存储要租的车 if (counts>0){ for(int count = 1; count<=counts; count ++){ System.out.println("请你输入第"+count+"辆车的序号"); int selectcar = sc.nextInt(); bookcars.add(rentcars[selectcar-1]);//保存客户输入的车的序号 } System.out.println("请输入要租的天数"); int rentdays = sc.nextInt(); System.out.println("您的账单为:"); int totalFee = 0; int totalCargo = 0; int totalPassenger = 0; String allcarname = ""; for(int i=0; i<bookcars.size() ;i++){//列出所有用户所选的汽车 if(allcarname != ""){ allcarname = allcarname.concat(","); } Car rentcar1 = bookcars.get(i); totalFee = totalFee + rentcar1.getCarRent()*rentdays; if (rentcar1 instanceof PassengerCar){ totalPassenger = totalPassenger + ((PassengerCar)rentcar1).getPassengers(); } if (rentcar1 instanceof PickUp){ totalPassenger = totalPassenger + ((PickUp)rentcar1).getPassengers(); totalCargo = totalCargo + ((PickUp)rentcar1).getCargo(); } if (rentcar1 instanceof Truck){ totalCargo = totalCargo + ((Truck)rentcar1).getCargo(); } allcarname = allcarname.concat(rentcar1.getCarName()); } System.out.println("租的车有:"+allcarname); System.out.println("总金额: "+totalFee); System.out.println("总载货量: "+totalCargo); System.out.println("总载客量: "+totalPassenger); }else System.out.println("请输入正确的数量"); }else{ System.out.println("谢谢使用!"); } } }
good