多态情景模拟—租车系统
- 我有一个车类
public class Vehicle {
private String id;
private String brand;
private int perRent;
public Vehicle(){
super();
}
public Vehicle(String id, String brand, int perRent) {
super();
this.id = id;
this.brand = brand;
this.perRent = perRent;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPerRent() {
return perRent;
}
public void setPerRent(int perRent) {
this.perRent = perRent;
}
public double calRent(int days){
return 0.0;
}
public void show(){
System.out.println(getId());
}
}
- 我有一个Car类,继承Vehicle,和Bus类区别在type
public class Car extends Vehicle {
private String type;
public Car() {
super();
}
public Car(String id, String brand, int perRent, String type) {
super(id, brand, perRent);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public double calRent(int days) {
double price = getPerRent() * days;
if (days >= 3 && days <= 7) {
price *= 0.9;
} else if (days > 7 && days <= 30) {
price *= 0.8;
} else if (days > 30 && days <= 90) {
price *= 0.7;
} else if (days > 90) {
price *= 0.66;
}
return price;
}
@Override
public void show() {
System.out.println("车牌号:" + this.getId() + ",车品牌:" + this.getBrand() + ",车日租价:" + this.getPerRent() + ",车型号:"
+ this.getType());
}
}
- 我有一个Bus类,继承Vehicle,和Car类区别在seatCount
public class Bus extends Vehicle {
private int seatCount;
public Bus() {
super();
}
public Bus(String id, String brand, int perRent, int seatCount) {
super(id, brand, perRent);
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
@Override
public double calRent(int days) {
double price = getPerRent() * days;
if (days >= 3 && days <= 7) {
price *= 0.9;
} else if (days > 7 && days <= 30) {
price *= 0.8;
} else if (days > 30 && days <= 90) {
price *= 0.7;
} else if (days > 90) {
price *= 0.66;
}
return price;
}
@Override
public void show() {
System.out.println("车牌号:" + this.getId() + ",车品牌:" + this.getBrand() + ",车日租价:" + this.getPerRent() + ",车座位数:"
+ this.getSeatCount());
}
}
- 我有一个Opeartion操作系统类,有一个Vehicle数组,存放Car和Bus,有一个方法提供查找车类并返回,还有一个方法根据返回的车类调用不同子类的重写的方法
import java.util.Scanner;
public class Operation {
Vehicle[] vc = new Vehicle[6];
Scanner sc=new Scanner(System.in);
public void init() {
String id = "湘D 520S";
for (int a = 0; a < vc.length; a++) {
switch (a) {
case 0:
vc[a] = new Car(id + a, "大众", 200, "新迈腾");
break;
case 1:
vc[a] = new Car(id + a, "大众", 100, "新桑塔纳");
break;
case 2:
vc[a] = new Car(id + a, "路虎", 1000, "揽胜");
break;
case 3:
vc[a] = new Bus(id + a, "金杯", 500, 10);
break;
case 4:
vc[a] = new Bus(id + a, "金杯", 600, 13);
break;
case 5:
vc[a] = new Bus(id + a, "宇通", 1300, 28);
break;
}
}
}
public Vehicle rent(String brand, String type, int seatCount) {
Vehicle ve = null;
for (int a = 0; a < vc.length; a++) {
ve = vc[a];
if (ve instanceof Car) {
Car c = (Car) ve;
if (brand.equals(c.getBrand()) && type.equals(c.getType())) {
ve = c;
break;
}
} else if (ve instanceof Bus) {
Bus b = (Bus) ve;
if (brand.equals(b.getBrand()) && seatCount == b.getSeatCount()) {
ve = b;
break;
} else {
ve = new Vehicle("暂无此车辆...","",0);
}
}
}
return ve;
}
public void rent() {
for (int a = 0; a < vc.length; a++) {
Vehicle ve = vc[a];
if (ve instanceof Car) {
Car car = (Car) ve;
car.show();
} else if (ve instanceof Bus) {
Bus bus = (Bus) ve;
bus.show();
}
}
}
public void select() {
System.out.println("请选择车类型");
System.out.println("1.轿车\t2.客车");
int carTypes = sc.nextInt();
String brand = "";
String type = "";
int seatCount = 0;
int carType = 0;
int carBrand = 0;
if (carTypes == 1) {
System.out.println("请选择品牌");
System.out.println("1.大众\t2.路虎");
carBrand = sc.nextInt();
if (carBrand == 1) {
brand = "大众";
System.out.println("请选择车型");
System.out.println("1.新迈腾 \t2.新桑塔纳\t3.宝来");
carType = sc.nextInt();
if (carType == 1) {
type = "新迈腾";
} else if (carType == 2) {
type = "新桑塔纳";
} else {
System.out.println("暂无此车型...");
}
} else if (carBrand == 2) {
brand = "路虎";
type = "揽胜";
} else {
System.out.println("暂无此品牌...");
}
} else if (carTypes == 2) {
System.out.println("请选择品牌");
System.out.println("1.金杯\t2.宇通");
carBrand = sc.nextInt();
if (carBrand == 1) {
brand = "金杯";
System.out.println("请选择座位数");
System.out.println("1.10\t2.13");
carType = sc.nextInt();
if (carType == 1) {
seatCount = 10;
} else if (carType == 2) {
seatCount = 13;
} else {
System.out.println("暂无此车型...");
}
} else if (carBrand == 2) {
brand = "宇通";
seatCount = 28;
} else {
System.out.println("暂无此品牌...");
}
} else {
System.out.println("暂无此车类型...");
}
Vehicle v = rent(brand, type, seatCount);
System.out.println("您选择的是:");
v.show();
System.out.println("请输入需要租赁的天数:");
int days=sc.nextInt();
double price=v.calRent(days);
System.out.println("您所需要支付的价格:"+price);
}
}
- 我有一个rent用户界面
public class Rent {
public static void main(String[] args) {
Operation op = new Operation();
op.init();
System.out.println("**************欢迎光临T-J租车公司**************");
System.out.println("请选择功能:");
int a = 0;
do {
System.out.println("1.展示所有车辆信息\t2.选择车辆类型");
a = sc.nextInt();
if (a == 1) {
op.rent();
} else if (a == 2) {
op.select();
} else {
System.out.println("暂无此功能,请重新选择...");
}
} while (a != 2);
}
}