根據(jù)所學知識放吩,編寫一個控制臺版的租車系統(tǒng)
功能:1智听、 展示所有可租車輛
2、 選擇車型渡紫、租車輛
3到推、 展示租車清單,包含:總金額惕澎、總載貨量及其車型莉测、總載人量及其車型
代碼參考imooc中[JavaEE]課程Demo編寫
父類Car:
package com.RentCar;
public class Car {
String name;
int money;
int people;
int goods;
public String getName() {
return name;
}
public int getMoney() {
return money;
}
public int getPeople() {
return people;
}
public int getGoods() {
return goods;
}
}
Car 的子類——載人汽車CarPeople:
package com.RentCar;
public class CarPeople extends Car{
String name;
int money;
int people;
public CarPeople(String name,int money,int people){
this.name = name;
this.money = money;
this.people = people;
}
@Override
public String toString() {
return this.name + "" + this.money + this.people;
}
public String getName() {
return name;
}
public int getMoney() {
return money;
}
public int getPeople() {
return people;
}
}
Car 的子類——載貨汽車CarGoods:
package com.RentCar;
public class CarGoods extends Car {
String name;
int money;
int goods;
public CarGoods(String name,int money,int goods){
this.name = name;
this.money = money;
this.goods = goods;
}
@Override
public String toString() {
return this.name + this.money + this.goods;
}
public String getName() {
return name;
}
public int getMoney() {
return money;
}
public int getGoods() {
return goods;
}
}
Car 的子類——載人載貨CarAll:
package com.RentCar;
public class CarAll extends Car {
String name;
int money;
int people;
int goods;
public CarAll(String name,int money,int people,int goods){
this.name = name;
this.money = money;
this.people = people;
this.goods = goods;
}
@Override
public String toString() {
return this.name + this.money + this.people + this.goods;
}
public String getName() {
return name;
}
public int getMoney() {
return money;
}
public int getPeople() {
return people;
}
public int getGoods() {
return goods;
}
}
主函數(shù):
package com.RentCar;
import java.util.Scanner;
public class CarRentingSystem {
public static void main(String[] args) {
// TODO 自動生成的方法存根
while (true) {
System.out.println("你到底要不要租虎哥的車?(租/不租)");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
if(input.equals("租")){
Car[] cars = {new CarPeople("奧迪A4",400,4),new CarPeople("馬自達",400,4),new CarAll("皮卡6",450,4,20), new CarPeople("金龍",800,20), new CarGoods("松花江",400,4),new CarGoods("依維柯",1000,20)
};
System.out.println("下面是您能租的車:");
System.out.println("序號 品牌 價格 載人/貨量");
for(int i = 0;i < cars.length;i++){
System.out.printf("%-3d %-6s% 6d %3d個人唧喉,%d噸貨",i+1,cars[i].getName(),cars[i].getMoney(),cars[i].getPeople(),cars[i].getGoods());
}
System.out.println("你想要租幾輛暗仿薄:");
int number = scanner.nextInt();
Car[] chooseCars = new Car[number];
Car[] chooseCarsPeople = new Car[number];
int CCPNum = 0;//選擇的載人汽車的數(shù)目
Car[] chooseCarsGoods = new Car[number];
int CCGNum = 0;//選擇的載貨汽車的數(shù)目
//定義總共的錢數(shù)忍抽,載客量,載貨量
int money = 0;
int people = 0;
int goods = 0;
for(int i = 0,j = 1;i < number;i++,j++){
System.out.println("請輸入第" + j + "輛車的序號:");
int carNum = scanner.nextInt() - 1;
try { chooseCars[i] = cars[carNum];
money += chooseCars[i].getMoney();
people += chooseCars[i].getPeople();
goods += chooseCars[i].getGoods();
if(chooseCars[i].getPeople() != 0){
chooseCarsPeople[CCPNum] = chooseCars[i]; CCPNum++;
}
if(chooseCars[i].getGoods() != 0){
chooseCarsGoods[CCGNum] = chooseCars[i]; CCGNum++;
}
}
catch (Exception e) {
System.out.println("說了幾遍了董朝,輸入1~6的序號梯找!\n"); i--; j--;
}
}
System.out.println("你想要租幾天啊:");
int dayNum = scanner.nextInt();
System.out.printf("你一共花了:%d元\n您總共租了%d輛車益涧,租賃%d天\n",money*dayNum,number,dayNum);
System.out.println("其中锈锤,載人的車有" + CCPNum + "輛");
try { for(Car CarsPeople:chooseCarsPeople){
System.out.println(CarsPeople.getName());
}
}
catch (Exception e) {
}
System.out.printf("總載客量:%d人\n",people*dayNum);
System.out.println("載貨的車有" + CCGNum + "輛");
try { for(Car CarsGoods:chooseCarsGoods){
System.out.println(CarsGoods.getName());
}
}
catch (Exception e) { }
System.out.printf("總載貨量:%d噸\n",goods*dayNum);
break;
}
else if(input.equals("不租")){
System.out.println("不租你過來干嘛!");
break;
}
else {
System.out.println("你輸錯了大兄弟闲询!\n");
}
}
System.out.println("慢走不送久免!");
}
}
關(guān)注作者獲取更多資料。