答答租车系统
相关说明
Car.java
PCar.java
CCar.java
PCCar.java
CarSystem.java
效果演示(请点击图片链接浏览)
相关说明
- Car为父类,定义共有属性和方法print();
- 子类分别改写方法print();
- 在main方法中增加了对输入内容的判定和异常的捕捉及解决,保证了输入内容的合理性;
- 优化了账单输出式样。
Car.java
package com.jeffli;
public class Car {
protected int id;
protected String name;
protected float price;
protected int capPerson;
protected float capCargo;
public void print(){}
}
PCar.java
package com.jeffli;
public class PCar extends Car {
public PCar(int id, String name, float price, int capPerson) {
this.id = id;
this.name = name;
this.price = price;
this.capPerson = capPerson;
}
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println(id + ".\t\t" + name + "\t\t" + price+ "元/天" + "\t\t" + "载人:" + capPerson + "人");
}
}
CCar.java
package com.jeffli;
public class CCar extends Car {
public CCar(int id, String name, float price, float capCargo) {
this.id = id;
this.name = name;
this.price = price;
this.capCargo = capCargo;
}
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println(id + ".\t\t" + name + "\t\t" + price+ "元/天" + "\t\t" + "载货:" + capCargo + "吨");
}
}
PCCar.java
package com.jeffli;
public class PCCar extends Car{
public PCCar(int id, String name, float price, int capPerson, float capCargo) {
this.id = id;
this.name = name;
this.price = price;
this.capPerson = capPerson;
this.capCargo = capCargo;
}
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println(id + ".\t\t" + name + "\t\t" + price+ "元/天" + "\t\t" + "载人:" + capPerson + "人" + " " + "载货:" + capCargo + "吨");
}
}
CarSystem.java
package com.jeffli;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class CarSystem {
public static void main(String[] args) {
Car[] cars = {new PCar(1, "奥迪A4", 500f, 4), new PCar(2, "马自达6", 400f, 4),
new PCCar(3, "皮卡雪6", 450f, 4, 2f), new PCar(4, "金龙", 800f, 20),
new CCar(5, "松花江", 400f, 4f), new CCar(6, "依维柯", 1000f, 20f)};
System.out.println("------------欢迎使用答答租车系统!------------");
System.out.println("您是否要租车?\n是:请输入1\t\t否:请按任意键退出");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
if (input.equals("1") == false) {
System.out.println("感谢您的使用,下次再见(^_^)");
scanner.close();
} else if (input.equals("1")) {
System.out.println("您可租用的车型及其价目为:");
System.out.println("序号\t汽车名称\t租金\t\t\t"+ "容量");
for (Car car : cars) {
if (car instanceof PCar) {
car.print();
}
if (car instanceof CCar) {
car.print();
}
if (car instanceof PCCar) {
car.print();
}
}
System.out.println();
float sum = 0f;
float capC = 0f;
int capP = 0;
List<String> namesP = new ArrayList<>();
List<String> namesC = new ArrayList<>();
outer:
for (int i = 1; ; i++) {
int index = 0;
int num = 0;
int day = 0;
while (true) {
System.out.print("请输入您要租用的第" + i + "辆车的序号:");
int in = 0;
try {
in = Integer.parseInt(scanner.next());
} catch (NumberFormatException e) {
System.out.println("【您输入的信息有误,请重新输入!】");
continue;
}
if (in < 1 || in > cars.length) {
System.out.println("【您输入的信息有误,请重新输入!】");
continue;
}
index = in - 1;
break;
}
while (true) {
System.out.print("请输入您要租用的数量:");
int in = 0;
try {
in = Integer.parseInt(scanner.next());
} catch (NumberFormatException e) {
System.out.println("【您输入的信息有误,请重新输入!】");
continue;
}
if (in < 1) {
System.out.println("【您输入的信息有误,请重新输入!】");
continue;
}
num = in;
break;
}
while (true) {
System.out.print("请输入您要租用的天数:");
int in = 0;
try {
in = Integer.parseInt(scanner.next());
} catch (NumberFormatException e) {
System.out.println("【您输入的信息有误,请重新输入!】");
continue;
}
if (in < 1) {
System.out.println("【您输入的信息有误,请重新输入!】");
continue;
}
day = in;
break;
}
sum += cars[index].price * num * day;
capC += cars[index].capCargo * num;
capP += cars[index].capPerson* num;
if (cars[index].capCargo != 0f) {
namesC.add(cars[index].name);
}
if (cars[index].capPerson != 0) {
namesP.add(cars[index].name);
}
while (true) {
System.out.println("\n您是否需要继续租车?\n是:请输入1\t\t否:请输入0");
int in = scanner.nextInt();
if (in == 1) {
continue outer;
} else if (in == 0) {
break outer;
} else {
System.out.println("您输入的信息有误,请重新输入:");
continue;
}
}
}
System.out.println("----------------------------------------------------------------------\n\n租车成功!下面是您的账单:\n");
System.out.println("------------------------------载客车------------------------------\n");
if (namesP.isEmpty()) {
System.out.print("无");
} else {
for (int i = 0; i < namesP.size(); i++) {
System.out.print(namesP.get(i) + "\t\t");
}
}
System.out.println();
System.out.println("共载人:" + capP + "人\n");
System.out.println("------------------------------载货车------------------------------\n");
if (namesC.isEmpty()) {
System.out.print("无");
} else {
for (int i = 0; i < namesC.size(); i++) {
System.out.print(namesC.get(i) + "\t\t");
}
}
System.out.println();
System.out.println("共载货:" + capC + "吨\n");
System.out.println("----------------------------------------------------------------------\n");
System.out.println("总计支付:" + sum + "元\n");
System.out.println("----------------------------------------------------------------------\n\n");
System.out.println("账单已完成,请按任意键退出:");
scanner.next();
System.out.println("感谢您的使用,下次再见(^_^)");
scanner.close();
}
}
}
效果演示(请点击图片链接浏览)
http://img1.sycdn.imooc.com//587e099b0001f86218560940.png
http://img1.sycdn.imooc.com//587e0a3a0001e4bd18520937.png
http://img1.sycdn.imooc.com//587e0a2a0001119418550938.png
点击查看更多内容
13人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦