Java從零編寫汽車租賃系統(tǒng)全程分析
覆蓋知識
程序基本概念、數(shù)據(jù)類型、流程控制、順序、選擇 、循環(huán)、跳轉(zhuǎn)語句、變量、類、方法、繼承、多態(tài)。
掌握數(shù)據(jù)庫、JDBC、三層架構(gòu)等相關(guān)知識。
掌握Druid連接池、Apache的DBUtils使用 。
項目需求
某汽車租賃公司出租多種轎車和客車,出租費用以日為單位計算。
出租車型及信息如下表所示:
| 車型 | 具體信息 | 日租金 | 折扣 |
| 轎車 | 寶馬X6(京NY28588) | 800 | days>7天9折 days>30天8折 days>150天7折 |
| 寶馬550i(京CNY3284) | 600 | ||
| 別克林蔭大道(京NT37465) | 300 | ||
| 別克GL8(京NT96968) | 600 | ||
| 客車 | 金杯,16座(京6566754) | 800 | days>=3天9折 days>=7天8折 days>=30天7折 days>=150天6折 |
| 金龍,16座(京8696997) | |||
| 金杯,34座(京9696996) | 1500 | ||
| 金龍,34座(京8696998) |
設(shè)計步驟
?
開發(fā)思路
(1)明確需求
(2)編碼順序
1)、添加需要的jar包到項目中,將lib文件夾中的jar文件通過鼠標(biāo)右單擊選擇Build Path的方式添加到你設(shè)置的eatJar文件目錄里。
?
2)、創(chuàng)建database.properties文件,用來配置注冊驅(qū)動和數(shù)據(jù)庫連接對象的相關(guān)數(shù)據(jù)。
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/java221804
username=root
password=huanghuang
initialSize=10
maxActive=30
maxIdle=5
maxWait=3000
3)、添加需要的工具類DBUtils類
package cn.eat.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class DBUtils {
private static DruidDataSource druidDataSource;
static {
Properties properties = new Properties();
try {
InputStream is = DBUtils.class
.getResourceAsStream("/database.properties");
properties.load(is);
druidDataSource = (DruidDataSource) DruidDataSourceFactory
.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource(){
return druidDataSource;
}
}4)、創(chuàng)建數(shù)據(jù)表:automobile表
CREATE TABLE `automobile` ( `numberPlate` varchar(20) DEFAULT NULL, `brand` varchar(10) DEFAULT NULL, `dayRent` double DEFAULT NULL, `type` varchar(10) DEFAULT NULL, `seat` int(11) DEFAULT NULL )
automobile表效果展示
?
5)、完成父類(汽車類)的編寫
6)、再完成子類(客車類和轎車類)的編寫
7)、數(shù)據(jù)訪問層DAO層的接口和實現(xiàn)類的增刪改查方法的編寫
8)、服務(wù)層Service層的接口和實現(xiàn)類的增刪改查方法的編寫
9)、最后完成視圖層View層測試類的編寫
類的屬性和方法
屬性:
- 汽車類:車牌號、品牌、日租金
- 客車類:車牌號、品牌、日租金、座位數(shù)
- 轎車類:車牌號、品牌、日租金、型號
- 汽車業(yè)務(wù)類:忽略
- 汽車租賃管理類:忽略
方法:
定義租車的方法,不同類型的汽車采用不同租金方法進行計算。
?
代碼展示
1、汽車類(父類)
package cn.automobile.entity;
public abstract class Automobile {
// 定義汽車類的屬性(車牌號、品牌、日租金)
private String numberPlate;
private String brand;
private double dayRent;
public Automobile() {
super();
}
public Automobile(String numberPlate, String brand, double dayRent) {
super();
this.numberPlate = numberPlate;
this.brand = brand;
this.dayRent = dayRent;
}
public String getNumberPlate() {
return numberPlate;
}
public void setNumberPlate(String numberPlate) {
this.numberPlate = numberPlate;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getDayRent() {
return dayRent;
}
public void setDayRent(double dayRent) {
this.dayRent = dayRent;
}
//定義計算租金的抽象方法
public abstract double calRent(int days,double dayRent);
@Override
public String toString() {
return "Automobile [numberPlate=" + numberPlate + ", brand=" + brand
+ ", dayRent=" + dayRent + "]";
}
}2、轎車類(子類)
package cn.automobile.entity;
public class Bus extends Automobile {
private int seat;
public Bus() {
super();
}
public Bus(String numberPlate, String brand, double dayRent, int seat) {
super(numberPlate, brand, dayRent);
this.seat = seat;
}
public int getSeat() {
return seat;
}
public void setSeat(int seat) {
this.seat = seat;
}
@Override
public double calRent(int days,double dayRent) {
// System.out.println("bus");
double discount=dayRent*days;
if(days>150){
discount*=0.6;
}else if(days>30){
discount*=0.7;
}else if(days>7){
discount*=0.8;
}else if(days>3){
discount*=0.9;
}
return discount;
}
}3、客車類(子類)
package cn.automobile.entity;
public class Car extends Automobile {
// 定義特有屬性
private String type;
public Car() {
super();
}
public Car(String numberPlate, String brand, double dayRent, String type) {
super(numberPlate, brand, dayRent);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public double calRent(int days,double dayRent) {
// System.out.println("car");
double discount=dayRent*days;
if(days>150){
discount*=0.7;
}else if(days>30){
discount*=0.8;
}else if(days>7){
discount*=0.9;
}
return discount;
}
}4、數(shù)據(jù)訪問層AutomobileDao接口
package cn.automobile.dao;
import cn.automobile.entity.Automobile;
import cn.automobile.entity.Bus;
import cn.automobile.entity.Car;
public interface AutomobileDao {
//查bus
Automobile selectOne(Bus bus);
//查car
Automobile selectOne(Car car);
}5、數(shù)據(jù)訪問層AutomobileDaoImpl實現(xiàn)類
package cn.automobile.dao.Impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import cn.automobile.dao.AutomobileDao;
import cn.automobile.entity.Automobile;
import cn.automobile.entity.Bus;
import cn.automobile.entity.Car;
import cn.automobile.utils.DBUtils;
public class AutomobileDaoImpl implements AutomobileDao {
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
@Override
public Automobile selectOne(Bus bus) {
String sql="select * from automobile;";
try {
List<Bus> listAutomobiles=queryRunner.query(sql, new BeanListHandler<Bus>(Bus.class));
for (Bus bus1 : listAutomobiles) {
if(bus1.getBrand().equals(bus.getBrand())&&bus1.getSeat()==bus.getSeat()){
return bus1;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public Automobile selectOne(Car car) {
String sql="select * from automobile;";
try {
List<Car> listAutomobiles=queryRunner.query(sql, new BeanListHandler<Car>(Car.class));
for (Car car1 : listAutomobiles) {
if(car1.getBrand().equals(car.getBrand())&&car1.getType().equals(car.getType())){
return car1;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}6、服務(wù)層 AutomobileService 接口
package cn.automobile.service;
import cn.automobile.entity.Automobile;
import cn.automobile.entity.Bus;
import cn.automobile.entity.Car;
public interface AutomobileService {
//查bus
Automobile selectBus(Bus bus);
//查car
Automobile selectCar(Car car);
}7、服務(wù)層AutomobileServiceImpl 實現(xiàn)類
package cn.automobile.service.impl;
import cn.automobile.dao.AutomobileDao;
import cn.automobile.dao.Impl.AutomobileDaoImpl;
import cn.automobile.entity.Automobile;
import cn.automobile.entity.Bus;
import cn.automobile.entity.Car;
import cn.automobile.service.AutomobileService;
public class AutomobileServiceImpl implements AutomobileService {
AutomobileDao autoD=new AutomobileDaoImpl();
@Override
public Automobile selectBus(Bus bus) {
return autoD.selectOne(bus);
}
@Override
public Automobile selectCar(Car car) {
return autoD.selectOne(car);
}
}8、視圖層:租車測試AutomobileMgr類
package cn.automobile.view;
import java.util.Scanner;
import cn.automobile.entity.Automobile;
import cn.automobile.entity.Bus;
import cn.automobile.entity.Car;
import cn.automobile.service.AutomobileService;
import cn.automobile.service.impl.AutomobileServiceImpl;
public class AutomobileMgr {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String brand=null,type=null;
int seat=0;
double money = 0;
Automobile automobile=null;
AutomobileService autoS=new AutomobileServiceImpl();
System.out.println("********歡迎光臨租賃公司********\n");
System.out.println("請選擇汽車類型:1、轎車\t2、客車");
int autoType=sc.nextInt();
if(autoType==1){
System.out.println("請選擇轎車品牌:1、寶馬\t2、別克");
brand=(sc.nextInt()==1)?"寶馬":"別克";
if(brand=="寶馬"){
System.out.println("請選擇轎車型號:1、X6\t2、550i");
type=(sc.nextInt()==1)?"X6":"550i";
}else if(brand=="別克"){
System.out.println("請選擇轎車型號:1、林蔭大道\t2、GL8");
type=(sc.nextInt()==1)?"林蔭大道":"GL8";
}
}else if(autoType==2){
System.out.println("請選擇客車品牌:1、金杯\t2、金龍");
brand=(sc.nextInt()==1)?"金杯":"金龍";
System.out.println("請選擇需要的座位數(shù):1、16座\t2、34座");
seat=(sc.nextInt()==1)?16:34;
}
System.out.println("請選擇租賃天數(shù):");
int days=sc.nextInt();
if (seat==0) {
Car car=new Car();
car.setBrand(brand);
car.setType(type);
automobile=autoS.selectCar(car);
money=car.calRent(days,automobile.getDayRent());
}else if(seat!=0) {
Bus bus=new Bus();
bus.setBrand(brand);
bus.setSeat(seat);
automobile=autoS.selectBus(bus);
money=bus.calRent(days,automobile.getDayRent());
}
// System.out.println(automobile);
System.out.println("租車成功!請按照"+automobile.getNumberPlate()+"車牌號取車!租金為:"+money+"元");
}
}效果展示
??
到此這篇關(guān)于Java從零編寫汽車租賃系統(tǒng)全程分析的文章就介紹到這了,更多相關(guān)Java租車系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Redisson實現(xiàn)注解式分布式鎖的示例代碼
這篇文章主要為大家詳細介紹了如何基于Redisson實現(xiàn)注解式分布式鎖,文中的示例代碼講解詳細,具有一定的參考價值,需要的可以了解一下2023-07-07
Spring-Cloud-Function-Spel?漏洞環(huán)境搭建
這篇文章主要介紹了Spring-Cloud-Function-Spel?漏洞復(fù)現(xiàn)及搭建方法,搭建方法也很簡單,首先需要安裝maven jdk,具體安裝過程跟隨小編一起看看吧2022-03-03
SpringCloud搭建Eureka服務(wù)模塊的過程
Eureka在分布式系統(tǒng)中起到了連接各個微服務(wù)的紐帶作用,使得服務(wù)之間的交互變得更加靈活、可靠,本文將深入探討如何使用Spring?Cloud,逐步引導(dǎo)讀者完成Eureka服務(wù)模塊的搭建,感興趣的朋友跟隨小編一起看看吧2024-02-02
spring security結(jié)合jwt實現(xiàn)用戶重復(fù)登錄處理
本文主要介紹了spring security結(jié)合jwt實現(xiàn)用戶重復(fù)登錄處理,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

