package com.xml.rentcar;//父类public class Car {private int num;//数量private String name;//车名private int rent;//租金private int manned;//载货private int burden;//容量public Car(int num, String name, int rent, int manned, int burden) { this.num = num; this.name = name; this.rent = rent; this.manned = manned; this.burden = burden;}public Car(){ }public int getNum() { return num;}public void setNum(int num) { this.num = num;}public String getName() { return name;}public void setName(String name) { this.name = name;}public int getRent() { return rent;}public void setRent(int rent) { this.rent = rent;}public int getManned() { return manned;}public void setManned(int manned) { this.manned = manned;}public int getBurden() { return burden;}public void setBurden(int burden) { this.burden = burden;}}-----------------------
package com.xml.rentcar;
/*
* 载人的子类
*/
public class MannedCar extends Car {
private int manned;
public int getManned() {
return manned;
}
public void setManned(int manned) {
this.manned = manned;
}
public MannedCar(int manned,String name,int rent,int num) {
this.manned = manned;
}
}
-----------------------
package com.xml.rentcar;
/*
* 载货的子类
*/
public class BurdenCar extends Car {
private int burden;
public int getBurden() {
return burden;
}
public void setBurden(int burden) {
this.burden = burden;
}
public BurdenCar(int num,String name,int rent,int burden) {
this.burden = burden;
}
}
-------------
package com.xml.rentcar;
/*
* 既有载人也有载货的子类
*/
public class BothCar extends Car {
private int manned;
private int burden;
public int getManned() {
return manned;
}
public void setManned(int manned) {
this.manned = manned;
}
public int getBurden() {
return burden;
}
public void setBurden(int burden) {
this.burden = burden;
}
public BothCar(int num,String name,int rent,int manned, int burden) {
this.manned = manned;
this.burden = burden;
}
}
----------------
package com.xml.rentcar;
import java.util.Scanner;
public class Main {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("欢迎来到涛涛租车系统:");
System.out.println("你是否要租车呀?1是,0否");
sc = new Scanner(System.in);
int a = sc.nextInt();
if(a==1){
System.out.println("你可以租车的类型及价目表");
System.out.println("序号 汽车名称 租金 容量");
Car[] types = { new MannedCar(1, "奥迪A4", 500, 4),
new MannedCar(2, "马自达", 400, 4),
new MannedCar(3, "上海大众", 600, 3),
new MannedCar(4, "吉利自由舰", 200, 5),
new MannedCar(5, "凯迪拉克", 600, 4) };
for (Car car : types) {
if(car instanceof MannedCar){
System.out.println(car.getNum()+"\t"+car.getName()+"\t"+car.getRent()+"\t"+"载人"+car.getBurden());
}
else if(car instanceof BurdenCar){
System.out.println(car.getNum()+"\t"+car.getName()+"\t"+car.getRent()+"\t"+"载货"+car.getBurden());
}
else {
System.out.println(car.getNum()+"\t"+car.getName()+"\t"+car.getRent()+"\t"+"载人:"+car.getManned()+"\t"+"载货"+car.getBurden());
}
}
System.out.println("请输入你要租车的数量:");
sc = new Scanner(System.in);
int num = sc.nextInt();
while(num |