标签:java
-一、需求分析
-二、
-一、需求分析与数据表设计
需求图:
01.moto类(汽车父类):
01.1:bus类(客车类)
01.2:car类(轿车类)
01.3:truck类(卡车类)
02.mototype类(汽车类型类)
03.用户类:软件系统的使用者,登录该系统 管理的 人,比如:管理员、业务经理等。
04.客户类:租车的人。
05.公司类:用于处理租车换车业务流程的人。
-二、设计概要设计
数据库设计:
-三、详细设计
实现各车的信息录入
一个问题:卡车跟大巴和轿车录入是不一样的,所以在业务层需要判断是什么车型,但这样代码比较臃肿,不利于扩展,不是面向对象编程。
解决:采用面向对象的特点(继承和多态),在moto类建立录入车信息的方法,这样轿车客车大巴继承moto类就可以有自己的录入车信息的方法。
代码演示:
moto类:
public abstract class Moto {
private String mno; //车牌号
private int seatCount;
private MotoType mtype;
//这里省略get\set方法
public Moto(MotoType mtype,String mno,int seatCount){
this.mno = mno;
this.seatCount = seatCount;
this.mtype = mtype;
}
/**
* 把当前对象存储到数据库中
* @throws Exception
*/
public void saveDB() throws Exception{
CompanyDao dao = new CompanyDao();
try {
dao.addMoto(this);
} catch (Exception e) {
e.printStackTrace();
}finally{
dao.closeConnection();
}
}
}car类:可以直接使用moto类中的方法
public class Car extends Moto{
public Car(MotoType mtype,String mno) {
super(mtype,mno, 5);
}
}truck类:重写了moto类的saveDB()方法
public class Truck extends Moto{
private int dun;
private double priceEachDun; //每吨每天的单价
public int getDun() {
return dun;
}
public double getPriceEachDun() {
return priceEachDun;
}
public void setPriceEachDun(double priceEachDun) {
this.priceEachDun = priceEachDun;
}
public Truck(MotoType mtype, String mno, int seatCount,int dun) {
super(mtype, mno, seatCount);
this.dun = dun;
}
public double getDayMoney() {
return priceEachDun*dun;
}
/**
* 把当前对象存储到数据库中 ----------重写
* @throws Exception
*/
@Override
public void saveDB() throws Exception{
CompanyDao dao = new CompanyDao();
try {
dao.beginTransaction();
dao.addMoto(this);
TruckEntity truckEntity = new TruckEntity();
truckEntity.setMno(this.getMno());
truckEntity.setDun(dun);
truckEntity.setPriceEachDun(priceEachDun);
dao.addTruck(truckEntity);
dao.commit();
} catch (Exception e) {
e.printStackTrace();
dao.rollback();
throw e;
}finally{
dao.closeConnection();
}
}
}在业务逻辑层方法就很简单了:
private List<Moto> motos;
/**
* 添加汽车
* @param moto
* @throws Exception
*/
public void addMoto(Moto moto) throws Exception{
if(moto != null ){
moto.saveDB(); //OO多态
motos.add(moto);
}else{
throw new Exception("入参moto错误");
}
}2.实现租车业务
图解:
注意:由于整个租车过程包含多个表的修改,比如加入两个订单在差不多时间操作同一辆车,肯定会有一个订单有问题,这是就需要保证事务的一致性和完整性。
在业务逻辑层的代码就可以:
public class RentCompany {
private String name;
private List<Moto> motos; //待租赁的汽车
public String getName() {
return name;
}
public List<Moto> getMotos() {
return motos;
}
public RentCompany(String name){
this.name = name;
motos = new ArrayList<Moto>(50);
}
/**
* 汽车租赁
* @param motos
* @param client
* @param rentinfo
* @return 租赁成功,返回订单号
* @throws Exception
*/
public String rent(List<Moto> motos,TClient client,TRentInfo rentinfo) throws Exception{
String rentno = null;
if(motos != null && client != null && rentinfo!= null){
CompanyDao dao = new CompanyDao();
try {
dao.beginTransaction(); //开启事务
//添加客户数据
dao.addTClient(client);
//添加汽车租赁信息
rentno = dao.addRentInfo(rentinfo);
//添加租赁明细
for(Moto moto : motos){
TRentDetail rentDetail = getTRentDetail(rentno,moto);
dao.addRentDetail(rentDetail);
}
dao.commit(); //提交事务
} catch (Exception e) {
dao.rollback(); //回滚事务
rentno = null;
throw e;
}finally{
dao.closeConnection();
}
}else{
throw new Exception("入参错误,请检查");
}
return rentno;
}
private TRentDetail getTRentDetail(String rentno,Moto moto){
TRentDetail detail = new TRentDetail();
detail.setMno(moto.getMno());
detail.setRentno(rentno);
detail.setDaymoney(moto.getDayMoney());
return detail;
}
}3.实现还车业务
本文出自 “秦斌的博客” 博客,请务必保留此出处http://qinbin.blog.51cto.com/11773640/1967828
标签:java
原文地址:http://qinbin.blog.51cto.com/11773640/1967828