Java汽车租赁系统1.3-面向对象+I/O流

汽车租赁系统

author:luckyboy!

version:1.3

知识储备:变量、数据类型、选择结构、循环结构、数组 、面向对象、集合、I/O流

系统概述:某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如下表所示。

车型具体信息日租金折扣
轿车宝马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)

面向对象设计步骤

        首先读懂项目需求,分析项目需求找出隐藏在其中的名词和动词,这些名词可能是所能用到的类和属性,动词可能是需要用到的方法。根据已知的类、属性、方法进一步优化设计,最后梳理项目运行过程。

 需求中的名词

        汽车租赁公司、汽车、轿车、客车、别克、宝马、金杯、金龙、X6、550i、GL8、林荫大道、座位数、日租金、折扣、车牌号(京NY28588、京CNY3284、京NT37465、京NT96968、京6566754、京6566754、京9696996、京8696998)

类和类属性

        根据已知名词找出需要使用的类和类属性

        汽车类:车牌号、车的品牌、日租金

        客车类:车牌号、车的品牌、日租金、座位数

        轿车类:车牌号、车的品牌、日租金、车的型号

        汽车业务类:

        汽车租赁管理类(测试类)

分析:

  • 客车和轿车都属于汽车,汽车是客车和汽车的父类,汽车和客车是汽车的子类。
  • 客车和汽车都具有相同的属性(车牌号、车的品牌、日租金);那么客车类和轿车类可以继承汽车类的属性。
  • 除去相同的属性还具有私有的属性,客车类具有座位数,轿车类具有车的型号。
  • 还应有一个汽车业务类来完成汽车租赁功能。
  • 汽车租赁管理类用来对汽车租赁系统进行测试。

需求中的动词

        计算租金、租赁、程序入口是类中所需大方法。

优化设计

 完成优化设计代码:

创建 .Java文件:MotoVehicle类(汽车类)、Car类(轿车类)、Bus类(客车类)、MotoOperation类(汽车业务类)、Test类(测试类)

阶段划分

  • 第一阶段:创建并完成汽车类
  • 第二阶段:创建并完成轿车类
  • 第三阶段:创建并完成客车类
  • 第四阶段:创建并完成汽车业务类
  • 第五阶段:创建并完成测试类

注意:通过添加将数据保存到文件中的方法和从文件中读取信息的方法,来存储和获取信息信息

第一阶段:创建并完成汽车类

父类MotoVehicle

//汽车类
public class MotoVehicle {

	// 车牌号 
	private String vehicleId;
	// 品牌
	private String brand;
	// 日租金
	private int perRent;

	//无参构造方法
	public MotoVehicle() {
		super();
	}

	//有参构造方法
	public MotoVehicle(String vehicleId, String brand, int perRent) {
		super();
		this.vehicleId = vehicleId;
		this.brand = brand;
		this.perRent = perRent;
	}

	//get和set方法
	public String getVehicleId() {
		return vehicleId;
	}

	public void setVehicleId(String vehicleId) {
		this.vehicleId = vehicleId;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public int getPerRent() {
		return perRent;
	}

	public void setPerRent(int perRent) {
		this.perRent = perRent;
	}
	
	//计算租金(抽象方法)
	

}

第二阶段:创建并完成轿车类 

子类Car继承父类MotoVehicle

import java.io.Serializable;

public class Car implements Serializable {

	// 车牌号
	private String vehicleId;
	// 品牌
	private String brand;
	// 日租金
	private int perRent;
	// 型号
	private String type;

	// 无参构造方法
	public Car() {
		super();
	}

	public Car( String vehicleId, String brand, int perRent,String type) {
		super();
		this.type = type;
		this.vehicleId = vehicleId;
		this.brand = brand;
		this.perRent = perRent;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getVehicleId() {
		return vehicleId;
	}

	public void setVehicleId(String vehicleId) {
		this.vehicleId = vehicleId;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public int getPerRent() {
		return perRent;
	}

	public void setPerRent(int perRent) {
		this.perRent = perRent;
	}

	@Override
	public String toString() {
		return "Car [type=" + type + ", vehicleId=" + vehicleId + ", brand="
				+ brand + ", perRent=" + perRent + "]";
	}

}

第三阶段:创建并完成客车类 

子类Bus继承父类MotoVehicle

import java.io.Serializable;

//客车类
public class Bus extends MotoVehicle implements Serializable {

	// 车牌号
	private String vehicleId;
	// 品牌
	private String brand;
	// 日租金
	private int perRent;
	// 座位数
	private int seatCount;

	// 无参构造方法
	public Bus() {
		super();
	}

	public Bus(String vehicleId, String brand, int perRent, int seatCount) {
		super();
		this.vehicleId = vehicleId;
		this.brand = brand;
		this.perRent = perRent;
		this.seatCount = seatCount;
	}

	public String getVehicleId() {
		return vehicleId;
	}

	public void setVehicleId(String vehicleId) {
		this.vehicleId = vehicleId;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public int getPerRent() {
		return perRent;
	}

	public void setPerRent(int perRent) {
		this.perRent = perRent;
	}

	public int getSeatCount() {
		return seatCount;
	}

	public void setSeatCount(int seatCount) {
		this.seatCount = seatCount;
	}

	@Override
	public String toString() {
		return "Bus [vehicleId=" + vehicleId + ", brand=" + brand
				+ ", perRent=" + perRent + ", seatCount=" + seatCount + "]";
	}

}

第四阶段:创建并完成汽车业务类

MotoOperation类

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Scanner;

import javax.imageio.stream.FileImageInputStream;

public class MotoOperation {
	Scanner in = new Scanner(System.in);
	
	

	// 初始化汽车信息
	public void inti() {
		// 实例化Car类
		Car car1 = new Car("京NY28588", "宝马", 800, "X6");
		Car car2 = new Car("京CNY28588", "宝马", 600, "550i");
		Car car3 = new Car("京NT37465", "别克", 300, "林萌大道");
		Car car4 = new Car("京NT96968", "别克", 600, "GL8");
		// 创建集合保存轿车信息
		ArrayList<Car> carList = new ArrayList<Car>();
		// 将轿车信息添加到集合
		carList.add(car1);
		carList.add(car2);
		carList.add(car3);
		carList.add(car4);
		//将集合中的信息保存到Car文件
		writeCar(carList);
		
		// 实例化Bus类
		Bus bus1 = new Bus("京6566754", "金杯", 800, 16);
		Bus bus2 = new Bus("京9696996", "金杯", 1500, 34);
		Bus bus3 = new Bus("京8696997", "金龙", 800, 16);
		Bus bus4 = new Bus("京8696998", "金龙", 1500, 34);
		// 创建集合保存客车信息
		ArrayList<Bus> busList = new ArrayList<Bus>();
		// 将客车信息添加到集合
		busList.add(bus1);
		busList.add(bus2);
		busList.add(bus3);
		busList.add(bus4);
		//将集合中的信息保存到Bus文件
		writeBus(busList);
	}
	
	//将集合中的信息保存到Car文件中
	@SuppressWarnings("resource")
	public void writeCar(ArrayList<Car> carList){
		OutputStream os;
		try {
			os = new FileOutputStream("Car.txt");
			ObjectOutputStream oos = new ObjectOutputStream(os);
			oos.writeObject(carList);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//将集合中的信息保存到Bus文件中
	@SuppressWarnings("resource")
	public void writeBus(ArrayList<Bus> busList){
		OutputStream os;
		try {
			os = new FileOutputStream("Bus.txt");
			ObjectOutputStream oos = new ObjectOutputStream(os);
			oos.writeObject(busList);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//读取Car文件中的信息保存到集合中
	@SuppressWarnings({ "resource", "unchecked" })
	public ArrayList<Car> readCar(){
		InputStream is;
		try {
			is = new FileInputStream("Car.txt");
			ObjectInputStream ois = new ObjectInputStream(is);
			Object object = ois.readObject();
			ArrayList<Car> list = (ArrayList<Car>)object;
			return list;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//读取Bus文件中的信息把保存到集合中
	@SuppressWarnings({ "resource", "unchecked" })
	public ArrayList<Bus> readBus(){
		InputStream is;
		try {
			is = new FileInputStream("Bus.txt");
			ObjectInputStream ois = new ObjectInputStream(is);
			Object object = ois.readObject();
			ArrayList<Bus> list = (ArrayList<Bus>)object;
			return list;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	// 汽车租赁方法
	public void start() {

		System.out.println("**********欢迎光临租赁公司**********");
		System.out.println("1-轿车\t2-客车");
		System.out.println("请选择您要租赁的汽车类型:");
		int type = in.nextInt();
		// 如果输入1和2以外的数字,则重新输入
		while (type < 1 || type > 2) {
			System.out.println("输入错误,请重新选择您要租赁的汽车类型:");
			type = in.nextInt();
		}
		if (type == 1) {// 用户选择租轿车
			// 租轿车的方法
			car();
		} else if (type == 2) {// 用户选择租客车
			// 租客车的方法
			bus();
		}

	}

	// 租轿车的方法
	public void car() {
		//取出Car文件中的信息保存到结合
		ArrayList<Car> carList = readCar();
		String brand = "";
		String type = "";
		System.out.println("请选择您要租赁的轿车品牌:1-别克\t2-宝马");
		int chooseBrand = in.nextInt();
		// 如果输入1和2以外的数字,则重新输入
		while (chooseBrand < 1 || chooseBrand > 2) {
			System.out.println("输入错误,请重新选择您要租赁的轿车品牌:");
			chooseBrand = in.nextInt();
		}

		if (chooseBrand == 1) {
			brand = "别克";
			System.out.println("请选择您要租赁的汽车型号:1-林萌大道\t2-GL8");
			int chooseType = in.nextInt();
			// 如果输入1和2以外的数字,则重新输入
			while (chooseType < 1 || chooseType > 2) {
				System.out.println("输入错误,请重新选择您要租赁的轿车型号:");
				chooseType = in.nextInt();
			}
			type = chooseType == 1 ? "林萌大道" : "GL8";
		} else if (chooseBrand == 2) {
			brand = "宝马";
			System.out.println("请选择您要租赁的汽车类型:1-X6\t2-50i");
			int chooseType = in.nextInt();
			// 如果输入1和2以外的数字,则重新输入
			while (chooseType < 1 || chooseType > 2) {
				System.out.println("输入错误,请重新选择您要租赁的轿车型号:");
				chooseType = in.nextInt();
			}
			type = chooseType == 1 ? "X6" : "550i";
		}

		// 计算租金并输出租车成功的信息
		for (int i = 0; i < carList.size(); i++) {
			// 如果存储轿车信息的集合中的信息与用户选择的轿车品牌和型号相同,输出车牌号提车
			if (carList.get(i).getBrand().equals(brand)
					&& carList.get(i).getType().equals(type)) {
				// 计算租金
				System.out.println("请输入您要租的天数:");
				int days = in.nextInt();
				double price = carList.get(i).getPerRent() * days;
				if (days > 7 && days <= 30) {
					price = price * 0.9;
				} else if (days > 30 && days <= 150) {
					price = price * 0.8;
				} else if (days > 150) {
					price = price * 0.7;
				}

				System.out.println("租车成功,请按照如下车牌号提车:"
						+ carList.get(i).getVehicleId());
				System.out.println("您需要支付:" + price + "元");
			}

		}

	}

	// 租客车的方法
	public void bus() {
		//取出Bus文件中的信息保存到结合
		ArrayList<Bus> busList = readBus();
		String brand = "";
		int count = 0;
		System.out.println("请选择您要租赁的客车品牌:1-金杯\t2-金龙");
		int chooseBrand = in.nextInt();
		// 如果用户输入1和2以外的数子,则重新输入
		while (chooseBrand < 1 && chooseBrand > 2) {
			System.out.println("输入错误,请重新选择您要租赁的轿车品牌:");
			chooseBrand = in.nextInt();
		}

		brand = chooseBrand == 1 ? "金杯" : "金龙";

		System.out.println("请选择您要租赁的客车座位数:1-16座\t2-34座");
		int chooseCount = in.nextInt();

		while (chooseCount < 1 && chooseCount > 2) {
			System.out.println("输入错误,请重新选择您要租赁的客车座位数:1-16座\t2-34座");
			chooseCount = in.nextInt();
		}

		count = chooseCount == 1 ? 16 : 34;

		// 计算租金并输出租车成功的信息
		for (int i = 0; i < busList.size(); i++) {
			// 如果存储轿车信息的集合中的信息与用户选择的轿车品牌和型号相同,输出车牌号提车
			if (busList.get(i).getBrand().equals(brand)
					&& busList.get(i).getSeatCount() == count) {
				// 计算租金
				System.out.println("请输入您要租的天数:");
				int days = in.nextInt();
				double price = busList.get(i).getPerRent() * days;
				if(days >= 3 && days < 7){
					price = price * 0.9;
				}else if(days >= 7 && days < 30){
					price = price * 0.8;
				}else if(days >= 30 && days < 150){
					price = price * 0.7;
				}else if(days >= 150){
					price = price * 0.6;
				}

				System.out.println("租车成功,请按照如下车牌号提车:"
						+ busList.get(i).getVehicleId());
				System.out.println("您需要支付:" + price + "元");
			}

		}

	}
}

 第五阶段:创建并完成测试类

Test类

public class Test {

	public static void main(String[] args) {
		//实例化汽车业务类
		MotoOperation moto = new MotoOperation();
		//调用汽车业务类中的初始化汽车信息方法
		moto.inti();
		//调用汽车业务类中的汽车租赁方法
		moto.start();
	}
}

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值