//抽象类
public abstract class Car {
int num;
String name;
int rent;
int capacity;
int capacity2;
}
//载人的车类
public void SetAll(int Vnum,String Vname,int Vrent,int Vcapacity){
num=Vnum;
name=Vname;
capacity=Vcapacity;
rent=Vrent;
}
public void PrintAll(){
System.out.println(num+".\t\t"+name+"\t\t"+rent+"元/天\t\t载人:"+capacity+"人");
}
}
//载货车类
public class Coach extends Car {
public void SetAll(int Cnum,String Cname,int Crent,int Ccapacity){
num=Cnum;
name=Cname;
capacity=Ccapacity;
rent=Crent;
}
public void PrintAll(){
System.out.println(num+".\t\t"+name+"\t\t"+rent+"元/天\t\t载货:"+capacity+"吨");
}
}
//能载人也能载货类
public class Pika extends Car{
public void SetAll(int Pnum,String Pname,int Prent,int Pcapacity1,int Pcapacity2){
num=Pnum;
name=Pname;
capacity=Pcapacity1;
capacity2=Pcapacity2;
rent=Prent;
}
public void PrintAll(){
System.out.println(num+".\t\t"+name+"\t\t"+rent+"元/天\t\t载人:"+capacity+"人"+" 载货:"+capacity2+"吨");
}
}
//main函数
import java.util.Scanner;
public class Dada {
public static void main(String[] args){
System.out.println("欢迎使用哒哒租车系统:");
System.out.println("您是否要租车:1.是 0.否");
Scanner scan=new Scanner(System.in);
int k=scan.nextInt();//获取控制台上用户输入的数字
if(k==1){
System.out.println("您可租车的类型及价目表:");
System.out.println("序号\t\t汽车名称\t\t租金\t\t容量");
Car Aodi=new Van();//创建并显示可租车的对象
((Van) Aodi).SetAll(1,"奥迪A4",500,4);
((Van) Aodi).PrintAll();
Car Mazida=new Van();
((Van) Mazida).SetAll(2,"马自达6",400,4);
((Van) Mazida).PrintAll();
Car Xue=new Pika();
((Pika) Xue).SetAll(3,"皮卡雪6",450,4,2);
((Pika) Xue).PrintAll();
Car Jin=new Van();
((Van) Jin).SetAll(4,"金龙",800,20);
((Van) Jin).PrintAll();
Car Song=new Coach();
((Coach) Song).SetAll(5,"松花江",400,4);
((Coach) Song).PrintAll();
Car Yi=new Coach();
((Coach) Yi).SetAll(6,"依维柯",1000,20);
((Coach) Yi).PrintAll();
System.out.println("请输入您要租汽车的数量:");
int i=scan.nextInt();
int Choose,sum=0,rent1=0,CarDate,manned=0,shipment=0,Allsum=0,Allmanned=0,Allshipment=0;//分别为用户选择的车型,单次金额,租车单价,租车天数,载人量,载货量和总金额
for(int j=1;j<=i;j++){
System.out.println("请选择您要租的第"+j+"辆车的序号");
Choose=scan.nextInt();
if (Choose<1||Choose>6) System.out.println("请输入正确的序号:");
else {
switch (Choose) {
case 1:
rent1 = Aodi.rent;
manned = Aodi.capacity;
shipment = 0;
break;
case 2:
rent1 = Mazida.rent;
manned = Mazida.capacity;
shipment = 0;
break;
case 3:
rent1 = Xue.rent;
manned = Xue.capacity;
shipment = Xue.capacity2;
break;
case 4:
rent1 = Jin.rent;
manned = Jin.capacity;
shipment = 0;
break;
case 5:
rent1 = Song.rent;
manned = 0;
shipment = Song.capacity;
break;
case 6:
rent1 = Yi.rent;
manned = 0;
shipment = Yi.capacity;
break;
}
Allmanned+=manned;
Allshipment+=shipment;
System.out.println("请输入租的天数:");
CarDate=scan.nextInt();
sum=rent1*CarDate;
Allsum+=sum;
System.out.println("当前载人数量为"+Allmanned+"个,载货数量为"+Allshipment+"吨");
System.out.println("您需要付的金额为:"+Allsum);
}
}
}else{
System.out.println("Bye");
System.exit(0);
}
}
//主要觉得冗长的就是main函数部分,原本想把显示可租车辆和选择车辆写成方法来使用,结果发现在另一个方法中创建的对象在main函数中无法使用,也没想到解决的方法,希望有大佬帮忙!