这是我的代码,仅供参考,欢迎大家指正,谢谢!!
//测试类 package com.imooc; import java.util.Scanner; public class Initial { 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 input=new Scanner(System.in); int u=input.nextInt(); if(u==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.getCost()+"/每天 \t\t"+currentCar.getCapacity()+"人"); } else if(currentCar instanceof PickUp){ System.out.println(""+i+"\t"+currentCar.getName()+"\t"+currentCar.getCost()+"/每天 \t\t"+currentCar.getCapacity()+"人/"+currentCar.getCapacity1()+"吨"); } else{ System.out.println(""+i+"\t"+currentCar.getName()+"\t"+currentCar.getCost()+"/每天\t\t"+currentCar.getCapacity1()+"吨"); } i++; } System.out.println("请输入您要租汽车的数量:"); int n=input.nextInt(); int fair=0; int contain=0; int contain1=0; int[] a = new int[6]; for(int j=1;j<=n;j++){ System.out.println("请输入第"+j+"辆汽车的序号:"); i=input.nextInt(); a[j-1]=i; fair+=carsForRent[i-1].cost; contain+=carsForRent[i-1].capacity; contain1+=carsForRent[i-1].capacity1; } System.out.println("请输入租车天数:"); int day=input.nextInt(); fair*=day; System.out.println("您的账单:"); System.out.println("***可载人的车有:"); int h=0; for(int k=0;k<n;k++){ if(a[k]!=5||a[k]!=6){ System.out.print(carsForRent[(a[k]-1)].name+"\t"); h++; } } if(h==0){ System.out.println("***没有可以载人的车!"); } else System.out.println("共载人:"+contain+"人"); h=0; System.out.println("***可以运货的车有:"); for(int k=0;k<n;k++){ if(a[k]==5||a[k]==6||a[k]==3){ System.out.print(carsForRent[(a[k]-1)].name+"\t"); h++; } } if(h==0){ System.out.println("***没有可以运货的车!"); } else System.out.println("共载货:"+contain1+"吨"); System.out.println("***租车总价格:"+fair+"元"); } input.close(); System.out.println("感谢您的使用!!!"); } }
//父类
package com.imooc;
public class Car {
String name;
int cost;
int capacity;
int capacity1;
public String getName(){
return name;
}
public int getCost(){
return cost;
}
public int getCapacity(){
return capacity;
}
public int getCapacity1(){
return capacity1;
}
}
//三个子类(1)
package com.imooc;
public class PassengerCar extends Car{
public PassengerCar(String name,int cost,int capacity){
this.name=name;
this.cost=cost;
this.capacity=capacity;
}
public String getName(){
return name;
}
public int getCost(){
return cost;
}
public int getCapacity(){
return capacity;
}
}
//三个子类(2)
package com.imooc;
public class PickUp extends Car {
public PickUp(String name,int cost,int capacity,int capacity1){
this.name=name;
this.cost=cost;
this.capacity=capacity;
this.capacity1=capacity1;
}
public String getName(){
return name;
}
public int getCost(){
return cost;
}
public int getCapacity(){
return capacity;
}
public int getCapacity1(){
return capacity1;
}
}
//三个子类(3)
package com.imooc;
public class Trunk extends Car {
public Trunk(String name,int cost,int capacity1){
this.name=name;
this.cost=cost;
this.capacity1=capacity1;
}
public String getName(){
return name;
}
public int getCost(){
return cost;
}
public int getCapacity1(){
return capacity1;
}
}