版本:V1.0.2018.2.22
答答租车系统:
- 数据模型分析:考虑了继承和多态的特性,创建了父类Vehicle,子类CarPeople(载人)、CarGoods(载货)、CarPeoGo(载人和载货)
- 业务模型分析:考虑不同选车、租车、统计数据等场景,在Initial类中创建了initialVehicle(初始化车辆信息)、rentalInfo(租车信息处理-车名、金额计算等)
- 显示和流程分析:在Initial类中引入了play(租车系统执行-初始化信息、租车信息录入、租车信息处理、账单输出)
代码内容
Initial类
package taxi;
import java.util.Scanner;
public class Initial {
Scanner in = new Scanner(System.in);
Vehicle [] carShow =initialVehicle(); //用于存放车辆信息的对象
//车辆信息的初始化
public Vehicle[] initialVehicle() {
Vehicle [] car = {
new CarPeople(1, "奥迪A4", 500, 4),
new CarPeople(2, "马自达6", 400, 4),
new CarPeoGo(3, "皮卡雪6", 450, 4, 2),
new CarPeople(4, "金龙", 800, 20),
new CarGoods(5, "松花江", 400, 4),
new CarGoods(6, "依维柯", 1000, 20)
};
return car;
}
//租车信息处理
public void rentalInfo() {
//租车数量环节
System.out.println("请输入您需要租汽车的数量");
int rentalNum = in.nextInt(); //租车数量
int [] rentalNo = new int[rentalNum]; //选择租的每一辆车序号
for(int i=0 ; i<rentalNo.length ; i++) {
System.out.println("请输入第"+(i+1)+"辆车的序号:");
rentalNo[i]=in.nextInt();
}
//租车天数环节
System.out.println("请输入租车天数:");
int rentalDay = in.nextInt(); //租车天数
//载货和载人的车和总计容量计算
StringBuffer CarNamePeo = new StringBuffer(); //可载人的车的名字输出
StringBuffer CarNameGo = new StringBuffer(); //可载货的车的名字输出
int sumPeo=0; //总载人数
double sumGo=0.0; //总载货书
for(int i=0 ; i<rentalNo.length ; i++) //载人和载货车名组合,以及容量的计算
{
//可载人的车处理
if(carShow[rentalNo[i]-1] instanceof CarPeople || carShow[rentalNo[i]-1] instanceof CarPeoGo)
{
CarNamePeo.append(carShow[rentalNo[i]-1].getCarName());
CarNamePeo.append(" ");
sumPeo+=carShow[rentalNo[i]-1].getPersonsCap();
}
//可载货的车处理
if(carShow[rentalNo[i]-1] instanceof CarGoods || carShow[rentalNo[i]-1] instanceof CarPeoGo)
{
CarNameGo.append(carShow[rentalNo[i]-1].getCarName());
CarNameGo.append(" ");
sumGo+=carShow[rentalNo[i]-1].getGoodsCap();
}
}
//租车金额计算
double sumRental = 0.0;
for(int k : rentalNo) {
sumRental+=carShow[k-1].getCarRental();
}
//账单输出环节
System.out.println("您的帐单:");
System.out.println("***可载人的车有:");
System.out.println(CarNamePeo+" 共载人:"+sumPeo+"人");
System.out.println("***可载货的车有:");
System.out.println(CarNameGo+" 共载货:"+sumGo+"吨");
System.out.println("***租车总价格:"+(sumRental*rentalDay)+"元");
in.close();
}
//租车系统执行
public void Play() {
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
int wp = in.nextInt();
while(true)
{
if(wp==1)
{
System.out.println("您可租车的类型及其价目表");
System.out.println("序号\t汽车名称\t租金\t容量");
for(int k=0; k<carShow.length;k++) {
System.out.println(carShow[k]); //可以租的车信息输出
}
rentalInfo(); //租车信息处理环节
break;
}
else
{
System.out.println("系统已退出");
break;
}
}
in.close();
}
public static void main(String[] args) {
Initial initial = new Initial();
initial.Play(); //租车系统启动
System.out.println("处理完毕,系统已退出");
}
}
Vehicle类
package taxi;
public class Vehicle {
//属性
private int number;
private String carName;
private int carRental;
//get属性的方法
public int getNumber() {
return number;
}
public String getCarName() {
return carName;
}
public int getCarRental() {
return carRental;
}
public int getPersonsCap() {
return 0;
}
public int getGoodsCap() {
return 0;
}
//构造方法
public Vehicle(int number, String carName, int carRental) {
super();
this.number = number;
this.carName = carName;
this.carRental = carRental;
}
//重写toString方法以便输出对象信息
@Override
public String toString() {
// TODO Auto-generated method stub
return (number+".\t"+carName+"\t"+carRental+"元/天\t");
}
}
CarPeople类继承Vehicle类
package taxi;
public class CarPeople extends Vehicle {
private int personsCap;
public CarPeople(int number, String carName, int carRental,int personCap) {
super(number, carName, carRental);
// TODO Auto-generated constructor stub
this.personsCap = personCap;
}
public int getPersonsCap() {
return personsCap;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return (super.toString()+"载人:"+personsCap+"人"+"\t");
}
}
CarGoods类继承Vehicle类
package taxi;
public class CarGoods extends Vehicle {
private int goodsCap;
public CarGoods(int number, String carName, int carRental , int goodsCap) {
super(number, carName, carRental);
// TODO Auto-generated constructor stub
this.goodsCap=goodsCap;
}
public int getGoodsCap() {
return goodsCap;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return (super.toString()+" 载货:"+goodsCap+"吨"+"\t");
}
}
CarPeoGo类继承Vehicle类
package taxi;
public class CarPeoGo extends Vehicle {
private int personsCap;
private int goodsCap;
public CarPeoGo(int number, String carName, int carRental , int personsCap , int goodsCap) {
super(number, carName, carRental);
// TODO Auto-generated constructor stub
this.personsCap = personsCap;
this.goodsCap = goodsCap;
}
public int getPersonsCap() {
return personsCap;
}
public int getGoodsCap() {
return goodsCap;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return (super.toString()+"载人:"+personsCap+"人"+" 载货:"+goodsCap+"吨"+"\t");
}
}
输出结果:
遗留问题
- 异常情况的处理:输入非整数、负数等情况;
- 业务模型部分:数据与操作的耦合度高。
请各位大神指点,谢谢!
点击查看更多内容
4人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦