手记

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://img.mukewang.com/587e099b0001f86218560940.png
http://img.mukewang.com/587e0a3a0001e4bd18520937.png
http://img.mukewang.com/587e0a2a0001119418550938.png

13人推荐
随时随地看视频
慕课网APP

热门评论

这是大神级的程序????好多没见过的用法

如果要用封装得怎么用?求指教

每种汽车都继承了Car 但是每种汽车都只用了Car类中几种参量 与继承思想有点 不符吧   以及内存方面都要给你不用的属性分配内存     我是  小白  很多不太懂 ^-^

查看全部评论