为了账号安全,请及时绑定邮箱和手机立即绑定

嗒嗒租车系统(JAVA入门第二季练习题)

标签:
Java
package com.imooc;
/**
 * 
* @ClassName: Car 
* @Description: 父类
* @author LU1024 
* @date 2016年8月18日 下午3:29:50 
* @version V1.0
 */
public abstract class Car {
    protected String name;//车名
    protected int rent;//租金
    protected int peopleCapacity;//载客量
    protected int goodsCapacity;//载人量

    public int getPeopleCapacity() {
        return peopleCapacity;
    }

    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }

    public int getGoodsCapacity() {
        return goodsCapacity;
    }

    public void setGoodsCapacity(int goodsCapacity) {
        this.goodsCapacity = goodsCapacity;
    }

    public abstract String setList();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getRent() {
        return rent;
    }

    public void setRent(int rent) {
        this.rent = rent;
    }
}
package com.imooc;
/**
 * 
* @ClassName: Bus 
* @Description: 客车子类
* @author LU1024  
* @date 2016年8月18日 下午3:31:16 
* @version V1.0
 */
public class Bus extends Car {
    //构造方法
    public Bus(String name,int rent,int peopleCapacity){
        this.name = name;
        this.rent = rent;
        this.peopleCapacity = peopleCapacity;
    }

    @Override
    public String setList() {
        // TODO Auto-generated method stub
        String info = '\t'+this.name+'\t'+this.rent+'\t'+'\t'+"载人:"+this.peopleCapacity+"人";
        return info;
    }

}
package com.imooc;
/**
 * 
* @ClassName: Truck 
* @Description: 货车子类
* @author LU1024 
* @date 2016年8月18日 下午3:32:06 
* @version V1.0
 */
public class Truck extends Car {
    //构造方法
    public Truck(String name,int rent,int goodsCapacity){
        this.name = name;
        this.rent = rent;
        this.goodsCapacity = goodsCapacity;
    }

    @Override
    public String setList() {
        // TODO Auto-generated method stub
        String info = '\t'+this.name+'\t'+this.rent+'\t'+'\t'+"载货:"+this.goodsCapacity+"吨";
        return info;
    }

}
package com.imooc;
/**
 * 
* @ClassName: Pickup 
* @Description: 皮卡子类
* @author LU1024 
* @date 2016年8月18日 下午3:33:32 
* @version V1.0
 */
public class Pickup extends Car {
    //构造方法
    public Pickup(String name,int rent,int peopleCapacity,int goodsCapacity){
        this.name = name;
        this.rent = rent;
        this.peopleCapacity = peopleCapacity;
        this.goodsCapacity = goodsCapacity;
    }
    @Override
    public String setList() {
        // TODO Auto-generated method stub
        String info = '\t'+this.name+'\t'+this.rent+'\t'+'\t'+"载人:"+this.peopleCapacity+"人"+'\t'+"载货:"+this.goodsCapacity+"吨";
        return info;
    }

}
package com.imooc;

import java.util.Scanner;

/** 
 * @ClassName: RentSystem
 * @Description: 主函数
 * @author LU1024 
 * @date 2016年8月18日 下午3:34:04
 * @version V1.0
 */
public class RentSystem {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //用父类初始化车辆
        Car[] RentCar = { new Bus("奥迪A6", 500, 4), new Bus("宝马x6", 400, 4), new Truck("自卸王", 300, 20),
                new Truck("翻土车", 200, 10), new Pickup("奔腾", 300, 3, 5), new Pickup("雪弗兰", 200, 5, 6) };
        System.out.println("欢迎使用嗒嗒租车系统");
        System.out.println("您是否租车:租车按1,推出按0");
        System.out.print("请输入您的选择:");
        while (true) {
            Scanner scan = new Scanner(System.in);
            int choice = scan.nextInt();
            System.out.println();
            try {
                if (choice == 1) {
                    System.out.println("       ***租车类型及价格表***");
                    //显示车辆详情
                    System.out.println("序号\t名称\t租金(元/天)\t容量");
                    for (int i = 0; i < RentCar.length; i++) {
                        System.out.println((i + 1) + "  " + RentCar[i].setList());
                    }
                    System.out.println();
                    System.out.print("请输入你要租车的数量:");
                    int num = scan.nextInt();//输入租车的数量
                    Car[] ChoseCar = new Car[num];//保存所租的车
                    int[] ChoseNumber = new int[num];//保存选择租车的序号
                    int[] ChoseDay = new int[num];//保存每种车所租天数
                    int[] SumPrice = new int[num];//保存每种车总租金
                    int sumPeople = 0;//载客总人数
                    int sumGoods = 0;//载货总人数
                    int sum = 0;//所有车总租金
                    for (int i = 0; i < ChoseCar.length; i++) {
                        System.out.print("请输入第" + (i + 1) + "辆车的序号:");
                        int choseNum = scan.nextInt();
                        ChoseNumber[i] = choseNum;
                        ChoseCar[i] = RentCar[choseNum - 1];
                        System.out.print("请输入要租第" + (i + 1) + "辆车的天数:");
                        int Day = scan.nextInt();
                        ChoseDay[i] = Day;
                        SumPrice[i] = ChoseDay[i] * ChoseCar[i].rent;

                        if (ChoseCar[i] instanceof Bus) {
                            sumPeople += ((Bus) ChoseCar[i]).getPeopleCapacity();
                        }
                        if (ChoseCar[i] instanceof Truck) {
                            sumGoods += ((Truck) ChoseCar[i]).getGoodsCapacity();
                        }
                        if (ChoseCar[i] instanceof Pickup) {
                            sumPeople += ((Pickup) ChoseCar[i]).getPeopleCapacity();
                            sumGoods += ((Pickup) ChoseCar[i]).getGoodsCapacity();
                        }

                        sum += SumPrice[i];
                    }
                    System.out.println(" *********租车清单********* ");
                    System.out.println("--------------------------");
                    System.out.println("序号\t名称\t租金(元/天)\t天数");
                    for (int i = 0; i < ChoseCar.length; i++) {
                        System.out.println(ChoseNumber[i] + "\t" + ChoseCar[i].name + "\t" + ChoseCar[i].rent + "\t\t"
                                + ChoseDay[i]);
                    }

                    System.out.println("--------------------------");
                    System.out.println("\t" + "\t" + "车载总人数:" + sumPeople);
                    System.out.println("\t" + "\t" + "车载总吨数:" + sumGoods);
                    System.out.println("\t\t总价:" + sum);
                    scan.close();
                    ;
                    System.out.println();
                    System.out.println("感谢您使用嗒嗒租车系统!");
                    break;
                }
                else if (choice == 0) {
                    System.out.println("感谢您使用嗒嗒租车系统!");
                } else {
                    System.out.print("输入有误,请重新输入:");
                    continue;
                }
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("系统异常,系统将关闭");
                scan.close();
                System.exit(0);
            }
        }

    }

}
点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消