美文网首页程序员
Java学习笔记——好记性不如烂键盘(答答租车系统)*内有源码

Java学习笔记——好记性不如烂键盘(答答租车系统)*内有源码

作者: 想象美 | 来源:发表于2017-07-24 22:14 被阅读297次

    根据所学知识,编写一个控制台版的租车系统
    功能:1、 展示所有可租车辆
    2、 选择车型、租车辆
    3、 展示租车清单,包含:总金额、总载货量及其车型、总载人量及其车型
    代码参考imooc中[JavaEE]课程Demo编写
    父类Car:

    package com.RentCar;
     
    public class Car {
        String name; 
        int money; 
        int people; 
        int goods; 
        public String getName() { 
            return name; 
            } 
        public int getMoney() { 
            return money; 
            } 
        public int getPeople() {
            return people; 
            } 
        public int getGoods() { 
            return goods; 
            } 
    }
    

    Car 的子类——载人汽车CarPeople:

    package com.RentCar;
     
    public class CarPeople extends Car{
        String name; 
        int money; 
        int people; 
        public CarPeople(String name,int money,int people){ 
            this.name = name; 
            this.money = money; 
            this.people = people; 
            } 
        @Override
        public String toString() { 
            return this.name + "" + this.money + this.people; 
            } 
        public String getName() { 
            return name; 
            } 
        public int getMoney() {
            return money; 
            } 
        public int getPeople() { 
            return people; 
            }
    }
    

    Car 的子类——载货汽车CarGoods:

    package com.RentCar;
     
    public class CarGoods extends Car {
        String name; 
        int money; 
        int goods; 
        public CarGoods(String name,int money,int goods){ 
            this.name = name; 
            this.money = money; 
            this.goods = goods; 
            } 
        @Override
        public String toString() {
            return this.name + this.money + this.goods;
            } 
        public String getName() { 
            return name; 
            } 
        public int getMoney() { 
            return money; 
            } 
        public int getGoods() { 
            return goods; 
            }
    }
    

    Car 的子类——载人载货CarAll:

    package com.RentCar;
     
    public class CarAll extends Car {
        String name; 
        int money; 
        int people; 
        int goods; 
        public CarAll(String name,int money,int people,int goods){ 
            this.name = name; 
            this.money = money; 
            this.people = people; 
            this.goods = goods; 
            } 
        @Override
        public String toString() { 
            return this.name + this.money + this.people + this.goods; 
            } 
        public String getName() { 
            return name; 
            } 
        public int getMoney() { 
            return money; 
            } 
        public int getPeople() { 
            return people; 
            } 
        public int getGoods() { 
            return goods;
            }
    }
    

    主函数:

    package com.RentCar;
    import java.util.Scanner; 
     
     
    public class CarRentingSystem {
     
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
            while (true) { 
                System.out.println("你到底要不要租虎哥的车?(租/不租)");
                Scanner scanner = new Scanner(System.in); 
                String input = scanner.next(); 
                if(input.equals("租")){ 
                    Car[] cars = {new CarPeople("奥迪A4",400,4),new CarPeople("马自达",400,4),new CarAll("皮卡6",450,4,20), new CarPeople("金龙",800,20), new CarGoods("松花江",400,4),new CarGoods("依维柯",1000,20)
                            }; 
                    System.out.println("下面是您能租的车:"); 
                    System.out.println("序号 品牌 价格 载人/货量"); 
                    for(int i = 0;i < cars.length;i++){ 
                        System.out.printf("%-3d %-6s% 6d %3d个人,%d吨货",i+1,cars[i].getName(),cars[i].getMoney(),cars[i].getPeople(),cars[i].getGoods()); 
                        } 
                    System.out.println("你想要租几辆啊:"); 
                    int number = scanner.nextInt(); 
                    Car[] chooseCars = new Car[number]; 
                    Car[] chooseCarsPeople = new Car[number]; 
                    int CCPNum = 0;//选择的载人汽车的数目
                    Car[] chooseCarsGoods = new Car[number]; 
                    int CCGNum = 0;//选择的载货汽车的数目 
                    //定义总共的钱数,载客量,载货量 
                    int money = 0; 
                    int people = 0; 
                    int goods = 0; 
                    for(int i = 0,j = 1;i < number;i++,j++){ 
                        System.out.println("请输入第" + j + "辆车的序号:"); 
                        int carNum = scanner.nextInt() - 1; 
                        try { chooseCars[i] = cars[carNum]; 
                        money += chooseCars[i].getMoney(); 
                        people += chooseCars[i].getPeople(); 
                        goods += chooseCars[i].getGoods(); 
                        if(chooseCars[i].getPeople() != 0){ 
                            chooseCarsPeople[CCPNum] = chooseCars[i]; CCPNum++; 
                            } 
                        if(chooseCars[i].getGoods() != 0){ 
                            chooseCarsGoods[CCGNum] = chooseCars[i]; CCGNum++; 
                            } 
                        } 
                        catch (Exception e) { 
                            System.out.println("说了几遍了,输入1~6的序号!\n"); i--; j--; 
                            } 
                        } 
                    System.out.println("你想要租几天啊:");
                    int dayNum = scanner.nextInt(); 
                    System.out.printf("你一共花了:%d元\n您总共租了%d辆车,租赁%d天\n",money*dayNum,number,dayNum); 
                    System.out.println("其中,载人的车有" + CCPNum + "辆"); 
                    try { for(Car CarsPeople:chooseCarsPeople){ 
                        System.out.println(CarsPeople.getName()); 
                        } 
                    } 
                    catch (Exception e) { 
     
                    } 
                    System.out.printf("总载客量:%d人\n",people*dayNum); 
                    System.out.println("载货的车有" + CCGNum + "辆"); 
                    try { for(Car CarsGoods:chooseCarsGoods){ 
                        System.out.println(CarsGoods.getName()); 
                        } 
                    }
                    catch (Exception e) { } 
                    System.out.printf("总载货量:%d吨\n",goods*dayNum); 
                    break; 
                    }
                else if(input.equals("不租")){
                    System.out.println("不租你过来干嘛!"); 
                    break; 
                    }
                else { 
                    System.out.println("你输错了大兄弟!\n"); 
                    } 
                } 
            System.out.println("慢走不送!");
                }
        }
    
    Paste_Image.png Paste_Image.png

    关注作者获取更多资料。

    相关文章

      网友评论

        本文标题:Java学习笔记——好记性不如烂键盘(答答租车系统)*内有源码

        本文链接:https://www.haomeiwen.com/subject/lmdikxtx.html