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

倾尽洪荒神力,实现史上最强答答租车系统。

标签:
Java

大致思路如下:

  1. 创建两个接口(载客接口A-载客量)、(载货接口B-载货量)
    一个抽象父类Car(ID、车型、名称、单价);
  2. 实现-货车类 extend 父类Car implement 载货接口B;
    实现-私家车类 extend 父类Car implement 载客接口A;
    实现-皮卡类 extend 父类Car implement 载客和货接口C;
  3. 租车系统类:一维数组 实例化 卡车对象、客车对象 和 皮卡对象;
  4. 客户选车、分类汇总车辆后,输出租车明细与总价。

代码如下:

载货接口:

package com.dadazuche;

public interface IWeight {
    public double getWeight();
}

载客接口:

package com.dadazuche;

public interface INum {
    public int getNum();
}

抽象卡车类:

package com.dadazuche;

public abstract class Car {
    private int ID;
    private String type;
    private String name;
    private double price;
    public Car() {
    }

    public Car(int ID, String type, String name, double price) {
        // TODO 自动生成的构造函数存根
        this.ID = ID;
        this.type = type;
        this.name = name;
        this.price = price;
    }

    public int getID() {
        return this.ID;
    }

    public String getType() {
        return this.type;
    }

    public String getName() {
        return this.name;
    }

    public double getPrice() {
        return this.price;
    }

    public abstract void printInfo();
}

私家车类:

package com.dadazuche;

public class SmallCar extends Car implements INum {

    private int num;
    final static String type = "SMALLCAR";
    public SmallCar(int ID,  String name, double price, int num) {
        // TODO 自动生成的构造函数存根
        super(ID, type, name, price);
        this.num = num;
    }

    @Override
    public int getNum() {
        // TODO 自动生成的方法存根
        return this.num;
    }

    @Override
    public void printInfo() {
        // TODO 自动生成的方法存根
        System.out.println(getID() + "\t" + getName() + "\t" + getPrice() + "元/天\t" + "载人:" + getNum() + "人");
    }

}

货车类:

package com.dadazuche;

public class Truck extends Car implements IWeight {

    private double weight;
    final static String type = "TRUCK";
    public Truck(int ID, String name, double price, double weight) {
        // TODO 自动生成的构造函数存根
        super(ID, type, name, price);
        this.weight = weight;
    }

    @Override
    public double getWeight() {
        // TODO 自动生成的方法存根
        return this.weight;
    }

    @Override
    public void printInfo() {
        // TODO 自动生成的方法存根
        System.out.println(getID() + "\t" + getName() + "\t" + getPrice() + "元/天\t" + "载货:" + getWeight() + "吨");
    }

}

皮卡车类:

package com.dadazuche;

public class PickUp extends Car implements IWeight, INum {

    private int num;
    private double weight;
    final static String type = "PICKUP";

    public PickUp(int ID, String name, double price, int num, double weight) {
        super(ID,type, name, price);
        this.num = num;
        this.weight = weight;
    }

    @Override
    public int getNum() {
        // TODO 自动生成的方法存根
        return this.num;
    }

    @Override
    public double getWeight() {
        // TODO 自动生成的方法存根
        return this.weight;
    }

    @Override
    public void printInfo() {
        // TODO 自动生成的方法存根
        System.out.println(getID() + "\t" + getName() + "\t" + getPrice() + "元/天\t" + "载人:" + getNum() + "人\t" + "载货:"
                + getWeight() + "吨");
    }

}

测试类:

package com.dadazuche;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        final int totalCarNum = 6;
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否需要租车:1是 0否");
        Scanner input = new Scanner(System.in);
        int isRent = input.nextInt();
        if (isRent == 1) {
            System.out.println("您可租车的类型及其价目表:");
            Car[] carArr = new Car[6];
            carArr[0] = new SmallCar(1, "奥迪A4", 500, 4);
            carArr[1] = new SmallCar(2, "马自达6", 400, 4);
            carArr[2] = new SmallCar(3, "金龙", 800, 20);
            carArr[3] = new Truck(4,  "松花江", 400, 4);
            carArr[4] = new Truck(5,  "依维柯", 1000, 20);
            carArr[5] = new PickUp(6, "皮卡雪6", 450, 4, 2);

            for (int i = 0; i < carArr.length; i++) {
                carArr[i].printInfo();
            }
            System.out.println("请输入您要租汽车的数量:0~6");
            int carRentNum = input.nextInt();
            if (carRentNum < 0 || totalCarNum > 6) {
                System.out.println("请输入合法的数量");
            }
            System.out.println("请依次输入车的序号:");
            int carIndexArr[] = new int[carRentNum];
            for (int i = 0; i < carIndexArr.length; i++) {
                carIndexArr[i] = input.nextInt() - 1;
            }
            System.out.println("请输入租赁天数:");
            int rentDays = input.nextInt();
            System.out.println("您的账单:");

            double totalPrice = 0;
            double totalweight = 0;
            int totalNum = 0;
            String truckStr = "";
            String pickUpStr = "";
            String smallCarStr = "";
            for (int i = 0; i < carIndexArr.length; i++) {

                totalPrice += carArr[carIndexArr[i]].getPrice();
                String type = carArr[carIndexArr[i]].getType();
                switch (type) {
                case "TRUCK":
                    truckStr += carArr[carIndexArr[i]].getName();
                    totalweight += ((Truck) carArr[carIndexArr[i]]).getWeight();
                    break;
                case "PICKUP":
                    pickUpStr += carArr[carIndexArr[i]].getName();
                    totalweight += ((PickUp) carArr[carIndexArr[i]]).getWeight();
                    totalNum += ((PickUp) carArr[carIndexArr[i]]).getNum();
                    break;
                case "SMALLCAR":
                    smallCarStr += carArr[carIndexArr[i]].getName();
                    totalNum += ((SmallCar) carArr[carIndexArr[i]]).getNum();
                    break;
                }

            }
            totalPrice *= rentDays;
            System.out.println("私家车有:");
            System.out.println(smallCarStr);
            if (truckStr != null) {
                System.out.println("货车有:");
                System.out.println(truckStr);
            }
            if (pickUpStr != null) {
                System.out.println("皮卡有:");
                System.out.println(pickUpStr);
            }

            System.out.print("租车总价格:" + totalPrice + "元\t共载货:" + totalweight + "吨\t共载人:" + totalNum + "人");
        } else if (isRent == 0) {
            System.out.println("再见,欢迎下次光临!");
        }

    }

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

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消