栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA 面向对象实战 汽车租赁

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA 面向对象实战 汽车租赁

题目: 某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如

下表所示 

 

 1、创建父类
//创建父类Car
public abstract class Car {
	// 定义属性
	private String licensePlateNumber;
	private String brand;
	private double dayRent;

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

	// 有参构造方法
	public Car(String licensePlateNumber, String brand, double dayRent) {
		super();
		this.licensePlateNumber = licensePlateNumber;
		this.brand = brand;
		this.dayRent = dayRent;
	}

	// getXxx/setXxx构造方法
	public String getLicensePlateNumber() {
		return licensePlateNumber;
	}

	public void setLicensePlateNumber(String licensePlateNumber) {
		this.licensePlateNumber = licensePlateNumber;
	}

	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;
	}

	// 租车的方法rent
	public abstract double rent(int days);

}
2、创建第一个子类
//创建客车类Bus
public class Bus extends Car {
	private int seat;

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

	// 无参构造方法
	public Bus(String licensePlateNumber, String brand, double dayRent, int seat) {
		super(licensePlateNumber, brand, dayRent);
		this.seat = seat;
	}

	// getXxx/setXxx构造方法
	public int getSeat() {
		return seat;
	}

	public void setSeat(int seat) {
		this.seat = seat;
	}

	// 租金计算方法
	@Override
	public double rent(int days) {
		double price = getDayRent() * days;
		if (days >= 3 && days < 7) {
			price *= 0.9;
		} else if (days >= 7 && days < 30) {
			price *= 0.8;
		} else if (days >= 30 && days < 150) {
			price *= 0.7;
		} else if (days >= 150) {
			price *= 0.6;
		}
		return price;

	}

}
 3、创建第二个子类
//创建轿车类 
public class SaloonCar extends Car {
	private String type;

	// 有参构造方法
	public SaloonCar() {
		super();
	}

	// 无参构造方法
	public SaloonCar(String licensePlateNumber, String brand, double dayRent,
			String type) {
		super(licensePlateNumber, brand, dayRent);
		this.type = type;
	}

	// getXxx/setXxx构造方法
	public String getType() {
		return type;
	}

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

	// 租金计算方法
	@Override
	public double rent(int days) {
		double price = getDayRent() * days;
		if (days > 7 && days <= 30) {
			price *= 0.9;
		} else if (days > 30 && days <= 150) {
			price *= 0.8;
		} else if (days > 150) {
			price *= 0.7;
		}
		return price;

	}

}
4、创建业务类
public class Business {

	// 创建客车对象数组
	Bus[] bus = new Bus[4];

	public void initBus() {
		bus[0] = new Bus("京NY123456", "金杯", 800, 16);
		bus[1] = new Bus("京NY234567", "金杯", 1500, 34);
		bus[2] = new Bus("京NY345678", "金龙", 800, 16);
		bus[3] = new Bus("京NY456789", "金龙", 1500, 34);
	}

	// 创建轿车对象数组
	SaloonCar[] saloonCars = new SaloonCar[4];

	public void initSaloonCar() {
		saloonCars[0] = new SaloonCar("京NY888888", "宝马", 800, "x6");
		saloonCars[1] = new SaloonCar("京NY666666", "宝马", 600, "550i");
		saloonCars[2] = new SaloonCar("京NY555555", "别克", 300, "林荫大道");
		saloonCars[3] = new SaloonCar("京NY999999", "别克", 600, "GL8");
	}

	// 获取用户想要租的车的信息
	public Bus rentBus(String brand, int seat) {
		// 要租的客车初始值
		Bus mybus = null;
		// 遍历客车
		for (Bus i : bus) {
			// 对比用户租车信息
			if (i.getBrand().equals(brand) && i.getSeat() == seat) {
				mybus = i;
			}
		}
		return mybus;
	}

	public SaloonCar rentSaloonCar(String brand, String type) {
		// 要租的轿车初始值
		SaloonCar myrentSaloonCar = null;
		// 遍历轿车
		for (SaloonCar i : saloonCars) {
			// 对比用户租车信息
			if (i.getBrand().equals(brand) && i.getType().equals(type)) {
				myrentSaloonCar = i;
			}
		}
		return myrentSaloonCar;
	}
}
5、创建测试类
import java.util.Scanner;

public class RentCar {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		// 创建租车对象(创建租车公司)
		Business rentCar = new Business();
		// 初始化车辆信息(导入可租车辆)
		rentCar.initBus();
		rentCar.initSaloonCar();

		System.out.println("************欢迎光临四金租车公司************");
		System.out.println("请选择您要租的车型:1、轿车   2、客车");
		int i = input.nextInt();
		// 初始化用户选择车辆信息
		String brand = null;
		String type = null;
		int seat = 0;
		// 判断车型
		if (i == 1) {// 轿车
			System.out.println("请选择您要租车的品牌:1、宝马   2、别克");
			int j = input.nextInt();
			// 判断品牌
			if (j == 1) {// 宝马
				brand = "宝马";
				System.out.println("请选择您要租车的型号:1、x6   2、550i");
				int k = input.nextInt();
				// 判断型号
				if (k == 1) {
					type = "x6";
				} else if (k == 2) {
					type = "550i";
				}
			} else if (j == 2) {// 别克
				brand = "别克";
				System.out.println("请选择您要租车的型号:1、林荫大道   2、GL8");
				int k = input.nextInt();
				if (k == 1) {
					type = "林荫大道 ";
				} else if (k == 2) {
					type = "GL8";
				}
			}
			// 调用轿车的租金方法
			SaloonCar rentSaloonCar = rentCar.rentSaloonCar(brand, type);
			System.out.println("请输入您要租的天数:");
			int days = input.nextInt();
			double price = rentSaloonCar.rent(days);
			System.out.println("租车成功,您租车车牌号为:"
					+ rentSaloonCar.getLicensePlateNumber());
			System.out.println("您需要支付:" + price + "元");

		} else if (i == 2) {// 客车
			System.out.println("请选择您要租车的品牌:1、金杯   2、金龙");
			int j = input.nextInt();
			// 判断品牌
			if (j == 1) {
				brand = "金杯";
				System.out.println("请选择您要租车的座位数:1、16座   2、34座");
				int k = input.nextInt();
				// 判断座位数
				if (k == 1) {
					seat = 16;
				} else if (k == 2) {
					seat = 34;
				}
			} else if (j == 2) {
				brand = "金龙";
				System.out.println("请选择您要租车的座位数:1、16座   2、34座");
				int k = input.nextInt();
				if (k == 1) {
					seat = 16;
				} else if (k == 2) {
					seat = 34;
				}
			}
			// 调用客车的租金方法
			Bus rentBus = rentCar.rentBus(brand, seat);
			System.out.println("请输入您要租的天数:");
			int days = input.nextInt();
			double price = rentBus.rent(days);
			System.out.println("租车成功,您租车车牌号为:"
					+ rentBus.getLicensePlateNumber());
			System.out.println("您需要支付:" + price + "元");
		}

	}

}
6、运行结果

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/998710.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号