这是我在学完java入门第二季之后编写的答答租车系统源码,相较于老师的给的流程,增加了用户输入错误字符时的提示。
主函数CarRentingSystem.java:
import java.util.Scanner;
/**
* Time 16-5-19
*
* 汽车租赁系统
*/
public class CarRentingSystem {
public static void main(String args[]){
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吨货\n",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;//选择的载人汽车的数目
Car[] chooseCarsGoods = new Car[number];
int CCGNum = 0;//选择的载货汽车的数目
//定义总共的钱数,载客量,载货量
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("不是")){
break;
}else {
System.out.println("请输入正确的选项!\n");
}
}
System.out.println("感谢您的光临,欢迎下次再来!");
}
}
所有类型汽车的父类Car.java
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.java
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.java
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.java
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;
}
}
实现结果:
请问您是要租车吗?(是/不是)
输入非法字符的情况:
请输入正确的选项!
请问您是要租车吗?(是/不是)
是
以下是您可以租的车:
序号 品牌 价格 载人/货量
1 奥迪A4 400 4个人,0吨货
2 马自达 400 4个人,0吨货
3 皮卡6 450 4个人,20吨货
4 金龙 800 20个人,0吨货
5 松花江 400 0个人,4吨货
6 依维柯 1000 0个人,20吨货
请输入您要租的数量:
6
请输入第1辆车的序号(1~6):
1
请输入第2辆车的序号(1~6):
2
请输入第3辆车的序号(1~6):
3
请输入第4辆车的序号(1~6):
4
请输入第5辆车的序号(1~6):
5
请输入第6辆车的序号(1~6):
6
请输入您要租的天数:
1
您的总费用:3450元
您总共租了6辆车,租赁1天
其中,载人的车有4辆
奥迪A4
马自达
皮卡6
金龙
总载客量:32人
载货的车有3辆
皮卡6
松花江
依维柯
总载货量:44吨
感谢您的光临,欢迎下次再来!
Process finished with exit code 0
热门评论
我这个大一新手居然看得懂😁👍