java集合租车_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 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 < 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)修改版(頁面展示):
總結
以上是生活随笔為你收集整理的java集合租车_Java入门第二季 租车系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中配置bean_Spring中基
- 下一篇: java递归深度克隆_递归方法实现深度克