简单汽车租赁系统JAVA程序

//车类
public abstract class Vehicle {
	//车牌号    品牌    日租金
	private String id;
	private String brand;
	private int perRent;
	
	public Vehicle(){}
	//Vehicle的带参构造方法
	public Vehicle(String id, String brand, int perRent) {
		this.id = id;
		this.brand = brand;
		this.perRent = perRent;
	}

	public void setId(String id){
		this.id=id ;
	}
	public String getId(){
		return id;
	}
	
	public void setBrand(String brand){
		this.brand=brand;
	}
	public String getBrand(){
		return brand;
	}
	
	public void setPerRent(int perRent){
		this.perRent=perRent;
	}
	public int getPerRent(){
		return perRent;
	}
	//抽象方法计算租金
	public abstract double calcRent(int days);
	
}

//轿车类
public class Car extends Vehicle{
	//型号
	private String type;
	public void setType(String type){
		this.type=type;
	}
	public String getType(){
		return type;
	}
	
	public Car(){}
	//Car的带参构造方法
	public Car(String id, String brand, int perRent,String type) {
		super(id,brand,perRent);
		this.type = type;
	}
	//重写父类的计算租金方法:根据自己的计算租金规则
	public double calcRent(int days) {
		double price = this.getPerRent()*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;
	}
}

//客车类
public class Bus extends Vehicle{
	//座位数
	private int seatCount;
	public void setSeatCount(int seatCount){
		this.seatCount=seatCount;
	}
	public int getSeatCount(){
		return seatCount;
	}
	

	public Bus(){}
	//Bus的带参构造方法
	public Bus(String id,String brand,int perRent,int seatCount){
		super(id,brand,perRent);
		this.seatCount = seatCount;
	}
	//重写父类的计算租金方法:根据自己的计算租金规则
	public double calcRent(int days) {
		double price = this.getPerRent()*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;
		
	}
}

//汽车业务类
public class Operation {
	public Vehicle[] vehicle = new Vehicle[8];
//初始化汽车信息
	public void init(){
		vehicle[0] = new Car("京NY28588","宝马",800,"X6");   //vehicle v = new Car();
		vehicle[1] = new Car("京CNY32584","宝马",600,"550i");   //vehicle v = new Car();
		vehicle[2] = new Car("京NT37465","别克",300,"林荫大道");   //vehicle v = new Car();
		vehicle[3] = new Car("京NT96968","别克",600,"GL8");   //vehicle v = new Car();
		vehicle[4] = new Bus("京6566754","金杯",800,16);   //vehicle v = new Bus();
		vehicle[5] = new Bus("京8696997","金龙",800,16);   //vehicle v = new Bus();
		vehicle[6] = new Bus("京9696996","金杯",1500,34);   //vehicle v = new Bus();
		vehicle[7] = new Bus("京8696998","金龙",1500,34);   //vehicle v = new Bus();
	}
	//租车:根据用户提供的条件去汽车数组中查找相应车辆并返回
	//如果租赁的是轿车   需要条件:品牌        型号
	//如果租赁的是客车   需要条件:品牌        座位数
	//简单工厂模式
	public Vehicle zuChe(String brand,String type,int seatCount){
		Vehicle che = null;
		//for循环遍历数组vehicle
		for(Vehicle myche : vehicle){
			//判断Vehicle类的myche的类型是否和Car一样
			if(myche instanceof Car){
				//Vehicle类的myche向下转型变成子类Car
				Car car = (Car)myche;
				if(car.getBrand().equals(brand) && car.getType().equals(type)){
					che=car;
					break;
				}
			}else{
				//Vehicle类的myche向下转型变成子类Bus
				Bus bus = (Bus)myche;
				if(bus.getBrand().equals(brand) && bus.getSeatCount() == seatCount){
					che=bus;
					break;
				}
			}
		}
		return che;
		
	}
}

//汽车租赁
public class Rent {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Operation operation = new Operation();
		//租赁公司界面
		operation.init();
		System.out.println("**********欢迎光临租赁公司**********");
		System.out.println("1.轿车\t\t2.客车");
		System.out.println("请选择您要租赁的汽车类型:(1.轿车    2.客车)");
		int cheType = input.nextInt();
		String brand = "";//品牌
		String type = "";//型号
		int seatCount = 0;//座位数
		//收集用户条件
		if(cheType == 1){
			//租赁轿车
			System.out.println("请选择您要租赁的轿车品牌:(1.别克    2.宝马)");
			int choose = input.nextInt();
			if(choose == 1){
				brand="别克";
				System.out.println("请选择您要租赁的汽车型号:(1.林荫大道    2.GL8)");
				type=(input.nextInt() == 1)?"林荫大道":"GL8";
			}else if(choose == 2){
				brand="宝马";
				System.out.println("请选择您要租赁的汽车型号:(1.X6   2.550i)");
				type=(input.nextInt() == 1)?"X6":"550i";
			}
			
		}else if(cheType == 2){
			//租赁客车
			type= "";
			System.out.println("请选择您要租赁的客车品牌:(1.金杯    2.金龙)");
				brand=(input.nextInt()==1)?"金杯":"金龙";
				System.out.println("请选择您要租赁的客车座位数:(1.16座    2.32座)");
				seatCount=(input.nextInt() == 1)?16:34;
			}
		//租车
		Vehicle che = operation.zuChe(brand, type, seatCount);
		System.out.println("请输入您的租赁天数:");
		int days = input.nextInt();
		double money = che.calcRent(days);
		System.out.println("租车成功,请按照如下车牌号提车:"+che.getId());
		System.out.println("您需要支付:"+money+"元");
	}

}
  • 32
    点赞
  • 149
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
好的,让我们来实现一个简单汽车租赁系统。首先,我们需要定义几个类来表示不同的对象。 1. `Car`类,表示汽车对象,包含以下属性: - `id`:车辆编号 - `brand`:品牌 - `model`:型号 - `pricePerDay`:每天租金 - `isAvailable`:是否可用 并且需要定义一些方法来获取和设置这些属性。 ```java public class Car { private String id; private String brand; private String model; private double pricePerDay; private boolean isAvailable; public Car(String id, String brand, String model, double pricePerDay, boolean isAvailable) { this.id = id; this.brand = brand; this.model = model; this.pricePerDay = pricePerDay; this.isAvailable = isAvailable; } public String getId() { return id; } public String getBrand() { return brand; } public String getModel() { return model; } public double getPricePerDay() { return pricePerDay; } public boolean isAvailable() { return isAvailable; } public void setAvailable(boolean available) { isAvailable = available; } } ``` 2. `Customer`类,表示顾客对象,包含以下属性: - `id`:顾客编号 - `name`:名字 - `rentedCar`:租用的汽车对象 并且需要定义一些方法来获取和设置这些属性。 ```java public class Customer { private String id; private String name; private Car rentedCar; public Customer(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public String getName() { return name; } public Car getRentedCar() { return rentedCar; } public void setRentedCar(Car rentedCar) { this.rentedCar = rentedCar; } } ``` 3. `CarRentalSystem`类,表示汽车租赁系统,包含以下方法: - `addCar`:添加汽车到系统中 - `removeCar`:从系统中删除汽车 - `rentCar`:顾客租用汽车 - `returnCar`:顾客归还汽车 - `getAvailableCars`:获取所有可用的汽车 - `getRentedCars`:获取所有已租用的汽车 - `getCustomers`:获取所有顾客 ```java import java.util.ArrayList; import java.util.List; public class CarRentalSystem { private List<Car> cars; private List<Customer> customers; public CarRentalSystem() { cars = new ArrayList<>(); customers = new ArrayList<>(); } public void addCar(Car car) { cars.add(car); } public void removeCar(Car car) { cars.remove(car); } public void rentCar(Customer customer, Car car) { if (!car.isAvailable()) { System.out.println("Sorry, this car is not available for rent."); return; } customer.setRentedCar(car); car.setAvailable(false); } public void returnCar(Customer customer, Car car) { customer.setRentedCar(null); car.setAvailable(true); } public List<Car> getAvailableCars() { List<Car> availableCars = new ArrayList<>(); for (Car car : cars) { if (car.isAvailable()) { availableCars.add(car); } } return availableCars; } public List<Car> getRentedCars() { List<Car> rentedCars = new ArrayList<>(); for (Car car : cars) { if (!car.isAvailable()) { rentedCars.add(car); } } return rentedCars; } public List<Customer> getCustomers() { return customers; } } ``` 现在我们可以编写一个简单的测试程序来测试我们的汽车租赁系统。 ```java public class CarRentalSystemTest { public static void main(String[] args) { CarRentalSystem carRentalSystem = new CarRentalSystem(); Car car1 = new Car("001", "Toyota", "Corolla", 50.0, true); Car car2 = new Car("002", "Honda", "Civic", 60.0, true); Car car3 = new Car("003", "Mazda", "CX-5", 70.0, true); carRentalSystem.addCar(car1); carRentalSystem.addCar(car2); carRentalSystem.addCar(car3); Customer customer1 = new Customer("001", "John"); Customer customer2 = new Customer("002", "Mary"); carRentalSystem.rentCar(customer1, car1); carRentalSystem.rentCar(customer2, car2); System.out.println("Available cars:"); for (Car car : carRentalSystem.getAvailableCars()) { System.out.println(car.getBrand() + " " + car.getModel()); } System.out.println("Rented cars:"); for (Car car : carRentalSystem.getRentedCars()) { System.out.println(car.getBrand() + " " + car.getModel()); } System.out.println("Customers:"); for (Customer customer : carRentalSystem.getCustomers()) { System.out.println(customer.getName()); } carRentalSystem.returnCar(customer1, car1); System.out.println("Available cars:"); for (Car car : carRentalSystem.getAvailableCars()) { System.out.println(car.getBrand() + " " + car.getModel()); } System.out.println("Rented cars:"); for (Car car : carRentalSystem.getRentedCars()) { System.out.println(car.getBrand() + " " + car.getModel()); } System.out.println("Customers:"); for (Customer customer : carRentalSystem.getCustomers()) { System.out.println(customer.getName()); } } } ``` 输出结果如下: ``` Available cars: Honda Civic Mazda CX-5 Rented cars: Toyota Corolla Customers: John Mary Available cars: Honda Civic Mazda CX-5 Toyota Corolla Rented cars: Customers: John Mary ``` 以上就是一个简单汽车租赁系统的实现。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值