用java面向对象程序设计实现汽车租赁系统
时间: 2024-05-04 09:19:05 浏览: 152
好的,让我们来实现一个简单的汽车租赁系统。首先,我们需要定义几个类来表示不同的对象。
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
```
以上就是一个简单的汽车租赁系统的实现。
阅读全文