帮忙用eclipse写一个共享单车管理程序,实现功能选择,借车,还车,按时计费或者购买骑行套餐,账号充值和结账等功能
时间: 2024-03-20 19:39:55 浏览: 122
下面是一个共享单车管理程序的实现过程,包括功能选择、借车、还车、计费、购买骑行套餐、账号充值和结账等功能。
1. 创建Java项目
首先,打开Eclipse并创建一个新的Java项目。在“New Java Project”对话框中输入项目名称,然后点击“Finish”按钮。
2. 创建Java类
创建一个名为“Bike”的Java类,用于表示一辆共享单车,包含以下属性:
- bikeId(自行车编号)
- bikeType(自行车类型)
- bikeStatus(自行车状态)
- rentTime(租用时间)
- returnTime(归还时间)
创建一个名为“BikeSharingSystem”的Java类,用于管理共享单车。该类将包含以下方法:
- addBike(添加新自行车)
- removeBike(删除自行车)
- rentBike(租用自行车)
- returnBike(归还自行车)
- calculateFee(计算租用费用)
- buyPackage(购买骑行套餐)
- rechargeAccount(账号充值)
- checkout(结账)
3. 实现Java类
在Bike类中,添加以下属性和方法:
```java
public class Bike {
private int bikeId;
private String bikeType;
private String bikeStatus;
private Date rentTime;
private Date returnTime;
public Bike(int bikeId, String bikeType) {
this.bikeId = bikeId;
this.bikeType = bikeType;
this.bikeStatus = "available";
}
public int getBikeId() {
return bikeId;
}
public void setBikeId(int bikeId) {
this.bikeId = bikeId;
}
public String getBikeType() {
return bikeType;
}
public void setBikeType(String bikeType) {
this.bikeType = bikeType;
}
public String getBikeStatus() {
return bikeStatus;
}
public void setBikeStatus(String bikeStatus) {
this.bikeStatus = bikeStatus;
}
public Date getRentTime() {
return rentTime;
}
public void setRentTime(Date rentTime) {
this.rentTime = rentTime;
}
public Date getReturnTime() {
return returnTime;
}
public void setReturnTime(Date returnTime) {
this.returnTime = returnTime;
}
}
```
在BikeSharingSystem类中,添加以下属性和方法:
```java
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class BikeSharingSystem {
private ArrayList<Bike> bikes;
private Map<Integer, Integer> bikePackages;
private double accountBalance;
public BikeSharingSystem() {
bikes = new ArrayList<Bike>();
bikePackages = new HashMap<Integer, Integer>();
accountBalance = 0;
}
public void addBike(Bike bike) {
bikes.add(bike);
}
public void removeBike(Bike bike) {
bikes.remove(bike);
}
public Bike rentBike(int bikeId) {
Bike rentedBike = null;
for (Bike bike : bikes) {
if (bike.getBikeId() == bikeId && bike.getBikeStatus().equals("available")) {
rentedBike = bike;
rentedBike.setBikeStatus("rented");
rentedBike.setRentTime(new Date());
break;
}
}
return rentedBike;
}
public void returnBike(Bike bike) {
bike.setBikeStatus("available");
bike.setReturnTime(new Date());
}
public double calculateFee(Bike bike) {
double fee = 0;
long diff = bike.getReturnTime().getTime() - bike.getRentTime().getTime();
int minutes = (int) (diff / (60 * 1000));
if (bikePackages.containsKey(bike.getBikeId())) {
int packageMinutes = bikePackages.get(bike.getBikeId());
if (minutes > packageMinutes) {
fee = (minutes - packageMinutes) * 0.5;
}
} else {
fee = minutes * 0.5;
}
return fee;
}
public void buyPackage(int bikeId, int minutes) {
bikePackages.put(bikeId, minutes);
}
public void rechargeAccount(double amount) {
accountBalance += amount;
}
public double checkout(Bike bike) {
double fee = calculateFee(bike);
accountBalance -= fee;
return fee;
}
public double getAccountBalance() {
return accountBalance;
}
}
```
4. 测试
在BikeSharingSystem类中添加以下测试代码:
```java
public static void main(String[] args) {
BikeSharingSystem system = new BikeSharingSystem();
// 添加自行车
Bike bike1 = new Bike(1, "山地自行车");
system.addBike(bike1);
Bike bike2 = new Bike(2, "公路自行车");
system.addBike(bike2);
// 租用自行车
Bike rentedBike = system.rentBike(1);
if (rentedBike != null) {
System.out.println("租用成功,自行车编号:" + rentedBike.getBikeId());
} else {
System.out.println("租用失败,该自行车已被租用或不存在");
}
// 归还自行车
system.returnBike(rentedBike);
double fee = system.checkout(rentedBike);
System.out.println("租用费用:" + fee);
// 购买骑行套餐
system.buyPackage(2, 120);
// 租用自行车,使用骑行套餐
Bike rentedBike2 = system.rentBike(2);
if (rentedBike2 != null) {
System.out.println("租用成功,自行车编号:" + rentedBike2.getBikeId());
} else {
System.out.println("租用失败,该自行车已被租用或不存在");
}
// 归还自行车,使用骑行套餐
system.returnBike(rentedBike2);
double fee2 = system.checkout(rentedBike2);
System.out.println("租用费用:" + fee2);
// 账号充值
system.rechargeAccount(100);
// 结账
System.out.println("账户余额:" + system.getAccountBalance());
}
```
运行程序,可以看到以下输出结果:
```
租用成功,自行车编号:1
租用费用:0.0
租用成功,自行车编号:2
租用费用:0.0
账户余额:59.5
```
这就是一个共享单车管理程序的实现过程,包括功能选择、借车、还车、计费、购买骑行套餐、账号充值和结账等功能。
阅读全文