Car.java
package com.imooc;
public class Car {
public String name;
public int cost;
public int passengerLoad = 0;
public int cargoCapacity = 0;
}
DadaCarRental.java
package com.imooc;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DadaCarRental {
public static int charToNum(int ch){
int num = ch - 48;
return num;
}
public static void chooseCar(){
Car od = new Car();
od.name = "奥迪A4";
od.cost = 500;
od.passengerLoad = 4;
Car mzd = new Car();
mzd.name = "马自达6";
mzd.cost = 400;
mzd.passengerLoad = 4;
Car pkx = new Car();
pkx.name = "皮卡雪6";
pkx.cost = 450;
pkx.passengerLoad = 4;
pkx.cargoCapacity = 2;
Car jl = new Car();
jl.name = "金龙";
jl.cost = 800;
jl.passengerLoad = 20;
Car shj = new Car();
shj.name = "松花江";
shj.cost = 400;
shj.cargoCapacity = 4;
Car ywk = new Car();
ywk.name = "依维柯";
ywk.cost = 1000;
ywk.cargoCapacity = 20;
ArrayList<Car> list = new ArrayList<>();
list.add(od);
list.add(mzd);
list.add(pkx);
list.add(jl);
list.add(shj);
list.add(ywk);
System.out.println("您可租车的类型及价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
for(int i = 0; i < list.size(); i++) {
Car nowCar = list.get(i);
// System.out.println(nowCar.name);
System.out.print((i+1) + ".\t" +nowCar.name + "\t" + nowCar.cost + "元/天\t");
if(nowCar.passengerLoad != 0) {
System.out.print("载人:"+nowCar.passengerLoad+"\t");
}
if(nowCar.cargoCapacity != 0){
System.out.print("载货:"+nowCar.cargoCapacity);
}
System.out.println();
}
System.out.println("请输入您要租汽车的数量:");
try{
int num = System.in.read();
int ch = System.in.read();
int rentNum = charToNum(num);
ArrayList<Car> rentList = new ArrayList<>();
for(int x = 0; x < rentNum; x++){
System.out.println("请输入第"+(x+1)+"辆车的序号:");
int indexCh = System.in.read();
int ch2 = System.in.read();
int index = charToNum(indexCh) - 1;
Car nowCar = list.get(index);
rentList.add(nowCar);
}
int passengerLoad = 0;
int cargoCapacity = 0;
int totalCost = 0;
System.out.print("您的账单:\n***可载人的车有:\n");
for(int y = 0; y < rentList.size(); y++){
Car nowCar = list.get(y);
if(nowCar.passengerLoad != 0){
passengerLoad += nowCar.passengerLoad;
}
if(nowCar.cargoCapacity != 0){
cargoCapacity += nowCar.cargoCapacity;
}
totalCost += nowCar.cost;
System.out.print(nowCar.name + "\t");
}
System.out.println("共载人:" + passengerLoad + "人");
System.out.println("***载货的车有:\n共载货:" + cargoCapacity + "吨");
System.out.println("***租车总价格:" + totalCost + "元");
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){
try{
System.out.println("欢迎使用答答租车系统:\n您是否要租车:1是,0否");
int isToRent = System.in.read();
int ch = System.in.read();
if(charToNum(isToRent) == 1){
chooseCar();
}else {
System.exit(1);
}
}catch (IOException e){
e.printStackTrace();
}
}
}