亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Java 答答租車系統(原創代碼分享)

標簽:
Java
答答租车系统
相关说明
  1. Car为父类,定义共有属性和方法print();
  2. 子类分别改写方法print();
  3. 在main方法中增加了对输入内容的判定和异常的捕捉及解决,保证了输入内容的合理性;
  4. 优化了账单输出式样。

Car.java
package com.jeffli;

public class Car {
    protected int id;
    protected String name;
    protected float price;
    protected int capPerson;
    protected float capCargo;
    public void print(){}
}

PCar.java
package com.jeffli;

public class PCar extends Car {
    public PCar(int id, String name, float price, int capPerson) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.capPerson = capPerson;
    }

    @Override
    public void print() {
        // TODO Auto-generated method stub
        System.out.println(id + ".\t\t" + name + "\t\t" + price+ "元/天" + "\t\t" + "载人:" + capPerson + "人");
    }

}

CCar.java
package com.jeffli;

public class CCar extends Car {
    public CCar(int id, String name, float price, float capCargo) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.capCargo = capCargo;
    }

    @Override
    public void print() {
        // TODO Auto-generated method stub
        System.out.println(id + ".\t\t" + name + "\t\t" + price+ "元/天" + "\t\t" + "载货:" + capCargo + "吨");
    }

}

PCCar.java
package com.jeffli;

public class PCCar extends Car{
    public PCCar(int id, String name, float price, int capPerson, float capCargo) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.capPerson = capPerson;
        this.capCargo = capCargo;
    }

    @Override
    public void print() {
        // TODO Auto-generated method stub
        System.out.println(id + ".\t\t" + name + "\t\t" + price+ "元/天" + "\t\t" + "载人:" + capPerson + "人" + "    " + "载货:" + capCargo + "吨");
    }

}

CarSystem.java
package com.jeffli;

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

public class CarSystem {

    public static void main(String[] args) {

        Car[] cars = {new PCar(1, "奥迪A4", 500f, 4), new PCar(2, "马自达6", 400f, 4),
                new PCCar(3, "皮卡雪6", 450f, 4, 2f), new PCar(4, "金龙", 800f, 20), 
                new CCar(5, "松花江", 400f, 4f), new CCar(6, "依维柯", 1000f, 20f)};

        System.out.println("------------欢迎使用答答租车系统!------------");
        System.out.println("您是否要租车?\n是:请输入1\t\t否:请按任意键退出");

        Scanner scanner = new Scanner(System.in);
        String input = scanner.next();

        if (input.equals("1") == false) {

            System.out.println("感谢您的使用,下次再见(^_^)");
            scanner.close();

        } else if (input.equals("1")) {

            System.out.println("您可租用的车型及其价目为:");
            System.out.println("序号\t汽车名称\t租金\t\t\t"+ "容量");

            for (Car car : cars) {
                if (car instanceof PCar) {
                    car.print();
                }
                if (car instanceof CCar) {
                    car.print();
                }
                if (car instanceof PCCar) {
                    car.print();
                }
            }
            System.out.println();
            float sum = 0f;
            float capC = 0f;
            int capP = 0;
            List<String> namesP = new ArrayList<>();
            List<String> namesC = new ArrayList<>();

            outer:
            for (int i = 1; ; i++) {
                int index = 0;
                int num = 0;
                int day = 0;
                while (true) {
                    System.out.print("请输入您要租用的第" + i + "辆车的序号:");
                    int in = 0;
                    try {
                        in = Integer.parseInt(scanner.next());
                    } catch (NumberFormatException e) {
                        System.out.println("【您输入的信息有误,请重新输入!】");
                        continue;
                    }
                    if (in < 1 || in > cars.length) {
                        System.out.println("【您输入的信息有误,请重新输入!】");
                        continue;
                    }
                    index = in - 1;
                    break;
                }
                while (true) {
                    System.out.print("请输入您要租用的数量:");
                    int in = 0;
                    try {
                        in = Integer.parseInt(scanner.next());
                    } catch (NumberFormatException e) {
                        System.out.println("【您输入的信息有误,请重新输入!】");
                        continue;
                    }
                    if (in < 1) {
                        System.out.println("【您输入的信息有误,请重新输入!】");
                        continue;
                    }
                    num = in;
                    break;
                }
                while (true) {
                    System.out.print("请输入您要租用的天数:");
                    int in = 0;
                    try {
                        in = Integer.parseInt(scanner.next());
                    } catch (NumberFormatException e) {
                        System.out.println("【您输入的信息有误,请重新输入!】");
                        continue;
                    }
                    if (in < 1) {
                        System.out.println("【您输入的信息有误,请重新输入!】");
                        continue;
                    }
                    day = in;
                    break;
                }   

                    sum += cars[index].price * num * day;
                    capC += cars[index].capCargo * num;
                    capP += cars[index].capPerson* num;

                    if (cars[index].capCargo != 0f) {
                        namesC.add(cars[index].name);
                    }
                    if (cars[index].capPerson != 0) {
                        namesP.add(cars[index].name);
                    }

                while (true) {
                    System.out.println("\n您是否需要继续租车?\n是:请输入1\t\t否:请输入0");
                    int in = scanner.nextInt();
                    if (in == 1) {
                        continue outer;
                    } else if (in == 0) {
                        break outer;
                    } else {
                        System.out.println("您输入的信息有误,请重新输入:");
                        continue;
                    }
                }
            }
            System.out.println("----------------------------------------------------------------------\n\n租车成功!下面是您的账单:\n");
            System.out.println("------------------------------载客车------------------------------\n");
            if (namesP.isEmpty()) {
                System.out.print("无");
            } else {
                for (int i = 0; i < namesP.size(); i++) {
                    System.out.print(namesP.get(i) + "\t\t");
                }
            }
            System.out.println();
            System.out.println("共载人:" + capP + "人\n");
            System.out.println("------------------------------载货车------------------------------\n");
            if (namesC.isEmpty()) {
                System.out.print("无");
            } else {
                for (int i = 0; i < namesC.size(); i++) {
                    System.out.print(namesC.get(i) + "\t\t");
                }
            }
            System.out.println();
            System.out.println("共载货:" + capC + "吨\n");
            System.out.println("----------------------------------------------------------------------\n");
            System.out.println("总计支付:"  + sum + "元\n");
            System.out.println("----------------------------------------------------------------------\n\n");
            System.out.println("账单已完成,请按任意键退出:");
            scanner.next();
            System.out.println("感谢您的使用,下次再见(^_^)");
            scanner.close();
        }
    }
}

效果演示(请点击图片链接浏览)

http://img1.sycdn.imooc.com//587e099b0001f86218560940.png
http://img1.sycdn.imooc.com//587e0a3a0001e4bd18520937.png
http://img1.sycdn.imooc.com//587e0a2a0001119418550938.png

點擊查看更多內容
13人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消