汽车租赁系统。
设计思路:分别去写一个汽车父类,三个子类分别是卡车类,公共汽车类,小汽车类。一个汽车管理类,一个测试类来实现
父类:
public abstract class MotoVehicle {
private String number;//车牌号
private String brand;//车型
private int dayPrice;//日租金
public MotoVehicle(){}
public MotoVehicle(String number,String brand,int dayPrice){
this.number = number;
this.brand = brand;
this.dayPrice = dayPrice;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getDayPrice() {
return dayPrice;
}
public void setDayPrice(int dayPrice) {
this.dayPrice = dayPrice;
}
public abstract double sumPrice(int day);
}
小汽车类:
public class Car1 extends MotoVehicle {
private String type;
public Car1(){}
public Car1(String number,String brand,int dayPrice,String type){
super(number,brand,dayPrice);
this.type = type;
}
public void setType(String type){
this.type = type;
}
public String getType(){
return type;
}
public double sumPrice(int day) {
double price = this.getDayPrice()*day;
if(day>=7 && day<30){
price *= 0.9;
}else if(day>=30 && day<150){
price *= 0.8;
}
else if(day>=150){
price *= 0.7;
}
return price;
}
}
公共汽车类:
public class Bus extends MotoVehicle{
private int seatCount;
public Bus(){}
public Bus(String number,String brand,int dayPrice,int seatCount){
super(number,brand,dayPrice);
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
public double sumPrice(int day) {
double price = this.getDayPrice()*day;
if(day>=3 && day<7){
price *= 0.9;
}else if(day>=7 && day<30){
price *= 0.8;
}
else if(day>=30 && day<150){
price *= 0.7;
}
else if(day>=150){
price *= 0.6;
}
return price;
}
}
大卡车:
public class BigCar extends MotoVehicle {
private int dun;
public BigCar(){}
public BigCar(String number,String brand,int dayPrice,int dun){
super(number,brand,dayPrice);
this.dun = dun;
}
public int getDun() {
return dun;
}
public void setDun(int dun) {
this.dun = dun;
}
public double sumPrice(int day) {
double price = this.getDun()*this.getDayPrice()*day;
return price;
}
}
汽车管理类:
public class MotoManager {
MotoVehicle[] motos = new MotoVehicle[12];
public void init(){
//初始化汽车
motos[0] = new Car1("京NY54689","宝马",800,"X6");
motos[1] = new Car1("京NT85258","宝马",600,"500i");
motos[2] = new Car1("京NS12356","别克",300,"林荫大道");
motos[3] = new Car1("京NC96874","别克",600,"别克GL8");
motos[4] = new Bus("京CT56566","金杯",800,16);
motos[5] = new Bus("京CE96542","金龙",800,16);
motos[6] = new Bus("京CQ41236","金杯",1500,34);
motos[7] = new Bus("京CY12345","金龙",1500,34);
motos[8] = new BigCar("京CJ56235","中型卡车",50,10);
motos[9] = new BigCar("京CY85236","大型卡车",50,50);
motos[10] = new BigCar("京CY35268","中型卡车",50,20);
motos[11] = new BigCar("京CY88888","大型卡车",50,100);
}
//租车代码,所以要使用汽车类作为返回值,返回汽车的类型
//下面代码就是返回给用户一个租车的类型
public MotoVehicle zuChe(String brand,String type,int seat,int dun){
MotoVehicle moto = null;
for(MotoVehicle m : motos){
if(m instanceof Car1){
Car1 car = (Car1)m;
if(car.getBrand().equals(brand) && car.getType().equals(type)){
moto = car;
break;
}
}else if(m instanceof Bus){
Bus bus = (Bus)m;
if(bus.getBrand().equals(brand) && bus.getSeatCount() == seat){
moto = bus;
break;
}
else if(m instanceof BigCar){
BigCar bcar = (BigCar)m;
if(bcar.getBrand().equals(brand) && bcar.getDun()==dun){
moto = bcar;
break;
}
}
}
}
return moto;
}
}
最后一个测试类:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MotoManager mtmg = new MotoManager();
mtmg.init();
System.out.println("**************欢迎使用租车公司系统****************");
String brand = null;
String type = "";
int seat = 0;
int dun=0;
String str = null;
double sumMoney = 0;
double money =0;
do{
System.out.println("请选择您要租车的类型(1、轿车 2、客车 3、卡车):");
int n = input.nextInt();
// String brand = null;
// String type = "";
// int seat = 0;
// int dun=0;
switch(n){
case 1:{
seat = 0;
dun = 0;
System.out.println("请选择您想要租的类型(1、宝马 2、别克):");
int m = input.nextInt();
if(m == 1){
brand = "宝马";
System.out.println("请选择您想要租的车型号(1、X6 2、500i):");
type = (input.nextInt() == 1)?"X6":"500i";
}
else if(m == 2){
brand = "别克";
System.out.println("请选择您想要租的车型号(1、林荫大道 2、别克GL8):");
type = (input.nextInt() == 1)?"林荫大道":"别克GL8";
}
break;
}
case 2:{
type = "";
dun = 0;
System.out.println("请选择您想要租的类型(1、金杯 2、金龙):");
int m1 = input.nextInt();
if(m1 == 1){
brand = "金杯";
System.out.println("请选择车的座位数(1、16 2、34):");
seat = (input.nextInt()==1)?16:34;
}
else if(m1 == 2){
brand = "金龙";
System.out.println("请选择车的座位数(1、16 2、34):");
seat = (input.nextInt()==1)?16:34;
}
break;
}
case 3:{
type = "";
seat = 0;
System.out.println("请选择您想要租的卡车类型(1、中型卡车 2、大型卡车):");
int m2 = input.nextInt();
if(m2 == 1){
brand = "中型卡车";
System.out.println("请选择车的吨位数(1、10 2、20):");
dun = (input.nextInt()==1)?10:20;
}else if(m2 == 2){
brand = "大型卡车";
System.out.println("请选择车的吨位数(1、50 2、100):");
dun = (input.nextInt()==1)?50:100;
}
break;
}
default:{
System.out.println("输入有误!请按照提示正确输入:");
n = input.nextInt();
}
}
MotoVehicle motos = mtmg.zuChe(brand,type,seat,dun);
System.out.println("请输入您要租赁的天数:");
int day = input.nextInt();
money = motos.sumPrice(day);
System.out.println("租车成功,您租的是"+motos.getBrand()+",请按照车牌号"+motos.getNumber()+"提车。");
System.out.println("您本次需要支付租金:"+money);
sumMoney +=money;
System.out.print("是否继续租车(y/n):");
str = input.next();
}while(str.equals("y"));
System.out.println("本次租车总共"+sumMoney+"元");
System.out.println("谢谢您的使用!!");
}