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

哒哒租车系统源码(添加用户选择车辆的异常处理)-互相交流学习

走路不穿鞋
关注TA
已关注
手记 1
粉丝 3
获赞 32

创建抽象父类Car类

package com.sac;

public abstract class Car {
    protected int number;
    protected String name;
    protected int price;
    protected int seat;
    protected int capacity;

    public Car(){

    }

    public Car(int number,String name,int price,int seat,int capacity){
        this.number = number;
        this.name = name;
        this.price = price;
        this.seat = seat;
        this.capacity = capacity;
    }

    public int getSeat(){
        return seat;
    }

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }

    public int getCapacity() {
        return capacity;
    }

    public abstract void print();

}

载人汽车类

package com.sac;

public class Bus extends Car {
    public Bus(int number,String name,int price,int seat){
        this.number = number;
        this.name = name;
        this.price = price;
        this.seat = seat;
    }

    public void print(){
        System.out.println(number+"\t"+name+"\t"+price+"元/天"+"\t"+"载人:"+seat+"人");
    }

}

载货卡车类

package com.sac;

public class Truck extends Car {
    public Truck(int number,String name,int price,int capacity){
        this.number = number;
        this.name = name;
        this.price = price;
        this.capacity = capacity;
    }

    public void print(){
        System.out.println(number+"\t"+name+"\t"+price+"元/天"+"\t"+"载货:"+capacity+"吨");
    }

}

皮卡类

package com.sac;

public class Pickup extends Car {
    public Pickup(int number,String name,int price,int seat,int capacity){
        this.number = number;
        this.name = name;
        this.price = price;
        this.seat = seat;
        this.capacity = capacity;
    }

    public void print(){
        System.out.println(number+"\t"+name+"\t"+price+"元/天"+"\t"+"载人:"+seat+"人、"+"载货:"+capacity+"吨");
    }

}

main主函数

package com.sac;
import java.util.Scanner;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //实例化5辆车
        int sumPrice = 0;
        int sumSeat = 0;
        int sumCapacity = 0;
        Car car[] = {new Bus(1,"马自达3",200,5),
                new Bus(2,"金龙",450,45),
                new Truck(3,"福田",400,20),
                new Truck(4,"东风",500,30),
                new Pickup(5,"福特猛禽",600,5,5)};
        System.out.println("欢迎使用哒哒租车系统!");
        System.out.println("确认租车请输入1,退出请输入0:");
        Scanner sure = new Scanner(System.in);
        //如果输入1进入选车阶段,输入0退出系统
        if(sure.nextInt() == 1){
            System.out.println("我公司目前可选车型信息如下:");
            System.out.println("序号"+"\t"+"车型"+"\t"+"价格"+"\t"+"容量");
            for(int i = 0;i<car.length;i++){
                car[i].print();
            }
            System.out.println("请输入您需要租用的车辆数量:");
            int nums = sure.nextInt();
            //新建一个数组用来存放下面用户选择的车辆序号
            int newNum[] = new int[nums];
            for(int i = 0;i<nums;i++){
                System.out.println("请输入您想要租用的第"+(i+1)+"辆车的序号:");
                int xuhao = sure.nextInt();
                //通过while语句进行用户输入数据的检查,如果超出已有车辆序号,则提示用户重新输入正确的序号
                while(0>xuhao || xuhao>car.length){
                    System.out.println("您输入的序号有误,请重新输入正确的车辆序号:");
                    xuhao = sure.nextInt();
                }
                //直到输入数据正确则直接存入数组
                newNum[i] = xuhao;
            }
            System.out.println("请输入您租用车辆的天数:");
            int days = sure.nextInt();
            //开始输出订单信息并计算相关金额
            System.out.println("您的订单信息如下:");
            System.out.println("=====可载人的车有:");
            for(int i = 0;i<newNum.length;i++){
                //首先计算总的载人座位数
                sumSeat += car[newNum[i]-1].getSeat();
                //其次计算订单单日总金额
                sumPrice += car[newNum[i]-1].getPrice();
                //通过判断座位数是否大于0输出可载人车辆的名字
                if(car[newNum[i]-1].getSeat()>0){
                    System.out.print(car[newNum[i]-1].getName()+"\t");
                }continue;
            }
            System.out.println("共载人:"+sumSeat+"人"+"\t");
            System.out.println("=====可载货的车有:");
            //采用和可载人车辆相同的方法进行判断并输出可载货车辆信息
            for(int i = 0;i<newNum.length;i++){
                sumCapacity += car[newNum[i]-1].getCapacity();
                if(car[newNum[i]-1].getCapacity()>0){
                    System.out.print(car[newNum[i]-1].getName()+"\t");
                }continue;
            }
            System.out.println("共载货:"+sumCapacity+"吨"+"\t");
            System.out.println("本次订单总金额为:");
            //订单每日总金额乘以租用天数得出订单总金额
            System.out.println(sumPrice*days+"元");
        }else{
            System.out.println("欢迎您下次使用哒哒租车系统,再见!");
        }

    }

}
打开App,阅读手记
25人推荐
发表评论
随时随地看视频慕课网APP

热门评论

终于找到跟我很像的了,楼主你这个代码,输入车辆,输入负数就蹦啦

非常好。基本接近标准范本

我觉得你这个父类就不行。。。因为继承讲的时候说的是,父类应该是集成子类锁共有的属性,而载客量还有载货量不是每个类型的车都有,应该提取出来放到句有这个属性类中吧

查看全部评论