/
车类Car
*/
public abstract class Car {
public String name;
public int personCapacity;//载客量
public int goodCapacity;//载货量
public int price;//租车价格
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void printInfo();
}
//客车类
public class Coach extends Car {
public Coach(String name,int price,int personCapacity){
this.name=name;
this.personCapacity=personCapacity;
this.price=price;
}
public void printInfo(){
System.out.println(this.name+"\t"+this.price
+"元/天\t载客:"+this.personCapacity+"人");
}
//货车
public class Truck extends Car {
public final int personCapacity=0;//货车载客量为0
public Truck(String name,int price,int goodCapacity){
this.name=name;
this.price=price;
this.goodCapacity=goodCapacity;
}
public void printInfo(){
System.out.println(this.name+"\t"+this.price
+"元/天\t载货:"+this.goodCapacity+"吨");
}
}
//皮卡,既能载客又能载货
public class Pk extends Car {
public Pk(String name,int price,int personCapacity,int goodCapacity){
this.name=name;
this.price=price;
this.personCapacity=personCapacity;
this.goodCapacity=goodCapacity;
}
public void printInfo(){
System.out.println(this.name+"\t"+this.price
+"元/天\t载客:"+this.personCapacity+"人\t载货:"+this.goodCapacity+"吨");
}
}
import java.util.Scanner;
public class Initial {
/**
* @param args
*/
public static void main(String[] args) {
Car[] cars={new Coach("奥迪",500,4),new Coach("马自达6",400,4)
,new Coach("金龙",800,20),new Pk("皮卡雪6",450,4,2)
,new Truck("松花江",400,4),new Truck("依维柯",1000,20)};
Scanner sc=new Scanner(System.in);
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车?0:不是,1:是");
int i=sc.nextInt();
switch(i){
case 0:{
System.out.println("您已退出该系统");
break;
}
case 1:{
System.out.println("您可租车的车型及其价目表如下:");
System.out.println("序号\t汽车名称\t租金\t容量");
for(int j=0;j<cars.length;j++){
System.out.print(j+1+"\t");
cars[j].printInfo();
}
System.out.println("请输入您要租车的数量:");
int number=sc.nextInt();
System.out.println("奥迪的数量:");
int number1=sc.nextInt();
System.out.println("马自达6的数量:");
int number2=sc.nextInt();
System.out.println("金龙的数量:");
int number3=sc.nextInt();
System.out.println("皮卡雪6的数量:");
int number4=sc.nextInt();
System.out.println("松花江的数量:");
int number5=sc.nextInt();
System.out.println("依维柯的数量:");
int number6=sc.nextInt();
System.out.println("请输入您要租车的天数:");
int day=sc.nextInt();
System.out.println("您租车的金额为:"
+(cars[0].price*number1+cars[1].price*number2
+cars[2].price*number3+cars[3].price*number4+
cars[4].price*number5+cars[5].price*number6)*day+"元");
break;
}
}
}
}
热门评论
可以可以,想的不错,用数组来new对象,你想到了不错
以上输出结果示意图,还有一些并未完善,希望提出宝贵意见