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

发布于:2022-10-17 ⋅ 阅读:(467) ⋅ 点赞:(0)

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

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

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

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

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

🥭本文内容:Java——汽车租赁系统(对象+XML)

更多内容点击👇

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

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

目录

        覆盖知识

        项目需求

        设计步骤 

        开发思路 

        类的属性和方法

        代码展示

        效果展示


覆盖知识

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

项目需求

        某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。

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

车型

具体信息

日租金

折扣

轿车

宝马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)、先完成父类(汽车类)的编写

                2)、再完成子类(客车类和轿车类)的编写

                3)、然后再完成汽车业务类的编写

                4)、最后完成测试类的编写

        (3)逻辑测试

类的属性和方法

属性:

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

方法: 

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

代码展示

 1、汽车类(父类)

package cn.bdqn.demo01;

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,String dayRent);
}

2、轿车类(子类)

package cn.bdqn.demo01;

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,String dayRent) {
		double discount=Integer.valueOf(dayRent)*days;
		if(days>150){
			discount*=0.7;
		}else if(days>30){
			discount*=0.8;
		}else if(days>7){
			discount*=0.9;
		}
		return discount;
	}
}

3、客车类(子类)

package cn.bdqn.demo01;

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,String dayRent) {
//		System.out.println("bus");
		double discount=Integer.valueOf(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;
	}

}

4、汽车业务类

        创建一个Automobile.xml文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Automobiles>
    <Automobile numberPlate="京NY28588" brand="宝马" dayRent="800" type="X6" seat="" />
    <Automobile numberPlate="京CNY3284" brand="宝马" dayRent="600" type="550i" seat="" />
    <Automobile numberPlate="京NT37465" brand="别克" dayRent="300" type="林荫大道" seat="" />
    <Automobile numberPlate="京NT96968" brand="别克" dayRent="600" type="GL8" seat="" />
    <Automobile numberPlate="京6566754" brand="金杯" dayRent="800" type="" seat="16" />
    <Automobile numberPlate="京8696997" brand="金龙" dayRent="800" type="" seat="16" />
    <Automobile numberPlate="京9696996" brand="金杯" dayRent="1500" type="" seat="34" />
    <Automobile numberPlate="京8696998" brand="金龙" dayRent="1500" type="" seat="34" />
</Automobiles>

package cn.bdqn.demo01;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

//汽车业务类
public class AutomobileMethod {
	Document document;

	// 声明方法获得Document对象的
	public void getDocument(File file) {
		// 获取解析器工厂对象
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		// 从DOM工厂获得DOM解析器
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			// 解析XML文档,得到一个Document对象,即DOM树
			document = db.parse(file);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 租车
	public void autoMethod(String brand, String type, int seat,int days) {
		Automobile auto = null;
		String numStr="";
		double money = 0;
		NodeList nl = document.getElementsByTagName("Automobile");
		for (int i = 0; i < nl.getLength(); i++) {
			Element autoXml = (Element) nl.item(i);
			if (autoXml.getAttribute("brand").equals(brand)
					&& autoXml.getAttribute("seat").equals("")) {
				auto=new Car();
				money=auto.calRent(days,autoXml.getAttribute("dayRent"));
				numStr=autoXml.getAttribute("numberPlate");
				break;				
			}else if(autoXml.getAttribute("brand").equals(brand)
					&& autoXml.getAttribute("type").equals("")) {
				auto=new Bus();
				money=auto.calRent(days,autoXml.getAttribute("dayRent"));
				numStr=autoXml.getAttribute("numberPlate");
				break;
			}
		}
		System.out.println("租车成功!请按照"+numStr+"车牌号取车!租金为:"+money);
	}
}

5、租车测试类

package cn.bdqn.demo01;

import java.io.File;
import java.util.Scanner;

//测试类
public class AutomobileLease {

	public static void main(String[] args) {
		
		Scanner sc=new Scanner(System.in);
		String brand=null,type=null;
		int seat=0;
		AutomobileMethod autoM=new AutomobileMethod();
		//获取所有车辆信息
		File file=new File("Automobile.xml");
		autoM.getDocument(file);
		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();

		autoM.autoMethod(brand, type, seat,days);
		
	}
}

效果展示


       码文不易,本篇文章就介绍到这里,如果想要学习更多Java系列知识,请关注博主,博主带你零基础学习Java知识。与此同时,对于日常生活有困扰的朋友,欢迎阅读我的第四栏目:《国学周更—心性养成之路》,学习技术的同时,我们也注重了心性的养成。

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到