千家信息网

Java如何实现汽车租赁系统

发表于:2025-01-17 作者:千家信息网编辑
千家信息网最后更新 2025年01月17日,这篇文章主要为大家展示了"Java如何实现汽车租赁系统",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Java如何实现汽车租赁系统"这篇文章吧。汽车租赁:分
千家信息网最后更新 2025年01月17日Java如何实现汽车租赁系统

这篇文章主要为大家展示了"Java如何实现汽车租赁系统",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Java如何实现汽车租赁系统"这篇文章吧。

汽车租赁:

分为客车和轿车两种:

客车小于20座:500一天,大于20座:900一天。

轿车分为豪华和普通:豪华600一天,普通200一天。

效果图:

代码如下:

机动车类:

package busTest;/*机动车类 */public abstract  class MotoVehicle {    private String carNumber; //车牌号    private String carBrand;  // 车品牌    //构造方法    public MotoVehicle(){}    public MotoVehicle(String carNumber, String carBrand) {        this.carNumber = carNumber;        this.carBrand = carBrand;    }    // get/set    public String getCarNumber(){        return carNumber;    }    public void setCarNumber(String carNumber){        this.carNumber = carNumber;    }    public String getCarBrand(){        return carBrand;    }    public void setCarBrand(String carBrand){        this.carNumber = carNumber;    }    /*    计算租赁的方法     */    public abstract int calRent(int days);}

客车类:

package busTest;public class Bus extends MotoVehicle {    private int setCount;  //座位数    //通过构造方法初始化对象    public Bus(String carNUmber, String brand, int setCount) {        super(carNUmber, brand);        this.setCount = setCount;    }    @Override    public int calRent(int days) {        //根据座位数量来判断租赁的金额        if (this.setCount < 20) {            return days * 500;        } else {            return days * 900;        }    }    public void showBusInfo(int days) {        System.out.println("*");        System.out.println("\t车牌号:" + super.getCarNumber());        System.out.println("\t车品牌:" + super.getCarBrand());        System.out.println("\t座位数:" + this.setCount);        System.out.println("\t租赁天数:" + days);        System.out.println("\t金额:" + calRent(days));    }}

轿车类:

package busTest;public class Car extends MotoVehicle {    private String type;  //汽车类型  普通/豪华    //通过构造方法初始化对象    public Car(String carNUmber, String brand, String type) {        super(carNUmber, brand);        this.type = type;    }    @Override    public int calRent(int days) {        //根据类型来决定价格        if ("豪车".equals(type)) {            return days * 600;        } else {            return days * 200;        }    }    public void showCarInfo(int days) {        System.out.println("*");        System.out.println("\t车牌号:" + super.getCarNumber());        System.out.println("\t车品牌:" + super.getCarBrand());        System.out.println("\t车类型:" + this.type);        System.out.println("\t租赁天数:" + days);        System.out.println("\t金额:" + calRent(days));    }}

租车顾客类:

package busTest;/*顾客类 */import java.util.Scanner;public class Customer {    private String name;    private int sum = 0;    //当不确定我的购物车内具体是轿车还是客车,那就以父亲类类型创建对象数组    MotoVehicle[] motos = new MotoVehicle[10];    Scanner input = new Scanner(System.in);    public void showMenu() {        //定义一个父类机动车的对象,在下面可以接收        MotoVehicle moto = null;        System.out.println("******汽车租赁系统*******");        String answer;        do {            System.out.println("1、租赁客车  2、租赁轿车");            System.out.print("请输入编号:");            int num = input.nextInt();            if (num == 1) {                //创建租赁的客车对象                moto = rentBus();            } else if (num == 2) {                //创建租赁的轿车对象                moto = rentCar();            }            for (int i = 0; i < motos.length; i++) {                if (motos[i] == null) {                    motos[i] = moto;                    break;                }            }            System.out.print("是否继续租赁?:y/n");            answer = input.next();        } while (!"n".equals(answer));        System.out.print("请输入你的姓名:");        this.name = input.next();        System.out.print("租赁的天数:");        int days = input.nextInt();        //根据天数来统计租赁金额        calTotalRent(days);        //显示租赁的信息        showInfo(days);    }    private void showInfo(int days) {        System.out.println("---------------------租赁汽车信息---------------------");        for (int i = 0; i < motos.length; i++) {            MotoVehicle moto = this.motos[i];            if (moto != null) {                if (moto instanceof Bus) {                    Bus bus = (Bus) moto;                    bus.showBusInfo(days);                } else if (moto instanceof Car) {                    Car car = (Car) moto;                    car.showCarInfo(days);                }            }        }        System.out.println("\t顾客:" + this.name + "\t\t总金额:" + sum);        System.out.println("----------------------------------------------------");    }    private void calTotalRent(int days) {        int total = 0;        for (MotoVehicle moto : motos) {            if (moto != null) {                int rent = moto.calRent(days);                total += rent;  //累加总金额            }            this.sum = total;// 把总金额复制给全局变量 sum        }    }    //轿车    private MotoVehicle rentCar() {        System.out.print("请输入轿车品牌:");        String brand = input.next();        System.out.print("请输入轿车车牌号:");        String carNumber = input.next();        System.out.print("请选择轿车类型[1、豪车 2、普通车:]");        int choise = input.nextInt();        String type;        if (choise == 1) {            type = "豪车";        } else {            type = "普通型";        }        return new Car(carNumber, brand, type);    }    //客车    private MotoVehicle rentBus() {        System.out.print("请输入客车品牌:");        String brand = input.next();        System.out.print("请输入客车车牌号:");        String carNumber = input.next();        System.out.print("请输入客车座位数:");        int seatCount = input.nextInt();        return new Bus(carNumber, brand, seatCount);    }}

测试类:

package busTest;public class TestMain {    public static void main(String[] args) {        Customer customer = new Customer();        customer.showMenu();    }}

以上是"Java如何实现汽车租赁系统"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0