面向对象练习-汽车租赁系统

目录

1.父类-汽车类

2.子类-轿车类

3.子类-客车类

4.业务类

5.测试类


面向对象练习-汽车租赁系统_第1张图片

1.父类-汽车类

package dh05;

/**
 * 汽车类-父类-抽象类
 * 
 * @author XXX
 * 
 *         2023年6月15日下午12:00:21
 */
public abstract class Car {

	private String carId;// 车牌号
	private String brand;// 品牌
	private int rent;// 日租金

	// 计算租金(抽象方法):根据用户租赁时间进行计算
	public abstract double price(int days);

	// 构造方法
	public Car() {
	}

	public Car(String carId, String brand, int rent) {
		this.carId = carId;
		this.brand = brand;
		this.rent = rent;
	}
	
	
	@Override
	public String toString() {
		return "Car [carId=" + carId + ", brand=" + brand + ", rent=" + rent + "]";
	}

	// ------------------------------------------------------
	// set&get
	public String getCarId() {
		return carId;
	}

	public void setCarId(String carId) {
		this.carId = carId;
	}

	public String getBrand() {
		return brand;
	}

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

	public int getRent() {
		return rent;
	}

	public void setRent(int rent) {
		this.rent = rent;
	}
//------------------------------------------------------	

}

2.子类-轿车类

package dh05;

/**
 * 轿车类
 * 
 * @author XXX
 * 
 *         2023年6月15日下午4:20:44
 */
public class Sedan extends Car {

	private String type;// 型号

	// 构造方法
	public Sedan() {
	}

	public Sedan(String carId, String brand, int rent, String type) {
		super(carId, brand, rent);
		this.type = type;
	}

	// 重写计算租金
	public double price(int days) {
		// 租金计算=日租金*天数*折扣
		double money = this.getRent() * days;
		if (days > 150) {
			money = money * 0.7;
		} else if (days > 30 && days <= 150) {
			money = money * 0.8;
		} else if (days > 7 && days <= 30) {
			money = money * 0.9;
		}

		return money;
	}

	@Override
	public String toString() {
		return "Sedan [type=" + type + ", getCarId()=" + getCarId() + ", getBrand()=" + getBrand() + ", getRent()="
				+ getRent() + "]";
	}


//----------------------------------------------------
	public String getType() {
		return type;
	}

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

}

3.子类-客车类

package dh05;

/**
 * 客车类
 * 
 * @author XXX
 * 
 *         2023年6月15日下午12:00:36
 */
public class PassengerCar extends Car {

	private int seats;// 座位数

	// 构造方法
	public PassengerCar() {
	}

	public PassengerCar(String carId, String brand, int rent, int seats) {
		super(carId, brand, rent);
		this.seats = seats;
	}
	

	@Override
	public String toString() {
		return "PassengerCar [seats=" + seats + ", getCarId()=" + getCarId()
				+ ", getBrand()=" + getBrand() + ", getRent()=" + getRent() + "]";
	}

	@Override
	public double price(int days) {
		// 租金计算=日租金*天数*折扣
		double money = this.getRent() * days;

		if (days >= 150) {
			money = money * 0.6;
		} else if (days >= 30 && days < 150) {
			money = money * 0.7;
		} else if (days >= 7 && days < 30) {
			money = money * 0.8;
		} else if (days >= 3 && days < 7) {
			money = money * 0.9;
		}
		return money;
	}

//--------------------------------------------------	
	public int getSeats() {
		return seats;
	}

	public void setSeats(int seats) {
		this.seats = seats;
	}
//--------------------------------------------------

}

4.业务类

package dh05;

/**
 * 业务类
 * 
 * @author XXX
 * 
 *         2023年6月15日下午7:01:25
 */
public class Work {
	// 初始化汽车数组信息
	Car[] cars = new Car[8];

	// 车辆信息初始化(initialization)
	public void initialization() {
//String carId,String brand,int rent,String type
		// 父类引用指向子类对象
		cars[0] = new Sedan("京N11111", "宝马", 800, "X6");
		cars[1] = new Sedan("京N22222", "宝马", 600, "550i");
		cars[2] = new Sedan("京N33333", "别克", 300, "林萌大道");
		cars[3] = new Sedan("京N44444", "别克", 600, "GL8");
//String carId, String brand, int rent, int seats		
		cars[4] = new PassengerCar("京N55555", "金杯", 800, 16);
		cars[5] = new PassengerCar("京N66666", "金龙", 800, 16);
		cars[6] = new PassengerCar("京N77777", "金杯", 1500, 34);
		cars[7] = new PassengerCar("京N88888", "金龙", 1500, 34);
	}

	// 租车,传入参数进行比较(品牌,型号,座位数)
	public Car rentCar(String brand, int seats, String type) {
		Car carName = null;
		// java遍历数组方法之foreach循环,car2变量
		for (Car car : cars) {
			// 判断car是否为轿车
			if (car instanceof Sedan) {
				// 轿车,向下转型,强转为轿车类型
				Sedan sedan = (Sedan) car;
				// 轿车的品牌及型号与用户所需吻合
				if (sedan.getBrand().equals(brand) && sedan.getType().equals(type)) {
					carName = sedan;
					break;
				}
			} else if(car instanceof PassengerCar){
				// 客车,向下转型,强转为客车类型
				PassengerCar passengerCar = (PassengerCar) car;
				// 轿车的品牌及型号与用户所需吻合
				if (passengerCar.getBrand().equals(brand) && (passengerCar.getSeats()== seats)) {
					carName = passengerCar;
					break;
				}
			}
		}

		return carName;
	}

}

5.测试类

package dh05;

import java.util.Scanner;

/**
 * 测试类
 * 
 * @author XXX
 * 
 *         2023年6月15日下午4:21:06
 */
public class Test {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String brand = null, type = null;
		int seats = 0;
		Work work = new Work();
		work.initialization();// 初始化信息
		System.out.println("------------欢迎进入租车系统----------");
		boolean bool = true;
		while (bool) {
			System.out.print("请选择您需要的品牌(1:轿车 2:客车 ):");
			int num1 = sc.nextInt();
			// 轿车类
			if (num1 == 1) {
				// 方法一:if判断进行赋值
				System.out.print("请选择品牌(1:宝马  2:别克):");
				if (sc.nextInt() == 1) {
					brand = "宝马";
					System.out.print("请选择型号(1:X6 2:550i):");
					type = (sc.nextInt() == 1) ? "X6" : "550i";
				} else if (sc.nextInt() == 2) {
					brand = "别克";
					System.out.print("请选择型号(1:林萌大道 2:GL8):");
					type = (sc.nextInt() == 1) ? "林萌大道" : "GL8";
				}
			} else if (num1 == 2) {
				// 方法二:条件判断进行赋值
				System.out.print("请选择品牌(1:金杯  2:金龙):");
				brand = (sc.nextInt() == 1) ? "金杯" : "金龙";
				System.out.print("请选择座位数(1:16座  2:34座):");
				seats = (sc.nextInt() == 1) ? 16 : 34;
			}
			// 根据选择的内容 获得相应的轿车 调用业务类
			Car car = work.rentCar(brand, seats, type);
			System.out.println("您租的车为:" + car.toString());
			System.out.print("请选择租赁天数:");
			int days = sc.nextInt();
			double sunMoney = car.price(days);
			System.out.println("总金额为:" + sunMoney);
			System.out.println("--------------------------------------");
			System.out.print("输入0退出系统,输入其他的继续:");
			int num3 = sc.nextInt();
			if (num3==0) {
				bool = false;
			}
		}
//---------------------------------------------------------

	}

}

你可能感兴趣的:(面向对象,汽车,eclipse,java)