求教
//汽车父类,设置成了抽象类
public abstract class Qiche {
private int num;
private int dun;
private int ren;
private String name;
private double price;
public abstract void test();
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getDun() {
return dun;
}
public void setDun(int dun) {
this.dun = dun;
}
public int getRen() {
return ren;
}
public void setRen(int ren) {
this.ren = ren;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
//载人汽车类
public class Car extends Qiche {
public Car(int num,String name,int ren,double price) {
setNum(num);
setRen(ren);
setName(name);
setPrice(price);
}
@Override
public void test() {
System.out.println(getNum()+". "+getName()+" "+getRen()+"人 "+getPrice()+"\天");
}
}
//载重汽车类
public class Huoche extends Qiche {
public Huoche(int num,String name,int dun,double price) {
setNum(num);
setDun(dun);
setName(name);
setPrice(price);
}
@Override
public void test() {
System.out.println(getNum()+". "+getName()+" "+getDun()+"吨 "+getPrice()+"\天");
}
}
//皮卡类
public class Pika extends Qiche {
public Pika(int num,String name,int ren,int dun,double price) {
setNum(num);
setRen(ren);
setDun(dun);
setName(name);
setPrice(price);
}
@Override
public void test() {
System.out.println(getNum()+". "+getName()+" "+getRen()+"人\"+getDun()+"吨"+" "+getPrice()+"\天");
}
}
测试类
import java.util.Scanner;
public class Ceshi {
Scanner console = new Scanner(System.in);
Qiche[] qcs = {new Car(1,"奔驰",5,200),new Car(2,"大巴",40,500),new Huoche(3,"三轮",2,100),
new Huoche(4,"重卡",20,1000),new Pika(5,"皮卡",4,2,800)};
public void neirong() {
System.out.println("欢迎使用达达租车系统!");
System.out.println("是否要使用达达租车系统 (1:是;0:否):");
int xuanze = console.nextInt();
if(xuanze==1) {
System.out.println("可供选择的车型、载客载重以及价格表:");
System.out.println("序号"+" "+"名称 "+"载人\载重"+"价格");
for (Qiche qiche : qcs) {
qiche.test();
}
System.out.println("请选择您的租车数量:");
int zucheNum = console.nextInt();
Qiche[] qcs2 = new Qiche[zucheNum];
for(int i = 1;i<=zucheNum;i++) {
System.out.println("请输入您要租的第"+i+"辆车的序号:");
int xuhao = console.nextInt();
qcs2[i-1]=qcs[xuhao-1];
}
System.out.println("请输入您的租车天数:");
int days = console.nextInt();
System.out.println("您的账单:");
int zairen = 0;
int zaizhong = 0;
double prices = 0;
System.out.print("**可载人的车有:");
for (Qiche qiche : qcs2) {
if(qiche instanceof Car || qiche instanceof Pika) {
zairen += qiche.getRen();
prices += qiche.getPrice();
System.out.print(qiche.getName()+"");
}
}
System.out.print("可载人"+zairen+"人"+"
");
System.out.print("**可载重的车有:");
for (Qiche qiche : qcs2) {
if(qiche instanceof Huoche || qiche instanceof Pika){
zaizhong += qiche.getDun();
prices += qiche.getPrice();
System.out.print(qiche.getName()+"");
}
}
System.out.print("可载重"+zaizhong+"吨"+"
");
System.out.println("**租车总价格:"+prices*days+"元");
System.out.println("感谢您的使用。再见!");
} else {
System.out.println("感谢您的使用。再见!");
}
}
public static void main(String[] args) {
Ceshi cs = new Ceshi();
cs.neirong();
}
}