手记

Java入门第二季 租车系统


二、代码实现:
(1)基础版(控制台调试):
car类:

package com.xiaoyao.car;
import java.util.List;
public class Car{
    private int id;//租车序号
    private int typeId;//租车分类下的序号
    private String name;//租车名字
    private double price;//租车单价
    private int personLoad;//载客量;
    private double goodsLoad;//载货量;
    private List<Car> carList;
    public int getId() {
        return id;
}
public void setId(int id) {
    this.id = id;
    }
    public int getTypeId() {
        return typeId;
    }
    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public int getPersonLoad() {
        return personLoad;
    }
    public void setPersonLoad(int personLoad) {
        this.personLoad = personLoad;
    }
    public double getGoodsLoad() {
        return goodsLoad;
    }
    public void setGoodsLoad(double goodsLoad) {
        this.goodsLoad = goodsLoad;
    }
    public List<Car> getCarList() {
        return carList;
    }
    public void setCarList(List<Car> carList) {
        this.carList = carList;
    }
}

carWithPerson类:

package com.xiaoyao.car;
import java.util.List;
public class CarWithPerson extends Car {
    private List<CarWithPerson> personCarList;  
    public CarWithPerson(int id, int typeId, String carName, double carPrice,int withPerson) {
        // TODO Auto-generated constructor stub
        super.setId(id);
        super.setTypeId(typeId);
        super.setName(carName);
        super.setPrice(carPrice);
        super.setPersonLoad(withPerson);
    }
    public List<CarWithPerson> getPersonCarList() {
        return personCarList;
    }
    public void setPersonCarList(List<CarWithPerson> personCarList) {
        this.personCarList = personCarList;
    }
}

carWithGoods类:

package com.xiaoyao.car;
import java.util.List;
public class CarWithGoods extends Car {
    private List<CarWithGoods> goodsCarList;
    public CarWithGoods(int id, int typeId, String carName, double carPrice, double withGoods) {
        // TODO Auto-generated constructor stub
        super.setId(id);
        super.setTypeId(typeId);
        super.setName(carName);
        super.setPrice(carPrice);
        super.setGoodsLoad(withGoods);
    }
    public List<CarWithGoods> getGoodsCarList() {
        return goodsCarList;
    }
    public void setGoodsCarList(List<CarWithGoods> goodsCarList) {
        this.goodsCarList = goodsCarList;
    }
}

carWithPersonAndGoods类:

package com.xiaoyao.car;
import java.util.List;

public class CarWithGoods extends Car {
    private List<CarWithGoods> goodsCarList;
    public CarWithGoods(int id, int typeId, String carName, double carPrice, double withGoods) {
        // TODO Auto-generated constructor stub
        super.setId(id);
        super.setTypeId(typeId);
        super.setName(carName);
        super.setPrice(carPrice);
        super.setGoodsLoad(withGoods);
    }
    public List<CarWithGoods> getGoodsCarList() {
        return goodsCarList;
    }
    public void setGoodsCarList(List<CarWithGoods> goodsCarList) {
        this.goodsCarList = goodsCarList;
    }
}

test类

package com.xiaoyao.car;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class test extends Car {
    public static void main(String[] args) {
        // 问答:
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎使用租车系统,您是否要租车?");
        System.out.println("是:请输入1;否:请输入0;");
        int num = sc.nextInt();
        if (num != 1) {
            System.out.println("欢迎下次光临");
            System.exit(0);
        } else {
        }
        // 输入租车时间:
        System.out.println("请输入租车天数");
        int days = sc.nextInt();        
        // 选择车辆类型:
        System.out.println("请选择车辆类型:0:全部显示;1:载人车;2:载货车;3:皮卡车;");
        int carType = sc.nextInt();
        // 输出可租车信息:
        System.out.println("您可租车的类型及其价目表:");
        System.out.println("序号   汽车名称     租金     容量");
        // 录入车辆信息:
        LinkedList<CarWithPerson> personCar = new LinkedList<CarWithPerson>();
        LinkedList<CarWithGoods> goodsCar = new LinkedList<CarWithGoods>();
        LinkedList<CarWithPersonAndGoods> personAndGoodsCar = new LinkedList<CarWithPersonAndGoods>();
        LinkedList<Car> cars = new LinkedList<Car>();
        CarWithPerson personCar1 = new CarWithPerson(1, 1, "奥迪A4", 500.0, 4),
        personCar2 = new CarWithPerson(2, 2,"马自达6", 400.0, 4),
        personCar3 = new CarWithPerson(3, 3,"金龙", 800.0, 20);
        CarWithGoods freightCar1 = new CarWithGoods(4, 1,"松花江", 400.0, 4.0),
        freightCar2 = new CarWithGoods(5, 2,"依维河", 1000.0, 20.0);
        CarWithPersonAndGoods personAndFreightCar1 = new CarWithPersonAndGoods(6, 1,"皮卡雪6", 450.0, 4, 2.0);
        goodsCar.add(freightCar1);
        goodsCar.add(freightCar2);
        personCar.add(personCar1);
        personCar.add(personCar2);
        personCar.add(personCar3);
        personAndGoodsCar.add(personAndFreightCar1);        
        cars.addAll(personCar);
        cars.addAll(goodsCar);
        cars.addAll(personAndGoodsCar);     
        int carsNumber = cars.size();
        int goodsCarNumber = goodsCar.size();
        int personCarNumber = personCar.size();
        int personAndGoodsCarNumber = personAndGoodsCar.size();
                //展示车辆
        switch (carType) {
        case 0:
            // 全部展示:
            System.out.println("共有" + carsNumber + "辆可租车");
            for (int i = 0; i < carsNumber; i++) {
                Car allCar = cars.get(i);
                System.out.println(allCar.getId()+ ".  " + "  " + allCar.getName() + "  " + allCar.getPrice() + "元/天    " + allCar.getPersonLoad() + "人  " + allCar.getGoodsLoad()+"吨 ");
            }
            System.out.println("请选择车辆序号:");
            int carId = sc.nextInt();
            int thereId = carId-1;
            System.out.println("请输入租车数量:");
            int number = sc.nextInt();          
            System.out.println("订单");
            System.out.println("车辆:" + cars.get(thereId).getName()+ "\n数量:" + number + "辆;\n单价:" + cars.get(thereId).getPrice() + "元/天;\n天数:" + days+"天; \n总载货:"+ cars.get(thereId).getGoodsLoad()*number + "吨  \n总载人: "+cars.get(thereId).getPersonLoad()*number+"人");
            System.out.println("合计总价:"+cars.get(thereId).getPrice()*days*number + "元。");
            break;
        case 1:
             //展示载客车:
                System.out.println("共有" + personCarNumber + "辆载客车");
                for (int i = 0; i < personCarNumber; i++) {
                    Car allCar = cars.get(i);
                    System.out.println((allCar.getId())+ ".\t" +allCar.getName() + "\t\t" + allCar.getPrice() + "元/天   \t" + allCar.getPersonLoad() + "人  \t ");
                }
                System.out.println("请选择车辆序号:");
                int carId1 = sc.nextInt();          
                int thereId1 = carId1-1;
                System.out.println("请输入租车数量:");
                int number1 = sc.nextInt();         
                System.out.println("订单");
                System.out.println("车辆:" + cars.get(thereId1).getName()+ "\n数量:" + number1 + "辆;\n单价:" + cars.get(thereId1).getPrice() + "元/天;\n天数:" + days+"天; \n总载人:"+cars.get(thereId1).getPersonLoad()*number1+"人");
                System.out.println("合计总价:"+cars.get(thereId1).getPrice()*days*number1 + "元。");
            break;
        case 2:
                // 展示载货车:
                System.out.println("共有" + goodsCarNumber + "辆载货车");
                for (int i = personCarNumber; i < goodsCarNumber+personCarNumber; i++) {
                    Car allCar = cars.get(i);
                    System.out.println((allCar.getId()-personCarNumber)+ ".\t" +allCar.getName() + "\t\t" + allCar.getPrice() + "元/天   \t" + allCar.getGoodsLoad() + "吨  \t ");
                }
                System.out.println("请选择车辆序号:");
                int carId2 = sc.nextInt();
                int thereId2 = carId2-1+personCarNumber;            
                System.out.println("请输入租车数量:");
                int number2 = sc.nextInt();
                System.out.println("订单");
                System.out.println("车辆:" + cars.get(thereId2).getName()+ "\n数量:" + number2 + "辆;\n单价:" + cars.get(thereId2).getPrice() + "元/天;\n天数:" + days+"天; \n总载货:"+ cars.get(thereId2).getGoodsLoad()*number2 + "吨  \t ");
                System.out.println("合计总价:"+cars.get(thereId2).getPrice()*days*number2 + "元。");
            break;
        case 3:
            // 展示皮卡车:
                System.out.println("共有" + personAndGoodsCarNumber + "辆皮卡车");
                for (int i = goodsCarNumber+personCarNumber; i < personCarNumber+goodsCarNumber+personAndGoodsCarNumber; i++) {
                    Car allCar = cars.get(i);
                    System.out.println((allCar.getId()-goodsCarNumber-personCarNumber)+ ".\t" +allCar.getName() + "\t\t" + allCar.getPrice() + "元/天   \t" + allCar.getPersonLoad() + "人  \t "+ allCar.getGoodsLoad() + "吨  \t ");
                }
                System.out.println("请选择车辆序号:");
                int carId3 = sc.nextInt();
                int thereId3 = carId3-1+personCarNumber+goodsCarNumber;         
                System.out.println("请输入租车数量:");
                int number3 = sc.nextInt();
                System.out.println("订单");
                System.out.println("车辆:" + cars.get(thereId3).getName()+ "\n数量:" + number3 + "辆;\n单价:" + cars.get(thereId3).getPrice() + "元/天;\n时间:" + days+"天; \n总载人:"+cars.get(thereId3).getPersonLoad()*number3+"人;  \n总载货:"+cars.get(thereId3).getGoodsLoad()*number3+"吨;");
                System.out.println("合计总价:"+cars.get(thereId3).getPrice()*days*number3 + "元。");
            break;
        default:
            System.out.println("请正确输入指令:1 or 2 or 3");
        }
    }
}

结果展示:
全部车:

分类展示车:

(2)修改版(页面展示):

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