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

Java入门第二季 项目实战 达达租车系统代码实现

标签:
Java

car.java

package com.meteor.dada.vo;

/**
 * 汽车类,封装租车的共有属性
 *
 * @author 刘佳星
 * @version v1.0
 */
public abstract class Car {

    protected int id;//序号
    protected String name;//汽车名称
    protected int rent;//租金

    public Car() {
    }

    public Car(int id, String name, int rent) {
        this.id = id;
        this.name = name;
        this.rent = rent;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }
}

PassengerCar.java

package com.meteor.dada.vo;

/**
 * 客车类
 *
 * @author 刘佳星
 * @version v1.0
 */
public class PassengerCar extends Car{

    private int busload;//载客量

    public PassengerCar() {
    }

    public PassengerCar(int id, String name, int rent, int busload) {
        super(id, name, rent);
        this.busload = busload;
    }

    public int getBusload() {
        return busload;
    }

    public void setBusload(int busload) {
        this.busload = busload;
    }
}

TruckCar.java

package com.meteor.dada.vo;

/**
 * 货车类
 *
 * @author 刘佳星
 * @version v1.0
 */
public class TruckCar extends Car{

    private float cargoCapacity;//载货量

    public TruckCar() {
    }

    public TruckCar(int id, String name, int rent, float cargoCapacity) {
        super(id, name, rent);
        this.cargoCapacity = cargoCapacity;
    }

    public float getCargoCapacity() {
        return cargoCapacity;
    }

    public void setCargoCapacity(float cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }
}

PickupCar.java

package com.meteor.dada.vo;

/**
 * 皮卡车类
 *
 * @author 刘佳星
 * @version v1.0
 */
public class PickupCar extends Car{
    private int busload;//载客量
    private float cargoCapacity;//载货量

    public PickupCar() {
    }

    public PickupCar(int id, String name, int rent, int busload, float cargoCapacity) {
        super(id, name, rent);
        this.busload = busload;
        this.cargoCapacity = cargoCapacity;
    }

    public int getBusload() {
        return busload;
    }

    public void setBusload(int busload) {
        this.busload = busload;
    }

    public float getCargoCapacity() {
        return cargoCapacity;
    }

    public void setCargoCapacity(float cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }
}

DaDaRentalCar.java

package com.meteor.dada.service;

import com.meteor.dada.vo.Car;
import com.meteor.dada.vo.PassengerCar;
import com.meteor.dada.vo.PickupCar;
import com.meteor.dada.vo.TruckCar;

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

/**
 * 达达租车系统
 *
 * @author 刘佳星
 * @version v1.0
 */
public class DaDaRentalCar {

    /**
     * 初始化车辆数据
     * @return 车辆信息
     */
    private List<Car> initialCar() {
        Car[] cars = new Car[]{new PassengerCar(1, "奥迪A4", 500, 4), new PassengerCar(2, "马自达6", 400, 4),
                new PickupCar(3, "皮卡雪6", 450, 4, 2f), new PassengerCar(4, "金龙", 800, 20),
                new TruckCar(5, "松花江", 400, 4), new TruckCar(6,"依维柯",1000,20)};
        return Arrays.asList(cars);
    }

    public static void main(String[] args) {
        while (true) {
            DaDaRentalCar daDaRentalCar = new DaDaRentalCar();
            List<Car> cars = daDaRentalCar.initialCar();//构造车辆数据
            System.out.println("欢迎使用达达租车系统:"+cars.size());
            System.out.println("您是否要租车:1是 0否");
            Scanner input = new Scanner(System.in);
            int rent = input.nextInt();
            if (rent != 0) {
                System.out.println("您可租车的类型及其价目表:");
                System.out.println("序号\t" + "汽车名称\t\t" + "租金\t\t" + "容量");
                for (Car car : cars) {
                    if (car instanceof PassengerCar) {
                        System.out.println(car.getId() + "\t" + car.getName() + "\t\t" + car.getRent() + "\t\t" + "载人:" + ((PassengerCar) car).getBusload() + "人");
                    }
                    if (car instanceof TruckCar) {
                        System.out.println(car.getId() + "\t" + car.getName() + "\t\t" + car.getRent() + "\t\t" + "载货:" + ((TruckCar) car).getCargoCapacity() + "吨");
                    }
                    if (car instanceof PickupCar) {
                        PickupCar pickupCar = (PickupCar) car;
                        System.out.println(car.getId() + "\t" + car.getName() + "\t\t" + car.getRent() + "\t\t" + "载人:" + pickupCar.getBusload() + "人" + " 载货:" + pickupCar.getCargoCapacity() + "吨");
                    }
                }
                System.out.println("请输入您要租汽车的数量:");
                int num = input.nextInt();
                List<Car> passCars = new ArrayList<>();//载客
                List<Car> TruckCars = new ArrayList<>();//载货
                float totalprice = 0;
                int totalBusload = 0;
                int totalCargoCapacity = 0;
                for (int i = 0; i < num; i++) {
                    System.out.println("请输入第" + (i + 1) + "辆车的序号:");
                    int carNum = input.nextInt();
                    if (carNum < 1 || carNum >cars.size()) {
                        System.out.println("请输入规定范围内的序号");
                        carNum = input.nextInt();
                    }
                    for (Car car : cars) {
                        if (car.getId() == carNum) {
                            totalprice += car.getRent();//总金额
                            if (car instanceof PassengerCar) {
                                PassengerCar passengerCar = (PassengerCar) car;
                                totalBusload += passengerCar.getBusload();//总载客量
                                passCars.add(passengerCar);//添加到载人车列表
                            }
                            if (car instanceof TruckCar) {
                                TruckCar truckCar = (TruckCar) car;
                                totalCargoCapacity += truckCar.getCargoCapacity();//总载货量
                                TruckCars.add(truckCar);//添加到载货车列表
                            }
                            if (car instanceof PickupCar) {
                                PickupCar pickupCar = (PickupCar) car;
                                totalBusload += pickupCar.getBusload();
                                totalCargoCapacity += pickupCar.getCargoCapacity();
                                passCars.add(pickupCar);
                                TruckCars.add(pickupCar);
                            }
                        }
                    }
                }
                System.out.println("请输入租车天数:");
                int days = input.nextInt();
                System.out.println("您的账单:");
                System.out.println("***可载人的车有:");
                for (Car car : passCars) {
                    System.out.print(car.getName() + "\t");
                }
                System.out.println("共载人:" + totalBusload + "人");
                System.out.println("***可载货的车有:");
                for (Car car : TruckCars) {
                    System.out.print(car.getName() + "\t");
                }
                System.out.println("共载货:" + totalCargoCapacity + "吨");
                System.out.println("***租车总价格:" + totalprice * days + "元");

            } else {
                System.out.println("感谢您使用达达租车系统!");
            }
        }
    }
}
点击查看更多内容
TA 点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消