【Java面向对象程序设计】PTA实验 - 汽车租赁系统V2.0

记录pta上Java实验二 —— 汽车租赁管理系统

这里记录下自己的代码,有些地方可能考虑并不是很全面,可以优化
在这里插入图片描述
运行截图:
在这里插入图片描述

以下是代码部分

RenCar

父类

Vehicle.java

package CarRentalSystem.RentCar;

public abstract class Vehicle {
    private String vehicleId;
    private String brand;
    private int perRent;
    private String type;

    /* setter 和 getter */
    //车辆种类的
    public String getVehicleId() {
        return vehicleId;
    }
    public void setVehicleId(String vehicleId) {
        this.vehicleId = vehicleId;
    }
    //车辆具体品牌的
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    //日租金的
    public int getPerRent() {
        return perRent;
    }
    public void setPerRent(int perRent) {
        this.perRent = perRent;
    }
    //车牌
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }

    public Vehicle(){}

    public Vehicle(String vehicleId, String brand, String type, int perRent){
        setVehicleId(vehicleId);
        setBrand(brand);
        setPerRent(perRent);
        setType(type);
    }

    //应付租金的抽象方法
    public abstract float calRent(int days);
}

三个子类

Car.java

package CarRentalSystem.RentCar;

public class Car extends Vehicle {

    public Car(){}
    //设置Car的构造方法,用于接收菜单中选择的结果,new要的车
    public Car(String vehicleId, String brand,String type, int perRent){
        //调用父类Vehicle的构造方法
        super(vehicleId, brand,  type, perRent);

    }

    @Override
    public  float calRent(int days){
        float rents;

        if(days > 150){
            rents = getPerRent() * days * (float)0.7;
        }else if(days > 30){
            rents = getPerRent() * days * (float)0.8;
        }else if(days > 7){
            rents = getPerRent() * days * (float)0.9;
        }else{
            rents = getPerRent() * days;
        }

        return rents;
    }
}

Bus.java

package CarRentalSystem.RentCar;

public class Bus extends Vehicle {
    private int seats;

    public int getSeats() {
        return seats;
    }
    public void setSeats(int seats) {
        this.seats = seats;
    }

    public Bus(){}
    //设置Bus的构造方法,用于接收菜单中选择的结果,new要的车
    public Bus(String vehicleId, String brand, String type, int perRent, int seats){
        //调用父类Vehicle的构造方法
        super(vehicleId, brand, type, perRent);

        setSeats(seats);
    }

    @Override
    public  float calRent(int days){
        float rents;

        if(days > 150){
            rents = getPerRent() * days * (float)0.7;
        }else if(days > 30){
            rents = getPerRent() * days * (float)0.8;
        }else if(days > 7){
            rents = getPerRent() * days * (float)0.9;
        }else{
            rents = getPerRent() * days;
        }

        return rents;
    }
}

Trunk.java

package CarRentalSystem.RentCar;

public class Trunk extends Vehicle {
    private int load;//载重量,单位是 吨/t

    public int getLoad() {
        return load;
    }
    public void setLoad(int load) {
        this.load = load;
    }

    public Trunk(){}
    //设置Trunk的构造方法,用于接收菜单中选择的结果,new要的车
    public Trunk(String vehicleId, String brand, String type, int perRent, int load){
        //调用父类Vehicle的构造方法
        super(vehicleId, brand, type, perRent);

        setLoad(load);
    }

    //重写父类的付费方法
    @Override
    public float calRent(int kilos){
        float rents = (float)0.0;

        //出于货车载重量的考虑,这里根据载重量的不同档位设置相应的收费标准
        if(getLoad() <= 10){
            rents = getLoad() * kilos * (float)0.08;
        }else if(getLoad() <= 40){
            rents = 10*(float)0.08 + (getLoad() - 10)*(float)0.12;
        }else if(getLoad() > 40){
            rents = 10*(float)0.08 + 30*(float)0.12 + (getLoad() - 30)*(float)0.2;
        }

        return rents;
    }
}

主类

RentMgrSys.java

package CarRentalSystem.RentMgrSys;
import  CarRentalSystem.RentCar.*;

import java.util.Scanner;

public class RentMgrSys {
    //打印系统欢迎语
    public static void printWelcome(){
        System.out.println("☆☆☆☆ 欢迎光临暮商汽车租赁系统  --Designed by 暮商十二 ☆☆☆☆");
    }

    //打印选择车型的菜单 --> 轿车、客车 or 卡车
    public static void menu_ChooseKind(){
        printWelcome();
        System.out.println("     1.轿车      2.客车     3.卡车    0、退出系统");
        System.out.printf("     请选择你要租赁的汽车类型:");
    }

    public static void whichCar(){
        System.out.printf("     1、宝马    2、别克");
    }
    //打印品牌
    public static void menu_ChooseYourCar(int type){
        if(type == 1){
            System.out.println("     1.宝马-X6      2.宝马-550i");
        }else{
            System.out.println("     1.别克-林荫大道      2、别克-GL8");
        }
        System.out.printf("     请选择你要租赁的轿车品牌:");
    }

    //打印客车品牌
    public static void menu_ChooseBusTYpe(){
            System.out.printf("     1.金杯      2.金龙");
    }
    //输入客车座位数
    public static void menu_ChooseSeats(){
        System.out.printf("     请输入乘客数量");
    }

    //打印货车品牌
    public static void menu_TrunkType(){
        System.out.println("     货车计重收费标准如下\n     1、装载10t内: 每吨每公里收费0.08元\n     2、装载超过10t但不超过40t: 超过部分每吨每公里加收0.04元\n     3/装载超过40t但未超过上限: 超过40t的部分每吨每公里再加收0.08元");
        menu_ChooseTrunkType();
        System.out.printf("     1、大卡A-限载50t  日租1200元     2、大卡B-限载70t  日租1600元");
    }
    //打印货车类型选择
    public static void menu_ChooseTrunkType(){
        System.out.println("     请选择您要租赁的货车类型");
    }

    public static void menu_TrunkLoad(){
        System.out.printf("     请输入装运货物重量,单位为吨: ");
    }

    //分配车牌号提示
    public static void menu_vehicle(String str){
        System.out.println("     分配给您的汽车车牌号是: "+str);
    }

    //天数
    public static void setDays(){
        System.out.printf("     请选择您要租赁的天数: ");
    }

    //付费提示
    /*public static void menu_showRents(float rents){
        System.out.println("     您需要支付的租金是: "+rents);
    }*/

    public static void main(String[] args){
        String vehicleId = "", type = "", brand = "";//品牌等车辆基础信息
        int carKind, theCar, busKind, trKind = 0;//车辆选择信息
        int preRent = 0, days = 0, load = 0, kelos = 0, seats = 0;//租金计算信息
        int nums = 0;//租的车辆数
        Bus bus;
        Car car;
        Trunk tr;

        Scanner in = new Scanner(System.in);


        menu_ChooseKind();
        int CarType = in.nextInt();


        //考虑到实际情景,所以设置一个while循环来模拟实际选车场景,用户可以通过选择不同的操作代号进行多次租车或是系统退出
        while(true){
            //无限循环,用户通过手动选择4来退出系统
            switch (CarType){//根据不同的选择代号进行不同操作,选择4退出
                case 1://轿车
                    //Car car;
                    whichCar();//选择宝马还是别克
                    carKind = in.nextInt();
                    menu_ChooseYourCar(carKind);//宝马或是别克的具体型号
                    theCar = in.nextInt();

                    if(carKind == 1){
                        vehicleId = "宝马";
                        if(theCar == 1){
                            brand = "X6";
                            preRent = 800;
                            type = "京NY28588";
                        }else if(theCar == 2){
                            brand = "550i";
                            preRent = 600;
                            type = "京CNY3284";
                        }
                    }else if(carKind == 2){
                        vehicleId = "别克";
                        if(theCar == 1){
                            brand = "林荫大道";
                            preRent = 300;
                            type = "京NT37465";
                        }else if(theCar == 2){
                            brand = "GL8";
                            preRent = 600;
                            type = "京NT96968";
                        }
                    }

                    setDays();
                    days = in.nextInt();
                    car = new Car(vehicleId, brand, type, preRent);//根据选择,录入所租车的数据
                    menu_vehicle(car.getType());
                    System.out.println("     您所租的车辆种类是: "+car.getVehicleId()+"-"+car.getBrand());
                    System.out.print("     您需要支付的租赁费用是:"+ car.calRent(days) + "元");
                    break;
                case 2:
                    //Bus bus;
                    nums = 0;
                    menu_ChooseBusTYpe();
                    busKind = in.nextInt();
                    if(busKind == 1){
                        vehicleId = "金杯";
                    }else{
                        vehicleId = "金龙";
                    }

                    menu_ChooseSeats();
                    seats = in.nextInt();

                    if(busKind == 1){
                        brand = "16座";
                        if(seats <= 16){
                            nums = 1;
                            type="京6566754";
                        }else if(seats > 16){
                            nums = seats%16 == 0?seats/16:(seats/16+1);
                            type="京6566754、京8696997";
                        }
                        preRent = 800 * nums;
                    }else if(busKind == 2){
                        brand = "34座";
                        if(seats <= 34){
                            nums = 1;
                            type = "京9696996";
                        }else if(seats > 34){
                            type = "京9696996、京8696998";
                            nums = seats%34 == 0?seats/34:(seats/34+1);
                        }
                        preRent = 1500*nums;
                    }

                    setDays();
                    days = in.nextInt();
                    bus = new Bus(vehicleId, brand, type, preRent,seats);
                    menu_vehicle(bus.getType());
                    System.out.println("     您所租的车辆种类是: "+bus.getVehicleId()+"-"+bus.getBrand());
                    System.out.print("     您需要支付的租赁费用是:"+ bus.calRent(days) + "元");
                    break;
                case 3:
                    //Trunk tr;
                    nums = 0;

                    menu_TrunkType();//选择卡车类型
                    trKind = in.nextInt();
                    if(trKind == 1){
                        vehicleId = "大卡A";
                    }else{
                        vehicleId = "大卡B";
                    }


                    menu_TrunkLoad();
                    load = in.nextInt();

                    if(trKind== 1){
                        brand = "限载50t";
                        if(load <= 50){
                            nums = 1;
                            type="京11111";
                        }else if(load > 50){
                            nums = load%50 == 0?load/50 :(load/50);
                            type="京11111、京22222";
                        }
                        preRent = 1200*nums;
                    }else if(trKind == 2){
                        brand = "限载70t";
                        if(load <= 70){
                            nums = 1;
                            type = "京22222";
                        }else if(load > 70){
                            type = "京11111、京22222";
                            nums = load%70 == 0?load/70:(load/70+1);
                        }
                        preRent = 1600*nums;
                    }

                    setDays();
                    days = in.nextInt();
                    menu_vehicle(type);
                    tr = new Trunk(vehicleId, brand, type, preRent, load);
                    System.out.printf("     请输入预计公里数(整数):");
                    kelos = in.nextInt();
                    float res = tr.calRent(kelos) + days*preRent;
                    System.out.println("     您所租的车辆种类是: "+tr.getVehicleId()+"-"+tr.getBrand());
                    System.out.println("     您需要支付的租赁费用是:" + res + "元");
                    break;
                case 0:
                    System.exit(0);
            }
            System.out.printf("\n\n");
            menu_ChooseKind();
            CarType = in.nextInt();
        }
    }
}

有很多地方还可以优化,参考的时候可以自己按照自己需求改一下

  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

六时二一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值