Car.java 所有汽车的父类
package com.imooc; public class Car { protected String name; protected double rent; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getRent() { return rent; } public void setRent(double rent) { this.rent = rent; } }
passengerCar.java 客车
package com.imooc; public class passengerCar extends Car { private double peopleCapacity; public passengerCar(String name,double rent,double peoplecapacity){ this.name=name; this.rent=rent; this.peopleCapacity=peoplecapacity; } public double getPeopleCapacity() { return peopleCapacity; } public void setPeopleCapacity(double peopleCapacity) { this.peopleCapacity = peopleCapacity; } }
PickUp.java 皮卡
package com.imooc; public class PickUp extends Car { private double cargoCapacity; private double peopleCapacity; public PickUp(String name,double rent,double cargoCapacity,double peopleCapacity){ this.name=name; this.rent=rent; this.cargoCapacity=cargoCapacity; this.peopleCapacity=peopleCapacity; } public double getCargoCapacity() { return cargoCapacity; } public void setCargoCapacity(double cargoCapacity) { this.cargoCapacity = cargoCapacity; } public double getPeopleCapacity() { return peopleCapacity; } public void setPeopleCapacity(double peopleCapacity) { this.peopleCapacity = peopleCapacity; } }
Trunk.java 货车
package com.imooc; public class Trunk extends Car { private double cargoCapacity; public Trunk(String name,double rent,double cargoCapacity){ this.name=name; this.rent=rent; this.cargoCapacity=cargoCapacity; } public double getCargoCapacity() { return cargoCapacity; } public void setCargoCapacity(double cargoCapacity) { this.cargoCapacity = cargoCapacity; } }
Test.java 测试类
package com.imooc; import java.util.Scanner; public class Test{ public static void main(String[] args) { Car[] carsForRent={new passengerCar("奥迪A4",500,4),new passengerCar("马自达6", 400, 4),new PickUp("皮卡雪6", 450,2, 4),new passengerCar("金龙", 800, 20),new Trunk("松花江", 400, 4),new Trunk("依维柯", 1000, 20)}; System.out.println("欢迎使用答答租车系统:"); System.out.println("你是否需要租车:1是 0否"); Scanner scan=new Scanner(System.in); String input=scan.next(); if(input.equals("1")){ System.out.println("你可租车的类型及其价目表:"); System.out.println("序号\t汽车名称\t租金\t\t容量"); int i=1; for(Car currentCar:carsForRent){ if(currentCar instanceof passengerCar){ System.out.println("" + i +"\t"+ currentCar.getName() + "\t" + currentCar.getRent() + "元/天\t" + ((passengerCar)currentCar).getPeopleCapacity()+"人"); } if(currentCar instanceof PickUp){ System.out.println("" + i +"\t"+ currentCar.getName() + "\t" + currentCar.getRent() + "元/天\t" + ((PickUp)currentCar).getPeopleCapacity()+"人 "+((PickUp)currentCar).getCargoCapacity()+"吨"); } if(currentCar instanceof Trunk){ System.out.println("" + i +"\t"+ currentCar.getName() + "\t" + currentCar.getRent() + "元/天\t" + ((Trunk)currentCar).getCargoCapacity()+"吨"); } i++; } System.out.println("请输入您要租汽车的数量:"); int rentNum=scan.nextInt(); int[] carsRent=new int[rentNum]; for(int j=0;j<rentNum;j++){ System.out.println("请输入第"+(j+1) + "辆车的序号:"); carsRent[j]=scan.nextInt(); } System.out.println("请输入租车天数:"); int daysRent=scan.nextInt(); int totalPeopley=0; int totalCargoy=0; double totalMoney=0; String carsForPeople=""; String carsForCargo=""; for(int j=0;j<rentNum;j++){ totalMoney+=carsForRent[carsRent[j]-1].getRent(); if(carsForRent[carsRent[j]-1] instanceof passengerCar){ totalPeopley+=((passengerCar)carsForRent[carsRent[j]-1]).getPeopleCapacity(); carsForPeople+=carsForRent[carsRent[j]-1].getName()+"\t"; } if(carsForRent[carsRent[j]-1] instanceof PickUp){ totalPeopley+=((PickUp)carsForRent[carsRent[j]-1]).getPeopleCapacity(); totalCargoy+=((PickUp)carsForRent[carsRent[j]-1]).getCargoCapacity(); carsForPeople+=carsForRent[carsRent[j]-1].getName()+"\t"; carsForCargo+=carsForRent[carsRent[j]-1].getName()+"\t"; } if(carsForRent[carsRent[j]-1] instanceof Trunk){ totalCargoy+=((Trunk)carsForRent[carsRent[j]-1]).getCargoCapacity(); carsForCargo+=carsForRent[carsRent[j]-1].getName()+"\t"; } } totalMoney=totalMoney*daysRent; System.out.println("您的账单:"); System.out.println("***可载人的车有:"); System.out.println(carsForPeople +"\t共载人:" +totalPeopley+"人" ); System.out.println("***载货的车有:"); System.out.println(carsForCargo +"\t共货:" +totalCargoy+"吨" ); System.out.println("***租车总价格:"+totalMoney+"元"); scan.close(); } } }