Java汽车租凭项目
模拟完成如图以下所示的汽车租凭项目
代码如下
机动车父类:
package exam;
public abstract class MotoV {
private String brand;
private String carId;
private int prive;
private double totalPrice;
public MotoV() {
super();
// TODO Auto-generated constructor stub
}
public MotoV(String brand, String carId, int prive) {
super();
this.brand = brand;
this.carId = carId;
this.prive = prive;
}
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 getPrive() {
return prive;
}
public void setPrive(int prive) {
this.prive = prive;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public abstract double price(int days);
}
轿车子类:
package exam;
public class Car extends MotoV {
private String type;
public Car() {
super();
// TODO Auto-generated constructor stub
}
public Car(String brand, String carId, int prive,String type) {
super(brand, carId, prive);
this.type = type;
// TODO Auto-generated constructor stub
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public double price(int days) {
double discount=1.0;
if(days>150) {
discount=0.7;
}else if(days>30) {
discount=0.8;
}else if(days>7) {
discount=0.9;
}
double total=getPrive()*discount*days;
return total;
}
}
客车子类:
package exam;
public class Bus extends MotoV {
private int seat;
public Bus() {
super();
// TODO Auto-generated constructor stub
}
public Bus(String brand, String carId, int prive,int seat) {
super(brand, carId, prive);
this.seat = seat;
// TODO Auto-generated constructor stub
}
public int getSeat() {
return seat;
}
public void setSeat(int seat) {
this.seat = seat;
}
@Override
public double price(int days) {
double discount=1.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;
}
double total=getPrive()*discount*days;
return total;
}
}
机动车操作类
package exam;
public class MotoVsys {
private MotoV[] mo= new MotoV[8];
public MotoVsys() {
mo[0]= new Car("宝马","京NY28588",800,"x6");
mo[1]= new Car("宝马","京CNY3284",600,"550i");
mo[2]= new Car("别克","京NT37465",300,"林荫大道");
mo[3]= new Car("别克","京NT96968",600,"GL8");
mo[4]= new Bus("金杯","京6566754",800,16);
mo[5]= new Bus("金龙","京8696997",800,16);
mo[6]= new Bus("金杯","京9696996",1500,32);
mo[7]= new Bus("金龙","京8696998",1500,32);
}
public MotoV leaveoutCar(String brand, String type,int seat) {
for(int i=0;i<mo.length;i++) {
if(mo[i] instanceof Car) {
Car c = (Car)mo[i];
if(c.getBrand().equals(brand) && c.getType().equals(type)) {
return c;
}
}else if(mo[i] instanceof Bus){
Bus b = (Bus)mo[i];
if(b.getBrand().equals(brand) && b.getSeat()==seat) {
return b;
}
}
}
return null;
}
public double totalPrice(MotoV mo,int days) {
if(mo instanceof Car) {
return mo.price(days);
}else if(mo instanceof Bus) {
return mo.price(days);
}
return 0;
}
public String carId(MotoV mo) {
if(mo instanceof Car) {
return mo.getCarId();
}else if(mo instanceof Bus) {
return mo.getCarId();
}
return null;
}
public double allPrice(MotoV[] mo) {
double total=0;
for(int i=0;i<mo.length;i++) {
if(mo[i] instanceof Car) {
total+=mo[i].getTotalPrice();
}else if(mo[i] instanceof Bus) {
total+=mo[i].getTotalPrice();
}
}
return total;
}
}
测试类:
package exam;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MotoVsys mot =new MotoVsys();
System.out.println("***********租车系统****************");
System.out.println("请输入租车几次");
int num = sc.nextInt();
MotoV[] mt = new MotoV[num];
double total = 0;
for(int i=0;i<num;i++) {
System.out.println("请输入要租的车的类型:1、轿车 2、客车");
int num1 = sc.nextInt();
String brand = null;
String type = null;
int seat =0;
switch (num1) {
case 1:
System.out.println("请输入要租的车的品牌: 1、宝马 2、别克");
int num2=sc.nextInt();
brand = num2==1?"宝马":"别克";
switch(num2) {
case 1:
System.out.println("请输入要租的车的型号: 1、X6 2、550i");
int num3=sc.nextInt();
type = num3==1?"X6":"550i";
break;
case 2:
System.out.println("请输入要租的车的型号: 1、林荫大道 2、GL8");
int num4=sc.nextInt();
type = num4==1?"林荫大道":"GL8";
break;
}
break;
case 2:
System.out.println("请输入要租的车的品牌: 1、金杯 2、金龙");
int num5=sc.nextInt();
brand = num5==1?"金杯":"金龙";
System.out.println("请选择座位: 1、16座 2、32");
int num6=sc.nextInt();
seat = num6==1?16:32;
break;
}
System.out.println("请输入要租的天数: ");
int days = sc.nextInt();
MotoV mo=mot.leaveoutCar(brand, type, seat);
mt[i]=mo;
System.out.println("车牌号为: "+mot.carId(mt[i]));
mo.setTotalPrice(mot.totalPrice(mo, days));
System.out.println("租金为: "+mot.totalPrice(mo, days));
System.out.println();
}
System.out.println("总租金为: "+mot.allPrice(mt));
}
}