提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


文章目录

  • 前言
  • 一、汽车租赁系统
  • 1.汽车类 抽象父类
  • 2.客车类 子类
  • 3.轿车类 子类
  • 4.业务类
  • 5.功能测试类(运行)
  • 总结



前言

使用数组,面向对象的知识实现一个汽车租赁系统
制作属于你自己的秋名山租赁公司!!!

1.汽车租赁信息表如下

gui java租车 租车系统java_对象数组

2.类和属性

gui java租车 租车系统java_子类_02


一、汽车租赁系统

1.汽车类 抽象父类

package week_test;

public abstract class Car {
    private String carNumber;//车牌号
    private String brand;//品牌
    private int money;//租金

    public Car() {
    }

    public Car(String brand) {
        this.brand = brand;
    }

    public Car(String carNumber, String brand, int money) {
        this.carNumber = carNumber;
        this.brand = brand;
        this.money = money;
    }

    public String getCarNumber() {
        return carNumber;
    }

    public void setCarNumber(String carNumber) {
        this.carNumber = carNumber;
    }

    public String getBrand() {
        return brand;
    }

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

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    //租赁费用
    public abstract double allMoney(int days,double dayMoney);
}

2.客车类 子类

package week_test;

public class PassengerCar extends Car{
    private String seating;//座位数

    public PassengerCar() {
    }

    public PassengerCar(String brand, String seating) {
        super(brand);
        this.seating = seating;
    }

    public PassengerCar(String carNumber, String brand, int money, String seating) {
        super(carNumber, brand, money);
        this.seating = seating;
    }

    public String getSeating() {
        return seating;
    }

    public void setSeating(String seating) {
        this.seating = seating;
    }

    //重写租赁费用
    @Override
    public double allMoney(int days, double dayMoney) {
        if (days>=150){
            return days*dayMoney*0.6;
        }else if (days>=30){
            return days*dayMoney*0.7;
        }else if (days>=7){
            return days*dayMoney*0.8;
        }else if (days>=3){
            return days*dayMoney*0.9;
        }else
            return days*dayMoney;
    }
}

3.轿车类 子类

package week_test;

public class SaloonCar extends Car{
    private String type;//型号

    public SaloonCar() {
    }

    public SaloonCar(String brand, String type) {
        super(brand);
        this.type = type;
    }

    public SaloonCar(String carNumber, String brand, int money, String type) {
        super(carNumber, brand, money);
        this.type = type;
    }

    public String getType() {
        return type;
    }

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

    //重写租赁费用
    @Override
    public double allMoney(int days, double dayMoney) {
        if (days>150){
            return days*dayMoney*0.7;
        }else if (days>30){
            return days*dayMoney*0.8;
        }else if (days>7){
            return days*dayMoney*0.9;
        }else
            return days*dayMoney;
    }
}

4.业务类

package week_test;

public class CarBusiness {
        //筛选车辆
    public static Car searchCar(Car car,Car[] cars){
        //遍历cars对象数组
        for (Car c : cars) {
            if (c instanceof SaloonCar) {
                //将客户选择的品牌和型号做比较
                if (c.getBrand().equals(car.getBrand())&&((SaloonCar) c).getType().equals(((SaloonCar) car).getType())){
                  //相同 则返回 对应的对象 方便后续输出车牌号
                    return c;
                }
            }
            if (c instanceof PassengerCar){
                if (c.getBrand().equals(car.getBrand())&&((PassengerCar) c).getSeating().equals(((PassengerCar) car).getSeating())){
                    return c;
                }
            }
        }
        return null;
    }
}

5.功能测试类(运行)

package week_test;

import java.util.Scanner;

import static week_test.CarBusiness.searchCar;//导入静态方法searchCar

public class TestCar {
    public static void main(String[] args) {
        //将汽车信息装入对象数组cars
        SaloonCar sc1 = new SaloonCar("京NY28588","宝马",800,"X6");
        SaloonCar sc2 = new SaloonCar("京CNY3284","宝马",600,"550i");
        SaloonCar sc3 = new SaloonCar("京NT37465","别克",300,"林荫大道");
        SaloonCar sc4 = new SaloonCar("京NT96968","别克",600,"GL8");
        PassengerCar pc1 = new PassengerCar("京6566754","金杯",800,"16座");
        PassengerCar pc2 = new PassengerCar("京8696997","金龙",800,"16座");
        PassengerCar pc3 = new PassengerCar("京9696996","金杯",1500,"34座");
        PassengerCar pc4 = new PassengerCar("京8696998","金龙",1500,"34座");
        Car [] cars ={sc1,sc2,sc3,sc4,pc1,pc2,pc3,pc4};
        //测试功能
        Scanner sc = new Scanner(System.in);
        System.out.println("***********欢迎光临秋名山守望者汽车租赁公司***********");
        String brand=null;//品牌
        String type=null;//型号
        Car car2=null;//创建父类对象,接受品牌和型号
        //租赁的汽车类型
        System.out.println("1、轿车  2、客车");
        System.out.println("请选择你要租赁的汽车类型:");
        int i = sc.nextInt();
        if (i==1){
            //租赁的汽车品牌
            System.out.println("请选择你要租赁的汽车品牌: 1、别克 2、宝马");
            brand=sc.nextInt()==1?"别克":"宝马";
            if (brand.equals("别克")){
                //租赁的汽车类型
                System.out.println("请选择你要租赁的汽车类型: 1、林荫大道 2、GL8");
                type=sc.nextInt()==1?"林荫大道":"GL8";
            }else {
                //租赁的汽车类型
                System.out.println("请选择你要租赁的汽车类型: 1、X6 2、550i");
                type=sc.nextInt()==1?"X6":"550i";
            }
            car2 = new SaloonCar(brand,type);
        }
        if (i==2){
            //租赁的汽车品牌
            System.out.println("请选择你要租赁的汽车品牌: 1、金龙 2、金杯");
            brand=sc.nextInt()==1?"金龙":"金杯";
            //租赁的汽车座位数
            System.out.println("请选择你要租赁的汽车座位数: 1、16座 2、34座");
            type=sc.nextInt()==1?"16座":"34座";
            car2 = new PassengerCar(brand,type);
        }
        Car car1;//接受searchCar传过来的对象
        car1=searchCar(car2,cars);
        //租赁的天数
        System.out.println("请输入你要租赁的天数: ");
        int i1 = sc.nextInt();
        //分配给您的汽车牌号
        System.out.println("分配给您的汽车牌号是: "+car1.getCarNumber());
        //支付的费用
        System.out.println("您需要支付的租赁费用是: "+car1.allMoney(i1,car1.getMoney())+"元。");
    }
}

轿车运行截图如下:

gui java租车 租车系统java_对象数组_03


客车运行截图如下:

gui java租车 租车系统java_对象数组_04

总结

难点:获取客户输入的汽车品牌和类型(座位数),遍历对象数组,if语句判断是否相等,输出你想要的对象,获取到绑定的车牌号。

详解:
brand和type用条件(三元)运算符讲客户输入的int类型的值转化为字符串
car2 向上转型存入brand和type接收到的客户输入的值

//部分代码片段
  brand=sc.nextInt()==1?"别克":"宝马";
  type=sc.nextInt()==1?"林荫大道":"GL8";
  car2 = new SaloonCar(brand,type);
  car1=searchCar(car2,cars);
//部分代码片段
   //筛选车辆
    public static Car searchCar(Car car,Car[] cars){
        //遍历cars对象数组
        for (Car c : cars) {
            if (c instanceof SaloonCar) {
                //将客户选择的品牌和型号做比较
                if (c.getBrand().equals(car.getBrand())&&((SaloonCar) c).getType().equals(((SaloonCar) car).getType())){
                  //相同 则返回 对应的对象 方便后续输出车牌号
                    return c;
                }
            }
            if (c instanceof PassengerCar){
                if (c.getBrand().equals(car.getBrand())&&((PassengerCar) c).getSeating().equals(((PassengerCar) car).getSeating())){
                    return c;
                }
            }
        }
        return null;
    }

希望能与您共进步。