java简单共享单车管理程序代码要求实现功能选择,借车还车,按时计费,账号充值和结账等功能
时间: 2024-03-25 12:36:46 浏览: 69
基于Java的共享单车管理系统的设计与实现.zip
5星 · 资源好评率100%
以下是一个简单的Java共享单车管理程序的代码实现,包括了您提到的功能选择、借车、还车、按时计费、账号充值和结账等功能:
```java
import java.util.Scanner;
public class BikeSharingSystem {
private static double balance = 0;
private static int availableBikes = 10;
private static final double RENTAL_RATE = 1.5;
private static final int RENTAL_DURATION = 60;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("请选择功能:");
System.out.println("1. 借车");
System.out.println("2. 还车");
System.out.println("3. 账号充值");
System.out.println("4. 结账");
System.out.println("0. 退出");
choice = scanner.nextInt();
switch (choice) {
case 1:
rentBike();
break;
case 2:
returnBike();
break;
case 3:
rechargeAccount();
break;
case 4:
settleAccounts();
break;
case 0:
System.out.println("欢迎下次使用!");
break;
default:
System.out.println("输入错误,请重新输入!");
}
} while (choice != 0);
scanner.close();
}
private static void rentBike() {
if (availableBikes <= 0) {
System.out.println("抱歉,没有可用的单车了!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入借车时间(分钟):");
int rentalTime = scanner.nextInt();
if (rentalTime <= 0) {
System.out.println("借车时间应大于0!");
return;
}
if (rentalTime > RENTAL_DURATION) {
System.out.println("借车时间超过" + RENTAL_DURATION + "分钟,按照" + RENTAL_DURATION + "分钟计费!");
rentalTime = RENTAL_DURATION;
}
double rentalFee = rentalTime * RENTAL_RATE;
balance -= rentalFee;
availableBikes--;
System.out.println("借车成功!借车时间:" + rentalTime + "分钟,租金:" + rentalFee + "元,当前可用单车数量:" + availableBikes);
scanner.close();
}
private static void returnBike() {
if (availableBikes >= 10) {
System.out.println("抱歉,车位已满,请寻找其他停车点!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入还车时间(分钟):");
int rentalTime = scanner.nextInt();
if (rentalTime <= 0) {
System.out.println("还车时间应大于0!");
return;
}
if (rentalTime > RENTAL_DURATION) {
System.out.println("还车时间超过" + RENTAL_DURATION + "分钟,按照" + RENTAL_DURATION + "分钟计费!");
rentalTime = RENTAL_DURATION;
}
double rentalFee = rentalTime * RENTAL_RATE;
balance += rentalFee;
availableBikes++;
System.out.println("还车成功!还车时间:" + rentalTime + "分钟,租金:" + rentalFee + "元,当前可用单车数量:" + availableBikes);
scanner.close();
}
private static void rechargeAccount() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入充值金额:");
double amount = scanner.nextDouble();
if (amount <= 0) {
System.out.println("充值金额应大于0!");
return;
}
balance += amount;
System.out.println("充值成功!当前余额:" + balance);
scanner.close();
}
private static void settleAccounts() {
if (balance < 0) {
System.out.println("当前余额为负数,需还款:" + Math.abs(balance) + "元");
} else {
System.out.println("当前余额为正数,可提现:" + balance + "元");
}
}
}
```
此程序的核心思想是使用控制台输入和输出来模拟真实的共享单车管理系统。在程序运行期间,用户可以选择不同的功能来借车、还车、充值或结账。其中,借车和还车的租金按时计费,每小时1.5元,最多计费60分钟;账户充值和结账则直接修改用户的余额。
阅读全文