Rumah > Java > javaTutorial > Bagaimana untuk melaksanakan sistem penyewaan kereta di Jawa

Bagaimana untuk melaksanakan sistem penyewaan kereta di Jawa

王林
Lepaskan: 2023-05-12 23:49:04
ke hadapan
2278 orang telah melayarinya

Kereta sewa:

Ia terbahagi kepada dua jenis: kereta penumpang dan sedan:

Kereta penumpang kurang daripada 20 tempat duduk: 500 sehari, lebih daripada 20 tempat duduk: 900 sehari .

Kereta dibahagikan kepada kereta mewah dan biasa: kereta mewah berharga 600 sehari dan kereta biasa berharga 200 sehari.

Rendering:

Bagaimana untuk melaksanakan sistem penyewaan kereta di Jawa

Bagaimana untuk melaksanakan sistem penyewaan kereta di Jawa

Kodnya adalah seperti berikut:

Kategori kenderaan bermotor:

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);




}
Salin selepas log masuk

Kategori kereta penumpang:

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));


    }
}
Salin selepas log masuk

Kategori kereta:

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));


    }
}
Salin selepas log masuk

Kategori pelanggan kereta sewa:

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);
    }
}
Salin selepas log masuk

Kategori ujian:

package busTest;

public class TestMain {
    public static void main(String[] args) {

        Customer customer = new Customer();
        customer.showMenu();
    }
}
Salin selepas log masuk

Atas ialah kandungan terperinci Bagaimana untuk melaksanakan sistem penyewaan kereta di Jawa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:yisu.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan