38、Java——汽车租赁系统(JDBC+MySQL+Apache DBUtils)

news2024/10/6 18:25:26

✅作者简介:热爱国学的Java后端开发者,修心和技术同步精进。

🍎个人主页:乐趣国学的博客

🍊个人信条:不迁怒,不贰过。小知识,大智慧。

💞当前专栏:Java案例分享专栏

✨特色专栏:国学周更-心性养成之路

🥭本文内容:Java——汽车租赁系统(JDBC+MySQL+Apache DBUtils)

更多内容点击👇

                       Java——汽车租赁系统(对象+JDBC)

                       Java——汽车租赁系统(对象+XML)

                       Java——汽车租赁系统(对象+集合)

                       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知识。与此同时,对于日常生活有困扰的朋友,欢迎阅读我的第四栏目:《国学周更—心性养成之路》,学习技术的同时,我们也注重了心性的养成。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/45890.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

iptables简述

netfilter iptables的底层实现是netfilter。netfilter实在Linux内核2.4版引入的子系统&#xff0c;作为通用框架提供一套hook函数的管理机制&#xff0c;使得数据包过滤、地址转换、访问控制、连接跟踪等功能得以实现。netfilter的架构就是在整个网络流程中放置了一些钩子&…

[附源码]计算机毕业设计springboot海滨学院学生大创项目申报与审批系统

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

大中型园区网络拓扑架构

目录 园区出口区 数据中心区 网络管理区 DMZ区 核心层 汇聚层 终端层 接入层 大中型园区网络通常采用核心层为“根”的树形网络架构&#xff0c;拓扑稳定&#xff0c;易于扩展和维护。 园区网络可划分为多个层次&#xff1a;接入层、汇聚层、核心层&#xff0c; 以及多个分区&a…

iwebsec靶场 SQL注入漏洞通关笔记9- 双写关键字绕过

系列文章目录 iwebsec靶场 SQL注入漏洞通关笔记1- 数字型注入_mooyuan的博客-CSDN博客 iwebsec靶场 SQL注入漏洞通关笔记2- 字符型注入&#xff08;宽字节注入&#xff09;_mooyuan的博客-CSDN博客 iwebsec靶场 SQL注入漏洞通关笔记3- bool注入&#xff08;布尔型盲注&#…

雨水情监测及视频监控解决方案 水库雨水情自动测报系统 介绍 功能 特点

平升电子水雨情自动监测系统/雨水情监测及视频监控解决方案/水库雨水情自动测报系统辅助水利管理部门实现水库雨水情信息“全要素、全量程、全覆盖”自动测报。系统具备水库水位、雨量、现场图像/视频等水文信息采集、传输、处理及预警广播等功能&#xff0c;有效提升了雨水情信…

技术分享 oracle中fm的作用

SQL> select |||to_char(5,999)||| from dual; 结果为&#xff1a;| 5| SQL> select |||to_char(5,000)||| from dual; 结果为&#xff1a;| 005| 如何去除多余的空格&#xff1f; SQL> select |||to_char(5,fm000)||| from dual; 结果为&#xff1a;|005| 空格…

mysql相关基础知识篇(三)

1.一条更新sql语句怎么执行的了解吗&#xff1f; 更新语句的执行是 Server 层和引擎层配合完成&#xff0c;数据除了要写入表中&#xff0c;还要记录相应的日志。 执行器先找引擎获取 ID2 这一行。ID 是主键&#xff0c;存储引擎检索数据&#xff0c;找到这一行。如果 ID2 这…

工业服务被忽视的销售力量:他们的技术人员

目录 1.从销售到服务的普遍 2.从服务到销售的滞后 3.是什么阻碍了售后服务时销售行为的发生 3.如何改善这种状况 1.从销售到服务的普遍 服务销售窗口的提前在工业企业已经是非常普遍的现象&#xff0c;特别是在互联网经济高度发达的今天&#xff0c;销售的触角已经直达消费…

MySQL如何确定查询处理各个阶段所消耗的时间

使用profile set profiling1; 启动profile 这是一个session级的配置 执行查询 show profiles; 查询每一个查询所消耗的总时间信息 show profile for query N; 查询的每个阶段所耗的时间 show profile cpu for query 1; 但是每次使用都会有一个warning 使用performanc…

59 - 类模板与函数模板的深度剖析

---- 整理自狄泰软件唐佐林老师课程 1. 多参数模板 类模板可以定义 任意多个不同的 类型参数 2. 类模板可以被 特化 模板本来是一组通用逻辑的实现&#xff0c;但是可能存在特定的参数类型下&#xff0c;通用的逻辑实现不能满足要求&#xff0c;这时就需要针对这些特殊的类型&…

【密码学基础】RSA加密算法

1 RSA介绍 RSA是一种非对称加密算法&#xff0c;即加密和解密时用到的密钥不同。加密密钥是公钥&#xff0c;可以公开&#xff1b;解密密钥是私钥&#xff0c;必须保密保存。基于一个简单的数论事实&#xff1a;两个大质数相乘很容易&#xff0c;但想要对其乘积进行因式分解却…

IIS 部署 SSL 证书提示证书链中的一个或多个中间证书丢失

现象描述 IIS Web 服务部署免费 SSL 证书时提示 “证书链中的一个或多个中间证书丢失&#xff0c;要解决此问题&#xff0c;请确保安装了所有中间证书”。 下载中间证书文件&#xff0c;根据您的证书加密算法类型下载中间证书至您的云服务器中。 安装中间证书 1. 在您需要部…

Arcgis地理配准栅格数据

前提 有时候获取的不同数据没有坐标,而却有共同点,这时候需要对数据进行配准校正,数据才能最终拼接镶嵌在一起 效果 1、准备数据 2、打开地理配准工具 3、地理配准中设置待配准数据 4、添加控制点 这份数据目测很容易找到控制点 在待配准数据上先选择控制点,再在基准数…

JavaScript_BOM

JavaScript_BOM BOM&#xff1a;Browser Object Model 浏览器对象模型。也就是 JavaScript 将浏览器的各个组成部分封装为对象。 我们要操作浏览器的各个组成部分就可以通过操作 BOM 中的对象来实现。 BOM 中包含了如下对象&#xff1a; Window&#xff1a;浏览器窗口对象N…

JavaScript基础语法(类型转换)

JavaScript基础语法&#xff08;类型转换&#xff09; 使用运算符的时候会发生类型转换。 其他类型转为number string转换为 number类型&#xff1a;按照字符串的字面值&#xff0c;转为数字。如果字面值不是数字&#xff0c;则转为NaN 将 string转换为 number有两种方式&…

存储器管理

文章目录存储器的层次结构存储器的指标多层结构的存储器系统主存储器与寄存器高速缓存和磁盘缓存程序的装入和链接对用户程序的处理步骤程序的装入程序的链接连续分配的存储管理方式单一连续分配固定分区分配动态分区分配基于顺序搜索的动态分区分配算法基于索引搜索的动态分区…

EventLoop

1.javascript是一门单线程语言 任务1 ---> 任务2--->任务3 单线程执行任务队列的问题&#xff1a; 如果前一个任务非常耗时&#xff0c;则后面的任务不得不一直等待&#xff0c;从而导致程序假死问题 2.同步任务和异步任务 同步任务&#xff1a;js主线程直接执行 同…

沉睡者IT - 什么是Web3.0?

欢迎关注沉睡者IT&#xff0c;点上面关注我 ↑ ↑ 什么是Web3.0&#xff1f; Web3&#xff08;也称为Web 3&#xff09;用最简单的话来解释就是&#xff0c;第三代互联网。 那么有朋友要问了&#xff0c;那么什么是第一代&#xff0c;什么是第二代&#xff1f;第三代又有什么…

2.1 Redis中SDS的定义

每个sds.h/sdshdr 结构表示一个SDS值 struct sdshdr { //记录 buf 数组中已经使用的字节数量 //等于SDS所保存字符串的长度 int len;//记录buf数组中未使用字节的数量 int free;//字节数组,用于保存字符串 char buf[]; };图2-1 展示了一个SDS 示例: 1、free 属性值为0&#x…

Kotlin 开发Android app(十三):RadioGroup和ViewPager控件实现底层分页按钮

安卓的控件是挺多的&#xff0c;没有办法一个一个的来说明&#xff0c;我们挑出了一些重点的控件&#xff0c;组成一些常见的布局&#xff0c;这样以后在遇到相同功能的界面时&#xff0c;就会有自己的思路&#xff0c;或者进行复用。 在这一节中&#xff0c;我们实现一个底层分…