继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

答答租车系统(优化版本,增加防止重复选择车辆以及完善了代码逻辑)

K_E_N
关注TA
已关注
手记 3
粉丝 15
获赞 242

Auto抽象父类

package com.auto;

public abstract class Auto {

    public String autoName;
    public String autoType;
    public int dailyRentalPrice;

    public String getAutoName() {
        return autoName;
    }

    public void setAutoName(String autoName) {
        this.autoName = autoName;
    }

    public String getAutoType() {
        return autoType;
    }

    public void setAutoType(String autoType) {
        this.autoType = autoType;
    }

    public double getDailyRentalPrice() {
        return dailyRentalPrice;
    }

    public void setDailyRentalPrice(int dailyRentalPrice) {
        this.dailyRentalPrice = dailyRentalPrice;
    }

    @Override
    public String toString() {
        return dailyRentalPrice + "元/天,";
    }

}

Bus子类

package com.auto;

public class Bus extends Auto implements ISeatingCapacity {

    Bus() {
        autoName = "金龙";
        dailyRentalPrice = 1000;
    }

    @Override
    public int seatingCapacity() {

        int seatingCapacity = 20;

        return seatingCapacity;
    }

    public String toString() {
        return autoName + "  " + super.toString() + "  载客:" + seatingCapacity() + "人";
    }

}

Haval子类

package com.auto;

public class Haval extends Auto implements ISeatingCapacity {

    Haval() {
        autoName = "哈佛";
        dailyRentalPrice = 300;
    }

    @Override
    public int seatingCapacity() {

        int seatingCapacity = 5;

        return seatingCapacity;
    }

    public String toString() {
        return autoName + "  " + super.toString() + "  载客:" + seatingCapacity() + "人";
    }

}

Naveco子类

package com.auto;

public class Naveco extends Auto implements ICarryingCapacity {

    Naveco() {
        autoName = "依维柯";
        dailyRentalPrice = 1000;
    }

    @Override
    public double carryingCapacity() {

        double carryingCapacity = 20;

        return carryingCapacity;
    }

    public String toString() {
        return autoName + "  " + super.toString() + "  载货:" + carryingCapacity() + "吨";
    }

}

PickupTruck子类

package com.auto;

public class PickupTruck extends Auto implements ISeatingCapacity, ICarryingCapacity {

    PickupTruck() {
        autoName = "皮卡";
        dailyRentalPrice = 500;
    }

    @Override
    public int seatingCapacity() {

        int seatingCapacity = 5;

        return seatingCapacity;
    }

    @Override
    public double carryingCapacity() {

        double carryingCapacity = 5;

        return carryingCapacity;
    }

    public String toString() {
        return autoName + "  " + super.toString() + "  载客:" + seatingCapacity() + "人" + "  载货:" + carryingCapacity()
                + "吨";
    }

}

ICarryingCapacity载货量接口

package com.auto;

public interface ICarryingCapacity {

    public double carryingCapacity();

}

ISeatingCapacity载客量接口

package com.auto;

public interface ISeatingCapacity {

    public int seatingCapacity();

}

SystemInitail系统初始化主方法

package com.auto;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class SystemInitail {

    public static void main(String[] args) {

        initail();
    }

    //  系统初始化方法
    public static void initail() {

        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车:1:是,0:否");

        try {
            Scanner sn = new Scanner(System.in);
            int input = sn.nextInt();
            if (input == 1) {
                show();     //输入指令“1”后,进入租车流程,并调用“遍历可租用车辆”方法
            } else if (input == 0) {
                System.out.println("退出系统,欢迎下次光临 !");
                sn.close();     //退出选车后,释放所占用的IO资源
                System.exit(0);     //退出系统
            } else {
                System.out.println("您输入的指令有误,请重新输入。");      //防止输入指令以外的数字,系统无法做出相应的判断
                initail();
            }
        } catch (Exception e) {
            System.out.println("您输入的指令有误,请重新输入。");      //防止控制台中输入非数字字符报错
            initail();      //重新初始化
        }
    }

    //遍历可租用车辆方法
    public static void show() {

        System.out.println("您可租车的类型及 其价目表 :");
        System.out.println("序号     汽车名称     租金       容量");

        int count = 1;
        Auto[] auto = { new Bus(), new Haval(), new Naveco(), new PickupTruck() };      //利用多态的特性,创建各种车型的对象

        for (Auto autoList : auto) {

            System.out.println(count + "  " + autoList.toString());
            count++;
        }
        selectAuto(auto);       //遍历完可租车辆后,调用“选车方法”
    }

    //  选车方法
    public static void selectAuto(Auto[] auto) {

        System.out.println("请输入车辆序号确定租车,输入指令‘100’结束 选车,输入指令‘101’终止选车,退出系统。");

        List<Integer> list = new ArrayList<Integer>();      //利用List集合记录所选车辆序号

        System.out.print("请输入辆要租的车序号:");

        while (true) {
            try {
                Scanner sn = new Scanner(System.in);
                int autoNum = sn.nextInt();

                if (autoNum <= auto.length && autoNum > 0) {
                    if (list.contains(autoNum)) {       //防止重复选车
                        System.out.println("车辆已被选择!请重新选择!或输入指令‘101’终止选车,退出系统。");
                        continue;
                    } else {
                        list.add(autoNum);
                    }
                } else if (autoNum == 100) {
                    System.out.println("选车完毕,您租用的车辆有以下" + list.size() + "辆:");      

                    int seatingCapacity = 0;
                    double carryingCapacity = 0;
                    double DailyRentalPrice = 0;
                    Iterator<Integer> it = list.iterator();

                    while (it.hasNext()) {
                        int num = it.next();
                        System.out.println(auto[num - 1].toString());       //遍历所租用的车辆
                        if (auto[num - 1] instanceof ISeatingCapacity) {        //判断所遍历的对象是否实现了载客的接口,是则进入载客量统计
                            seatingCapacity += ((ISeatingCapacity) auto[num - 1]).seatingCapacity();    //向上类型转换,调用所遍历对象实现的接口方法获取载客值并进行累计
                        }
                        if (auto[num - 1] instanceof ICarryingCapacity) {       //判断所遍历的对象是否实现了载货的接口,是则进入载货量统计
                            carryingCapacity += ((ICarryingCapacity) auto[num - 1]).carryingCapacity();     //向上类型转换,调用所遍历对象实现的接口方法获取载货值并进行累计
                        }
                        DailyRentalPrice += auto[num - 1].getDailyRentalPrice();    //向上类型转换,调用所遍历对象继承的租金方法获取租金金额并累计
                    }
                    System.out.println("所选车辆租金总额为:" + DailyRentalPrice + "元/天。");
                    System.out.println("所选车辆载客量为:" + seatingCapacity + "人。");
                    System.out.println("所选车辆载货量为:" + carryingCapacity + "吨。");
                    sn.close();     //退出选车后,释放所占用的IO资源
                            //终止循环
                } else if (autoNum == 101) {
                    System.out.println("退出系统,欢迎下次光临 !");
                    sn.close();     //终止选车,释放所占用的IO资源
                    System.exit(0);     //退出系统      
                } else {
                    System.out.println("您输入的指令有误,请重新输入。");      //防止输入指令以外的数字,系统无法做出相应的判断
                    continue;       //跳出当前循环 
                }
            } catch (Exception e) {
                System.out.println("您输入的指令有误,请重新输入。");      //防止控制台中输入非数字字符报错
                continue;       //跳出当前循环 
            }
        }
    }
}
打开App,阅读手记
9人推荐
发表评论
随时随地看视频慕课网APP