带你深入了解Java!汽车租赁系统!

< 汽车租赁系统 >

可以作为参考,没有在子类去重写,没有优化各个功能,自己练习写也没有太熟练掌握。

测试类main方法

package gatsby4y10;

import java.util.*;
/**
 * 测试类
 * @author Douer
 *
 */
public class Text {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("—————————————————————————————————————————————————");
		System.out.println("|\t\t       欢迎来到租车系统     \t\t|");
		System.out.println("—————————————————————————————————————————————————");
		System.out.println("|\t    —————— 本店的汽车品牌如下 ——————    \t|");
		System.out.println("|\t· 1 · 保时捷(Porsche)\t\t\t|");
		System.out.println("|\t· 2 · 兰博基尼(Lamborghini)\t\t|");
		System.out.println("|\t· 3 · 迈凯伦(Mclaren)\t\t\t|");
		System.out.println("|\t· 4 · 劳斯莱斯(Rolls-Royce)\t\t|");
		System.out.println("—————————————————————————————————————————————————");
		System.out.print("请输入选择你要租赁的汽车品牌的序号: ");
		//定义一个变量存储选择的车型
		String model = null;
		int num = input.nextInt();
		switch(num) {
		case 1:
			System.out.println("·1·帕拉梅拉(Panamera)  ·2·卡宴 (Cayenne)");
			System.out.print("请输入你要选择的汽车型号:");
			model = input.nextInt() == 1 ? "帕拉梅拉(Panamera)":"卡宴 (Cayenne)";
			break;
		case 2:
			System.out.println("·1·兰博基尼(Huracan)  ·2·兰博基尼(Aventador)");
			System.out.print("请输入你要选择的汽车型号:");
			model = input.nextInt() == 1 ? "帕拉梅拉(Panamera)":"兰博基尼(Aventador)";
			break;
		case 3:
			System.out.println("·1·迈凯伦(570S)  ·2·迈凯伦 (720S)");
			System.out.print("请输入你要选择的汽车型号:");
			model = input.nextInt() == 1 ? "迈凯伦(570S)":"迈凯伦 (720S)";
			break;
		case 4:
			System.out.println("·1·幻影(Phantom)  ·2·魅影(Wraith)");
			System.out.print("请输入你要选择的汽车型号:");
			model = input.nextInt() == 1 ? "幻影(Phantom)":"魅影(Wraith)";
			break;
		default:
			break;
		}
		//创建对象 并调用有参方法
		Vehicle car = Business.go(model);
		car.show();
		System.out.println("|请输入你要租赁的天数(7天以上9折 / 15天以上8折  / 0天以上7折)|");
		System.out.println("—————————————————————————————————————————————————");
		//调用方法输出所需车辆信息
		int sky = input.nextInt();
		System.out.println("—————————————————————————————————————————————————");
		System.out.println("|\t\t      根据你选择的天数      \t\t|");
		System.out.println("—————————————————————————————————————————————————");
		//调用方法并输出总金额
		System.out.println("|\t你所需要交付的金额为: "+car.money1(sky)+"  元\t\t|");
		System.out.println("——————————————感谢你的消费!祝你兜风愉快!———————————————");
	}
}

业务类

package gatsby4y10;

/**
 * 汽车业务类
 * 
 * @author Douer
 *
 */
public class Business {
	static Vehicle[] car = new Vehicle[8];
	// 存入所有车龄信息
	static {
		car[0] = new Bs("保时捷(Porsche)", "帕拉梅拉(Panamera)", "京A 88666", 600);
		car[1] = new Bs("保时捷(Porsche)", "卡宴 (Cayenne)", "京A 12345", 700);
		car[2] = new Lb("兰博基尼(Lamborghini)", "兰博基尼(Huracan)", "京A 88886", 1100);
		car[3] = new Lb("兰博基尼(Lamborghini)", "兰博基尼(Aventador)", "京A 68888", 1300);
		car[4] = new Mk("迈凯伦(Mclaren)", "迈凯伦(570S)", "京A 88888", 1300);
		car[5] = new Mk("迈凯伦(Mclaren)", "迈凯伦 (720S)", "京A 11111", 1500);
		car[6] = new Ls(" 劳斯莱斯(Rolls-Royce)", "幻影(Phantom)", "京A 66666", 3000);
		car[7] = new Ls(" 劳斯莱斯(Rolls-Royce)", "魅影(Wraith)", "京A 55555", 2500);
	}

	// 定义方法对比相符的车型
	public static Vehicle go(String brand) {
		for (Vehicle one : car) {
			if (one instanceof Bs) {
				Bs bs = (Bs) one;
				if (brand.equals(bs.getModel())) {
					return bs;
				}
			} else if (one instanceof Lb) {
				Lb lb = (Lb) one;
				if (brand.equals(lb.getModel())) {
					return lb;
				}
			} else if (one instanceof Mk) {
				Mk mk = (Mk) one;
				if (brand.equals(mk.getModel())) {
					return mk;
				}
			} else if (one instanceof Ls) {
				Ls ls = (Ls) one;
				if (brand.equals(ls.getModel())) {
					return ls;
				}
			}
		}
		return null;
	}
}

汽车父类

package gatsby4y10;

/**
 * 定义汽车父类
 * @author Douer
 *
 */
public class Vehicle {
	/**汽车品牌*/
	private String brand;
	/**汽车型号*/
	private String model;
	/**车牌号*/
	private String number;
	/**日租金*/
	private double money;
	
	//无参构造
	public Vehicle() {
		super();
	}
	//有参构造
	public Vehicle(String brand, String model, String number, double money) {
		super();
		this.brand = brand;
		this.model = model;
		this.number = number;
		this.money = money;
	}
	//定义方法展示所定汽车信息
	public void show() {
		System.out.println("—————————————————————————————————————————————————");
		System.out.println("你所选的汽车品牌为: "+brand);
		System.out.println("你所选的汽车型号为: "+model);
		System.out.println("汽车车牌号为: "+number);
		System.out.println("汽车的每日租金为:"+money);
		System.out.println("—————————————————————————————————————————————————");
	}
	//计算总金额 天数单日租金*折扣*天数
	public double money1(int sky) {
		if(sky > 7) {
			return this.money * 0.9 * sky;
		}else if(sky > 15) {
			return this.money * 0.8 * sky;
		}else if(sky > 30) {
			return this.money * 0.7 * sky;
		}
		return this.money * 1 * sky;
	}
	

	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		this.model = model;
	}
	public String getBumber() {
		return number;
	}
	public void setBumber(String bumber) {
		this.number = bumber;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
}

各种品牌车子类

package gatsby4y10;
/**
 * 子类兰博基尼
 * @author Douer
 *
 */
public class Lb extends Vehicle{

	public Lb() {
		super();
	}
	public Lb(String brank, String model, String bumber, double money) {
		super(brank, model, bumber, money);
	}
}

package gatsby4y10;
/**
 * 子类劳斯莱斯
 * @author Douer
 *
 */
public class Ls extends Vehicle{

	public Ls() {
		super();
	}

	public Ls(String brand, String model,String number, double money) {
		super(brand, model, number, money);
	}
}


package gatsby4y10;
/**
 * 子类保时捷
 * @author Douer
 *
 */
public class Bs extends Vehicle{

	public Bs() {
		super();
	}
	public Bs(String brand, String model, String number, double money) {
		super(brand, model, number, money);
	}
}



package gatsby4y10;
/**
 * 子类兰迈凯伦
 * @author Douer
 *
 */
public class Mk extends Vehicle{

	public Mk() {
		super();
	}
	public Mk(String brand, String model,String number, double money) {
		super(brand, model,number, money);
	}
}

猜你喜欢

转载自blog.csdn.net/Gastby98/article/details/89206672
今日推荐