java基于面向对象思想的模拟汽车租赁系统

一.需求分析
汽车租赁系统用户需要完成租赁汽车的操作,并将租金结算。
二.总体设计
汽车租赁公司内有两种车,分别是轿车和客车,所以需要设计两个类,轿车类和客车类,这两个类都继承于共同的父类,汽车类。
在这里插入图片描述
由上图可知,轿车和客车公有的属性是品牌,日租金,车牌号,所以这些属性可以定义在父类中,公有的方法为计算租金,因为折扣不同,需要在两个子类分别具体实现,所以在父类中就定义为抽象类,所以父类就要定义为抽象类。
在具体实现的时候,我们还要对汽车实行初始化,所以需要定义一个汽车操作类,汽车操作类需用一个数组 存储汽车信息,然后再用工厂方法,初始化具体的汽车类型。
最后还需要一个入口,定义一个类似于平常编代码中Test类的类。
代码实现:
Vehicle类
package cn.com.cn.com.vehicle2;

public abstract class Vehicle {
private String carID;
private String brand;
private double perRent;

public String getCarID() {
    return carID;
}

public void setCarID(String carID) {
    this.carID = carID;
}

public String getBrand() {
    return brand;
}

public double getPerRent() {
    return perRent;
}

public void setPerRent(double perRent) {
    this.perRent = perRent;
}

public void setBrand(String brand) {
    this.brand = brand;
}

public Vehicle(String carID, String brand, double perRent) {
    this.carID = carID;
    this.brand = brand;
    this.perRent = perRent;
}

public  abstract  double countRentMoney(int days);

}
Car类
public class Car extends Vehicle{
private String type;

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Car(String carID, String brand, double perRent, String type) {
    super(carID, brand, perRent);
    this.type = type;
}

@Override
public double countRentMoney(int days) {
   double price=this.getPerRent()*days;
    if(days>7){
        price*=0.9;
    }else  if(days>30){
        price*=0.8;
    }else if(days>150){
        price*=0.7;
    }
    return price;
}

}
Bus类
package cn.com.cn.com.vehicle2;

public class Bus extends Vehicle{
private int seatCount;

public int getSeatCount() {
    return seatCount;
}

public void setSeatCount(int seatCount) {
    this.seatCount = seatCount;
}

public Bus(String carID, String brand, double perRent, int seatCount) {
    super(carID, brand, perRent);
    this.seatCount = seatCount;
}

@Override
public double countRentMoney(int days) {
   double price=this.getPerRent()*days;
    if(days>=3){
        price*=0.9;
    }else  if(days>=7){
        price*=0.8;
    }else if(days>=30){
        price*=0.7;
    }else if(days>=150){
        price*=0.6;
    }
    return price;
}

}
VehicleOperation类
package cn.com.cn.com.manage2;

import cn.com.cn.com.vehicle2.Bus;
import cn.com.cn.com.vehicle2.Car;
import cn.com.cn.com.vehicle2.Vehicle;

public class VehicleOperation {
Vehicle vehicles[] = new Vehicle[8];
//把车辆进行初始化
public void init() {
vehicles[0] = new Car(“京NY28588”, “宝马”, 800, “X60”);
vehicles[1] = new Car(“京CNY3284”, “宝马”, 600, “550i”);
vehicles[2] = new Car(“京NT37465”, “别克”, 300, “林荫大道”);
vehicles[3] = new Car(“京NT96968”, “别克”, 600, “GL8”);
vehicles[4] = new Bus(“京6566754”, “金杯”, 800, 16);
vehicles[5] = new Bus(“京86969974”, “金龙”, 800, 16);
vehicles[6] = new Bus(“京9696996”, “金杯”, 1500, 34);
vehicles[7] = new Bus(“京8696998”, “金龙”, 1500, 34);

}
//租车操作
public Vehicle rentVehicle(String brand,int seatCounts,String type ){
    Vehicle v=null;
    for (Vehicle vehicle:vehicles) {
        if(vehicle instanceof  Car){
            Car car=(Car)vehicle;
            if(car.getBrand().equals(brand)&&car.getType().equals(type)){
                v=car;
                break;
            }
        }else if(vehicle instanceof Bus ){
            Bus bus=(Bus)vehicle;
            if(bus.getBrand().equals(brand)&&bus.getSeatCount()==seatCounts)
            {
                v=bus;
                break;
            }
        }
    }
    return  v;
}

}
VehicleRent类
package cn.com.cn.com.manage2;

import cn.com.cn.com.vehicle2.Vehicle;

import java.util.Scanner;

public class VehicleRent {

public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    VehicleOperation vehicleOperation=new VehicleOperation();
    System.out.println("**************欢迎来到马栏山租车公司****************");
    System.out.println("请选择您要租的车型:1.汽车 2.客车");
    int typecase=input.nextInt();
   String brand="";
   String type="";
   int seatCount=0;
   int choose=0;
    switch (typecase){

        case  1:
            System.out.println("请选择您要的品牌:1.宝马,2.别克");
            choose=input.nextInt();
            if(choose==1){
                brand="宝马";
                System.out.println("请选择您想要的型号:1.X6 2.550i");
                type=(input.nextInt()==1)?"X3":"550i";
            }else if (choose==2){
                brand="别克";
                System.out.println("请选择您想要的型号:1.GL8 2.林荫大道");
                type=(input.nextInt()==1)?"GL8":"林荫大道";
            }
            break;
        case 2:
            System.out.println("请选择您要的品牌:1.金杯,2.金龙");
            choose=input.nextInt();
            if(choose==1){
                brand="金杯";
                System.out.println("请选择您想要的座位数:1.16 2.34");
                seatCount=(input.nextInt()==1)?16:34;
            }else if (choose==2){
                brand="金龙";
                System.out.println("请选择您想要的座位数:1.16 2.34");
                seatCount=(input.nextInt()==1)?16:34;
            }
            break;
             default:
                System.out.println("对不起,我们暂时只有这几种车辆");
                break;
    }

   vehicleOperation.init();
    Vehicle v=vehicleOperation.rentVehicle(brand,seatCount,type);
    System.out.print("请输入您需要租赁的天数:");
    int days=input.nextInt();
    double price=v.countRentMoney(days);

    System.out.println("分配给您的车牌号为"+v.getCarID());
    System.out.println("您租车所需的费用为" + price);
}

}
运行演示:
在这里插入图片描述
关于面向对象的思考:
在进行基于面向对象思想的编程时,需求描述中的名词,就是属性,动词就是方法,还要充分利用继承,封装,多态的思想,是代码更加简洁,更加易于修改,增强了代码的可读性。

发布了35 篇原创文章 · 获赞 3 · 访问量 546

猜你喜欢

转载自blog.csdn.net/biaogegegege1/article/details/104997839