最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java從零編寫汽車租賃系統(tǒng)全程分析

 更新時間:2022年12月13日 08:57:33   作者:Java Fans  
這篇文章介紹了Java實現(xiàn)汽車租賃系統(tǒng)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

覆蓋知識

程序基本概念、數(shù)據(jù)類型、流程控制、順序、選擇 、循環(huán)、跳轉(zhuǎn)語句、變量、類、方法、繼承、多態(tài)。

掌握數(shù)據(jù)庫、JDBC、三層架構(gòu)等相關(guān)知識。

掌握Druid連接池、Apache的DBUtils使用 。

項目需求

某汽車租賃公司出租多種轎車和客車,出租費用以日為單位計算。

出租車型及信息如下表所示:

車型具體信息日租金折扣
轎車寶馬X6(京NY28588)800days>7天9折 days>30天8折 days>150天7折
寶馬550i(京CNY3284)600
別克林蔭大道(京NT37465)300
別克GL8(京NT96968)600
客車金杯,16座(京6566754)800days>=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ù)類:忽略
  • 汽車租賃管理類:忽略

方法:

定義租車的方法,不同類型的汽車采用不同租金方法進(jìn)行計算。

?

代碼展示

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)文章

最新評論

阳春市| 东兰县| 湘乡市| 安多县| 石景山区| 兴义市| 临夏县| 承德县| 夏河县| 仁化县| 政和县| 化德县| 广元市| 景东| 会同县| SHOW| 黔西县| 土默特左旗| 万载县| 宜兰市| 周口市| 桂东县| 奇台县| 伊金霍洛旗| 和政县| 清涧县| 嘉鱼县| 湛江市| 中江县| 渭源县| 临西县| 化州市| 五峰| 大连市| 文水县| 鄂伦春自治旗| 洛隆县| 邵阳县| 贵港市| 长宁区| 肇州县|