Java小项目(三)---汽车租赁系统(面向对象)

一、使用技术

Java SE

二、实现功能

使用数组,面向对象的知识实现一个汽车租赁系统(作者未使用数组)

2.1 汽车租赁信息表如下

在这里插入图片描述

2.2 类和属性

在这里插入图片描述

三、运行效果图

在这里插入图片描述
在这里插入图片描述

package cn.kgc.demo8.zuche;
/**轿车类*/
public class BigCar extends Car {

    private int seatCount; // 座位数

    public BigCar() {
    }

    public BigCar(String brand, String carNo, int dayMoney,int seatCount) {
        super(brand, carNo, dayMoney);
        this.seatCount = seatCount;
    }

    // 计算客车的租金
    @Override
    public double calc(int days) {
        double totalMoney = super.getDayMoney() * days;
        if(days >= 150){
            totalMoney *= 0.6;
        } else if(days >= 30){
            totalMoney *= 0.7;
        } else if(days >= 7){
            totalMoney *= 0.8;
        } else if(days >= 3) {
            totalMoney *= 0.9;
        }
        return totalMoney;
    }

    public int getSeatCount() {
        return seatCount;
    }

    public void setSeatCount(int seatCount) {
        this.seatCount = seatCount;
    }
}
package cn.kgc.demo8.zuche;

/**
 * 汽车父类
 */
public abstract class Car {

    private String brand; // 品牌
    private String carNo; // 车牌
    private int dayMoney; // 日租金

    public Car() {
    }

    public Car(String brand, String carNo, int dayMoney) {
        this.brand = brand;
        this.carNo = carNo;
        this.dayMoney = dayMoney;
    }

    // 抽象方法计算租金
    public abstract double calc(int days);

    public String getBrand() {
        return brand;
    }

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

    public String getCarNo() {
        return carNo;
    }

    public void setCarNo(String carNo) {
        this.carNo = carNo;
    }

    public int getDayMoney() {
        return dayMoney;
    }

    public void setDayMoney(int dayMoney) {
        this.dayMoney = dayMoney;
    }
}
package cn.kgc.demo8.zuche;

/**
 * 客车类继承父类汽车
 */
public class SmallCar extends Car {

    // 特有属性
    private String type; // 型号

    public SmallCar() {
    }

    public SmallCar(String brand, String carNo, int dayMoney, String type) {
        super(brand, carNo, dayMoney);
        this.type = type;
    }

    // 计算小轿车的租金方法
    @Override
    public double calc(int days) {
        double totalMoney = super.getDayMoney() * days;
        if(days>150){
            totalMoney *= 0.7;
        } else if(days > 30){
            totalMoney *= 0.8;
        } else if(days > 7){
            totalMoney *= 0.9;
        }
        return totalMoney;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
package cn.kgc.demo8.zuche;

import java.util.Scanner;

/**
 *  租车的管理类:业务方法
 * */


public class CarManager {

    private Car[] cars;

    public CarManager(){
        // 初始化当前所有车辆信息
        cars = new Car[8];
        // 4辆轿车
        cars[0] = new SmallCar("宝马","京NY28588",800,"X6");
        cars[1] = new SmallCar("宝马","京CNY3284",600,"550i");
        cars[2] = new SmallCar("别克","京NT37465",300,"林荫大道");
        cars[3] = new SmallCar("别克","京NT96968",600,"GL8");
        // 4辆客车
        cars[4] = new BigCar("金杯","京6566754",800,16);
        cars[5] = new BigCar("金龙","京8696997",800,16);
        cars[6] = new BigCar("金杯","京9696996",1500,34);
        cars[7] = new BigCar("金龙","京8696998",1500,34);
    }

    // 启动方法
    public void start(){
        Scanner input = new Scanner(System.in);
        System.out.println("***********欢迎光临腾飞汽车租赁公司***********");
        System.out.println("1、轿车\t2、客车");
        System.out.print("请选择您要租赁的汽车类型:");
        int carType = input.nextInt(); // 车辆类型:1、轿车, 2、客车
        int brandType = 0; // 品牌
        int typeId = 0; // 轿车的型号
        int seatType = 0; // 客车的座位数类型:1、16座 2、34座
        if(carType == 1) {
            System.out.print("请选择您要租赁的汽车品牌:1、别克\t2、宝马");
            brandType = input.nextInt();
            System.out.print("请选择你要租赁的汽车类型:"
                    + (brandType == 1 ? "1、林荫大道\t2、GL8":"1、X6\t2、550i"));
            typeId = input.nextInt();
        } else {
            System.out.print("请选择您要租赁的汽车品牌:1、金龙\t2、金杯");
            brandType = input.nextInt();
            System.out.print("请选择您要租赁的汽车座位数:1、16座\t2、34座");
            seatType = input.nextInt();
        }
        System.out.print("请输入您要租赁的天数:");
        int days = input.nextInt();
        // 根据用户交互信息进行选车
        Car car = choiceCar(carType,brandType,typeId,seatType);
        System.out.println("分配给你的汽车牌号是:"+car.getCarNo());
        System.out.println("您需要支付的租赁费用是:"+car.calc(days)+"元。");
    }
    // 选车
    private Car choiceCar(int carType,int brandType,int typeId,int seatType){
        String brand = carType == 1 ? (brandType == 1 ? "别克":"宝马") : (brandType == 1 ? "金龙":"金杯");
        String type = null;
        if(brand.equals("别克")) {
            type = typeId == 1 ? "林荫大道":"GL8";
        } else if(brand.equals("宝马")) {
            type = typeId == 1 ? "X6" : "550i";
        }
        int seatCount = seatType == 0 ? 0 : (seatType == 1 ? 16:34);
        // 遍历所有车辆:查找条件满足brand+type 或者 brand+seatCount
        for (Car car: cars ) {
            if(car.getBrand().equals(brand)){
                if(type != null) {
                    if(((SmallCar)car).getType().equals(type)){
                        return car;
                    }
                } else {
                    if(((BigCar)car).getSeatCount() == seatCount){
                        return car;
                    }
                }
            }
        }
        return null;
    }

}
package cn.kgc.demo8.zuche;

public class TestCar {

    public static void main(String[] args) {
        new CarManager().start();
    }
}
  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值