用Java实现一个汽车出租管理程序,不能使用数据库,只能使用面向对象和数组
时间: 2024-01-24 22:16:44 浏览: 27
好的,我可以帮你实现一个简单的汽车出租管理程序。程序会有两个类:Car和CarRentalSystem。
首先,我们来定义Car类,它包含以下属性:车牌号(plateNumber)、品牌(brand)、颜色(color)、每日租金(rentalPrice)、是否被出租(isRented)。
```java
public class Car {
private String plateNumber;
private String brand;
private String color;
private double rentalPrice;
private boolean isRented;
public Car(String plateNumber, String brand, String color, double rentalPrice) {
this.plateNumber = plateNumber;
this.brand = brand;
this.color = color;
this.rentalPrice = rentalPrice;
this.isRented = false;
}
public String getPlateNumber() {
return plateNumber;
}
public String getBrand() {
return brand;
}
public String getColor() {
return color;
}
public double getRentalPrice() {
return rentalPrice;
}
public boolean isRented() {
return isRented;
}
public void rent() {
isRented = true;
}
public void returnCar() {
isRented = false;
}
}
```
接下来是CarRentalSystem类,它包含以下属性:汽车库存数组(cars)和租车记录数组(rentedCars)。
```java
public class CarRentalSystem {
private Car[] cars;
private Car[] rentedCars;
public CarRentalSystem(int numOfCars) {
cars = new Car[numOfCars];
rentedCars = new Car[numOfCars];
}
public void addCar(Car car) {
for (int i = 0; i < cars.length; i++) {
if (cars[i] == null) {
cars[i] = car;
break;
}
}
}
public void rentCar(String plateNumber) {
for (int i = 0; i < cars.length; i++) {
if (cars[i] != null && cars[i].getPlateNumber().equals(plateNumber)) {
cars[i].rent();
for (int j = 0; j < rentedCars.length; j++) {
if (rentedCars[j] == null) {
rentedCars[j] = cars[i];
break;
}
}
break;
}
}
}
public void returnCar(String plateNumber) {
for (int i = 0; i < rentedCars.length; i++) {
if (rentedCars[i] != null && rentedCars[i].getPlateNumber().equals(plateNumber)) {
rentedCars[i].returnCar();
for (int j = 0; j < cars.length; j++) {
if (cars[j] == null) {
cars[j] = rentedCars[i];
break;
}
}
rentedCars[i] = null;
break;
}
}
}
public void displayAvailableCars() {
System.out.println("Available cars:");
for (int i = 0; i < cars.length; i++) {
if (cars[i] != null && !cars[i].isRented()) {
System.out.println(cars[i].getPlateNumber() + " " + cars[i].getBrand() + " " + cars[i].getColor() + " $" + cars[i].getRentalPrice() + "/day");
}
}
}
public void displayRentedCars() {
System.out.println("Rented cars:");
for (int i = 0; i < rentedCars.length; i++) {
if (rentedCars[i] != null) {
System.out.println(rentedCars[i].getPlateNumber() + " " + rentedCars[i].getBrand() + " " + rentedCars[i].getColor() + " $" + rentedCars[i].getRentalPrice() + "/day");
}
}
}
}
```
现在我们可以在main函数中测试我们的程序了。
```java
public static void main(String[] args) {
CarRentalSystem rentalSystem = new CarRentalSystem(10);
// 添加汽车
rentalSystem.addCar(new Car("001", "Toyota", "Red", 50));
rentalSystem.addCar(new Car("002", "Honda", "Blue", 60));
rentalSystem.addCar(new Car("003", "Ford", "Yellow", 70));
// 显示可租汽车
rentalSystem.displayAvailableCars();
// 租一辆汽车
rentalSystem.rentCar("001");
// 显示已租汽车
rentalSystem.displayRentedCars();
// 还一辆汽车
rentalSystem.returnCar("001");
// 显示可租汽车和已租汽车
rentalSystem.displayAvailableCars();
rentalSystem.displayRentedCars();
}
```
输出结果:
```
Available cars:
001 Toyota Red $50.0/day
002 Honda Blue $60.0/day
003 Ford Yellow $70.0/day
Rented cars:
001 Toyota Red $50.0/day
Available cars:
001 Toyota Red $50.0/day
002 Honda Blue $60.0/day
003 Ford Yellow $70.0/day
Rented cars:
```