参考了老师的内容。以下需一路正确操作才能执行成功,输入错误时的情况没有做处理。
package com.imooc.zuche;
public class Car {
public String name;
public 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;
}
}
package com.imooc.zuche;
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;
}
}
package com.imooc.zuche;
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;
}
}
package com.imooc.zuche;
public class PickUp extends Car {
private double peoplecapacity;
private double cargoCapacity;
public PickUp(String name,double rent,double peoplecapacity,double cargoCapacity){
this.name = name;
this.rent = rent;
this.peoplecapacity = peoplecapacity;
this.cargoCapacity = cargoCapacity;
}
public double getPeoplecapacity() {
return peoplecapacity;
}
public void setPeoplecapacity(double peoplecapacity) {
this.peoplecapacity = peoplecapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
}
package com.imooc.zuche;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car[] carsForRent = {
new PassengerCar("奥迪A4",500,4) ,
new PassengerCar("马自达6",400,4),
new PickUp("皮卡雪6",450,4,2),
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\t容量");
int i = 1;
for(Car currentCar:carsForRent){ //遍历数组显示租车类型及其价目
if(currentCar instanceof PassengerCar){ //判断对象是否为PassengerCar类的一个实例
System.out.println(" "+i+"\t"+currentCar.getName()+"\t\t"+currentCar.getRent()+"元/天\t"+((PassengerCar)currentCar).getPeoplecapacity()+"人");
}else if(currentCar instanceof Trunk){ //判断对象是否为Trunk类的一个实例
System.out.println(" "+i+"\t"+currentCar.getName()+"\t\t"+currentCar.getRent()+"元/天\t"+((Trunk)currentCar).getCargoCapacity()+"吨");
}else if(currentCar instanceof PickUp){ //判断对象是否为PickUp类的一个实例
System.out.println(" "+i+"\t"+currentCar.getName()+"\t\t"+currentCar.getRent()+"元/天\t"+((PickUp)currentCar).getPeoplecapacity()+"人,"+((PickUp)currentCar).getCargoCapacity()+"吨");
}
i++;
}
System.out.println("请输入您要租车的数量:");
//接收用户输入的数量
int amount = scan.nextInt();
//定义一个数组,存放所选择的车辆信息
Car rent[] = new Car[amount];
for(int n=0;n<amount;n++){
System.out.println("请输入第"+(n+1)+"辆车的序号:");
int num = scan.nextInt();
rent[n] = carsForRent[num-1];
}
System.out.println("请输入租车的天数:");
//接收用户输入的租车天数
int days = scan.nextInt();
//定义租金总金额sum
double sum = 0;
System.out.println("以下是您租车的信息:");
System.out.println("租车天数:"+days);
System.out.println("租车型号:");
for(int n=0;n<rent.length;n++){
System.out.println(rent[n].getName());
sum += rent[n].getRent()*days;
}
System.out.println("需支付的金额:\t"+sum+"元");
}
scan.close();
}
}