Java从零编写汽车租赁系统全程分析

目录
  • 覆盖知识
  • 项目需求
  • 设计步骤
  • 开发思路
  • 类的属性和方法
  • 代码展示
  • 效果展示

覆盖知识

程序基本概念、数据类型、流程控制、顺序、选择 、循环、跳转语句、变量、类、方法、继承、多态。

掌握数据库、JDBC、三层架构等相关知识。

掌握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)

设计步骤

开发思路

(1)明确需求

(2)编码顺序

1)、添加需要的jar包到项目中,将lib文件夹中的jar文件通过鼠标右单击选择Build Path的方式添加到你设置的eatJar文件目录里。

2)、创建database.properties文件,用来配置注册驱动和数据库连接对象的相关数据。

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)、创建数据表: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)、数据访问层DAO层的接口和实现类的增删改查方法的编写

8)、服务层Service层的接口和实现类的增删改查方法的编写

9)、最后完成视图层View层测试类的编写

类的属性和方法

属性:

  • 汽车类:车牌号、品牌、日租金
  • 客车类:车牌号、品牌、日租金、座位数
  • 轿车类:车牌号、品牌、日租金、型号
  • 汽车业务类:忽略
  • 汽车租赁管理类:忽略

方法:

定义租车的方法,不同类型的汽车采用不同租金方法进行计算。

代码展示

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、数据访问层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、数据访问层AutomobileDaoImpl实现类

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、服务层 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、服务层AutomobileServiceImpl 实现类

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("请选择需要的座位数:1、16座\t2、34座");
			seat=(sc.nextInt()==1)?16:34;
		}
		System.out.println("请选择租赁天数:");
		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+"元");
	}
}

效果展示

​​

到此这篇关于Java从零编写汽车租赁系统全程分析的文章就介绍到这了,更多相关Java租车系统内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • java编写汽车租赁系统

    本文实例为大家分享了java编写汽车租赁系统的具体代码,供大家参考,具体内容如下 题目要求: 1,汽车租赁信息表如下: 2,类和属性: 3,运行效果: 效果实现: 代码实现: 1,车类: package homework.exam; public abstract  class Vehicle {     private String num;     private String brand;     private double rent;     public String getNum(

  • java实现简单的汽车租赁系统

    本文实例为大家分享了java实现简单的汽车租赁系统的具体代码,供大家参考,具体内容如下 欢迎进入xx汽车租赁公司请输入用户名请输入密码(用户名默认是名字缩写,密码是123,将登陆模块封装到方法中去调用方法)请输入您的操作1)查看现在车库中的所有车辆信息2)租赁汽车3)往车库中添加汽车4)修改汽车租赁价格信息 用switch去判断操作 类分析 代码: package com.youjiuye.bms; public class CRMS {     public static void main(

  • Java面向对象实现汽车租赁系统

    本文实例为大家分享了Java实现汽车租赁系统的具体代码,供大家参考,具体内容如下 父类Vehicle public abstract class Vehicle {     private String num;     private String brand;     private  double rent;     //重写equals方法     public abstract boolean equals(Vehicle v);     //计算费用     public abstr

  • Java实现简单汽车租赁系统

    本文实例为大家分享了Java实现简单汽车租赁系统的具体代码,供大家参考,具体内容如下 需求如下:  问题分析: 首先应当构建一个MotoVehicle的抽象(abstract)类,类里面包含一个brand属性,表示汽车品牌:还包含一个no属性,表示汽车牌号: package cn.jbit.car; public abstract class MotoVehicle { private String no; private String brand; /** * 无参构造方法 */ public

  • Java 汽车租赁系统详细实现代码

    汽车租赁: 分为客车和轿车两种: 客车小于20座:500一天,大于20座:900一天. 轿车分为豪华和普通:豪华600一天,普通200一天. 效果图: 代码如下: 机动车类: package busTest; /* 机动车类 */ public abstract class MotoVehicle { private String carNumber; //车牌号 private String carBrand; // 车品牌 //构造方法 public MotoVehicle(){} publ

  • java实现汽车租赁系统

    本文实例为大家分享了java实现汽车租赁系统的具体代码,供大家参考,具体内容如下 //车类 public abstract class Vehicle { //车牌号 品牌 日租金 private String id; private String brand; private int perRent; public Vehicle(){} //Vehicle的带参构造方法 public Vehicle(String id, String brand, int perRent) { this.i

  • Java从零编写汽车租赁系统全程分析

    目录 覆盖知识 项目需求 设计步骤 开发思路 类的属性和方法 代码展示 效果展示 覆盖知识 程序基本概念.数据类型.流程控制.顺序.选择 .循环.跳转语句.变量.类.方法.继承.多态. 掌握数据库.JDBC.三层架构等相关知识. 掌握Druid连接池.Apache的DBUtils使用 . 项目需求 某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算. 出租车型及信息如下表所示: 车型 具体信息 日租金 折扣 轿车 宝马X6(京NY28588) 800 days>7天9折 days>30天

  • Java从零编写吃货联盟订餐系统全程讲解

    目录 项目需求 项目环境准备 案例覆盖的技能点 难点分析 1.环境搭建和相关配置 2.使用数据库保存Order订单以及Food菜单信息 3.Dao层实现类中的增删改查方法的编写 项目实现思路 1.数据初始化 2.实现菜单切换 3.实现查看餐袋 4.实现我要订餐 5.实现签收订单 6.实现删除订单 7.实现我要点赞 代码展示 1.订单信息-Order类 2.菜品信息-Food类 3.定义FoodDao接口 4.定义FoodDaoImpl实现类 5.定义OrderDao接口 6.定义OrderDao

  • C#实现简单的汽车租赁系统

    最近学习了继承,多态,集合,设计模式,有一个汽车租凭系统,给大家分享一下: 我们首先来看看我们这个系统的效果 1.做一个项目,我们首先对项目进行分析 根据我们最近学的知识,我们可以看出继承,多态,集合,设计模式,我们都能用到 我们把所需要的类和简单模式中的"简单工厂"的工厂准备好 类图: 01.车辆类(父类) using System; using System.Collections.Generic; using System.Linq; using System.Text; usi

  • C#实现汽车租赁系统项目

    本文实例为大家分享了C#实现汽车租赁系统的具体代码,供大家参考,具体内容如下 汽车和卡车的父类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //父类变量和方法 namespace 汽车租赁系统 { public class Inheritance { public Inheritance() { } publi

随机推荐