ITPub博客

首页 > IT职业 > IT图书 > Java入门第二季 租车系统

Java入门第二季 租车系统

IT图书 作者:flyingknife 时间:2021-09-09 09:04:46 0 删除 编辑

图片描述
二、代码实现:
(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 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 getCarList() {
        return carList;
    }
    public void setCarList(List carList) {
        this.carList = carList;
    }
}

carWithPerson类:

package com.xiaoyao.car;
import java.util.List;
public class CarWithPerson extends Car {
    private List 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 getPersonCarList() {
        return personCarList;
    }
    public void setPersonCarList(List personCarList) {
        this.personCarList = personCarList;
    }
}

carWithGoods类:

package com.xiaoyao.car;
import java.util.List;
public class CarWithGoods extends Car {
    private List 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 getGoodsCarList() {
        return goodsCarList;
    }
    public void setGoodsCarList(List goodsCarList) {
        this.goodsCarList = goodsCarList;
    }
}

carWithPersonAndGoods类:

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

public class CarWithGoods extends Car {
    private List 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 getGoodsCarList() {
        return goodsCarList;
    }
    public void setGoodsCarList(List 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 personCar = new LinkedList();
        LinkedList goodsCar = new LinkedList();
        LinkedList personAndGoodsCar = new LinkedList();
        LinkedList cars = new LinkedList();
        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 

结果展示:
全部车:
图片描述
分类展示车:
图片描述
(2)修改版(页面展示):

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/393/viewspace-2807927/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-07-23

  • 博文量
    0
  • 访问量
    464