汽车租赁项目
模拟完成如图所示汽车租赁项目
代码如下:
机动车父类:
public abstract class MotoV {
private String brand; //品牌
private String carId; //车牌号
private int price; //日租金
private double totalPrice; //每台车的总租金
public MotoV() {
}
public MotoV(String brand, String carId, int price) {
this.brand = brand;
this.carId = carId;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getCarId() {
return carId;
}
public void setCarId(String carId) {
this.carId = carId;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
//计算一辆车的租金
public abstract double price(int days);
}
轿车子类:
public class Car extends MotoV {
private String type; //轿车型号
public Car() {
super();
}
public Car(String brand, String carId, int price,String type) {
super(brand, carId, price);
this.type =type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//计算轿车租金
@Override
public double price(int days) {
double discount =1.0;
double total =0;
if(days>150){
discount =0.7;
}else if(days>30) {
discount =0.8;
}else if(days>7) {
discount =0.9;
}
total=this.getPrice()*discount*days;
return total;
}
}
客车子类:
public class Bus extends MotoV {
private int seat; //客车座位数
public Bus() {
super();
}
public Bus(String brand, String carId, int price,int seat) {
super(brand, carId, price);
this.seat =seat;
}
public int getSeat() {
return seat;
}
public void setSeat(int seat) {
this.seat = seat;
}
//计算客车租金
@Override
public double price(int days) {
double discount =1.0;
double total =0;
if(days>=150){
discount =0.6;
}else if(days>=30) {
discount =0.7;
}else if(days>=7) {
discount =0.8;
}else if(days>=3) {
discount =0.9;
}
total=this.getPrice()*discount*days;
return total;
}
}
机动车操作类
public class MotoVSys {
MotoV[] mt =new MotoV[8];
//租赁公司买入8辆车
public void buyCar() {
mt[0]=new Car("宝马", "粤B99999", 800, "X6");
mt[1]=new Car("宝马", "粤B88888", 600, "550i");
mt[2]=new Car("别克", "粤B77777", 300, "林荫大道");
mt[3]=new Car("别克", "粤B66666", 600, "GL8");
mt[4]=new Bus("金杯", "粤B44444", 800, 16);
mt[5]=new Bus("金龙", "粤B33333", 800, 16);
mt[6]=new Bus("金杯", "粤B22222", 1500, 34);
mt[7]=new Bus("金龙", "粤B11111", 1500, 34);
}
//根据用户要求找到对应的车出租
public MotoV leaveoutCar(String brand,String type,int seat) {
for(int i=0;i<mt.length;i++) {
if(mt[i] instanceof Car) {
Car c =(Car)mt[i];
if(c.getBrand().equals(brand)&&c.getType().equals(type)) {
return c;
}
}else {
Bus b =(Bus)mt[i];
if(b.getBrand().equals(brand)&&b.getSeat()==seat) {
return b;
}
}
}
return null;
}
//计算出租一辆车的总租金
public double totalPrice(MotoV mt,int days) {
if(mt instanceof Car) {
return ((Car)mt).price(days);
}else if(mt instanceof Bus) {
return ((Bus)mt).price(days);
}
return 0;
}
//计算出租多辆车的总租金
public double allPrice(MotoV[] motos) {
double total =0;
for(int i=0;i<motos.length;i++) {
if(motos[i]!=null) {
if(motos[i] instanceof Car) {
total+=((Car)motos[i]).getTotalPrice();
}else {
total+=((Bus)motos[i]).getTotalPrice();
}
}
}
return total;
}
}
测试类:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
MotoVSys mts =new MotoVSys();
mts.buyCar();
Scanner scan=new Scanner(System.in);
System.out.println("欢迎来到租车公司");
//用来接收用户一次租多辆车的数组
MotoV[] motos=new MotoV[5];
boolean a=true;
int i=0;
for(;i<motos.length;i++) {
System.out.println("请选择要租的类型:1、轿车 2、客车");
int choose =scan.nextInt();
String brand =null;
String type =null;
int seat =0;
switch(choose) {
case 1:
System.out.println("请选择轿车品牌:1、宝马 2、别克");
int choose1 = scan.nextInt();
switch(choose1) {
case 1:
brand ="宝马";
System.out.println("请选择型号:1、X6 2、550i");
type = scan.nextInt() ==1?"X6":"550i";
break;
case 2:
brand ="别克";
System.out.println("请选择型号:1、林荫大道 2、GL8");
type = scan.nextInt() ==1?"林荫大道":"GL8";
break;
}
break;
case 2:
System.out.println("请选择客车品牌:1、金杯 2、金龙");
brand = scan.nextInt()==1?"金杯":"金龙";
System.out.println("请选择座位数:1、16座 2、34座");
seat = scan.nextInt() ==1?16:34;
break;
}
MotoV mt =mts.leaveoutCar(brand, type, seat);
motos[i] =mt;
System.out.println("请输入要租车的天数:");
int days= scan.nextInt();
mt.setTotalPrice(mts.totalPrice(mt, days));
System.out.println("租车成功,车牌号为:"+mt.getCarId());
System.out.println("是否继续租车:1、是 2、否");
int num =scan.nextInt();
if(num ==2) {
a=false;
break;
}
}
if(i>=motos.length) {
System.out.println("已无空车,总租金为"+mts.allPrice(motos));
}else if(!a){
System.out.println("谢谢使用,总租金为:"+mts.allPrice(motos));
}
}
}