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<0){
System.out.println("你输入有误,请重新输入");
num = sc.nextInt();
}
double rentSum = 0;
int mannedSum = 0;
double burdenSum = 0;
for(int j =1 ;j<=num ;j++){
System.out.println("请选择第"+j+"辆车");
sc = new Scanner(System.in);
int num2 = sc.nextInt();
while(num2<=0&&num2>types.length){
System.out.println("你输入的有误!!请重新输入");
num2 = sc.nextInt();
}
if(types[num2-1] instanceof MannedCar){
System.out.println("你选择了" + types[num2 - 1].getName()
+ "\t" + "租金为每天" + types[num2 - 1].getRent() + "元"
+ "\t" + "载客量为" + types[num2 - 1].getBurden());
}
else if(types[num2-1] instanceof BurdenCar){
System.out.println("您选择了" + types[num2 - 1].getName()
+ "\t" + "租金为每天" + types[num2 - 1].getRent() + "元"
+ "\t" + "载客量为" + types[num2 - 1].getManned()
+ "\t" + "载货量为" + types[num2 - 1].getBurden());
}
else{
System.out.println("您选择了" + types[num2 - 1].getName()
+ "\t" + "租金为每天" + types[num2 - 1].getRent() + "元"
+ "\t" + "载客量为" + types[num2 - 1].getManned()
+ "\t" + "载货量为" + types[num2 - 1].getBurden());
}
rentSum = rentSum+types[num2-1].getRent();
mannedSum = mannedSum+types[num2-1].getManned();
burdenSum =burdenSum+types[num2-1].getBurden();
}
System.out.println("你的账单是:");
System.out.println("*****总载客量为:"+mannedSum+"人");
System.out.println("*****总载货量为:"+burdenSum+"吨");
System.out.println("*****总共需要:"+rentSum+"元");
}
else if(a==0){
System.out.println("谢谢你的使用!系统将自动退出");
}else{
System.out.println("你输入的有误!系统将自动退出");
}
}
}
--------------
输出结果:
欢迎来到涛涛租车系统:
你是否要租车呀?1是,0否
1
你可以租车的类型及价目表
序号 汽车名称 租金 容量
0 null 0 载人0
0 null 0 载人0
0 null 0 载人0
0 null 0 载人0
0 null 0 载人0
请输入你要租车的数量:
1
请选择第1辆车
1
你选择了null 租金为每天0元 载客量为0
你的账单是:
*****总载客量为:1人
*****总载货量为:0.0吨
*****总共需要:0.0元
MengFly
Bobzhangxiaotao
相关分类