eclipse编写一个借书卡程序,要求每张借书卡信息包含账号、持卡人姓名、身份证号码、地址、已借书数、可借书数、本次借书数、本次还书数。方法有借书、还书和查询。要求根据持卡人不同操作,显示不同信息。当借书操作后,显示本次借书数及已借书数当还书操作后,显示本次还书数和已借书数。
时间: 2024-10-20 07:16:40 浏览: 32
在Eclipse中创建一个借书卡程序,首先需要设计一个`BookCard`类作为基本模型,包含如账号、姓名、身份证号、地址等属性,并声明借书、还书和查询方法。下面是一个简单的例子:
```java
public class BookCard {
private String account;
private String holderName;
private String idNumber;
private String address;
private int borrowedBooks;
private int availableBooks;
private int currentBorrowedCount = 0;
private int currentReturnCount = 0;
public BookCard(String account, String name, String id, String addr) {
this.account = account;
this.holderName = name;
this.idNumber = id;
this.address = addr;
this.borrowedBooks = 0;
this.availableBooks = 0;
}
// 借书方法
public void borrowBook(int bookCount) {
if (bookCount <= availableBooks) {
currentBorrowedCount += bookCount;
availableBooks -= bookCount;
System.out.println("借书成功,本次借了" + bookCount + "本书,当前已借书总数:" + currentBorrowedCount);
} else {
System.out.println("抱歉,无法借阅更多书籍。");
}
}
// 还书方法
public void returnBook(int bookCount) {
if (currentBorrowedCount >= bookCount) {
currentBorrowedCount -= bookCount;
availableBooks += bookCount;
System.out.println("还书成功,还了" + bookCount + "本书,当前已借书总数:" + currentBorrowedCount);
} else {
System.out.println("还书数量超过已借书量。");
}
}
// 查询方法
public void query() {
System.out.println("账号:" + account +
", 持卡人姓名:" + holderName +
", 身份证号码:" + idNumber +
", 地址:" + address +
", 已借书数:" + currentBorrowedCount +
", 可借书数:" + availableBooks);
}
}
// 根据用户输入的不同,展示对应持卡人的信息
public static void main(String[] args) {
BookCard card = new BookCard("123456", "张三", "123456789012345678", "上海市浦东新区...");
card.query();
// 用户选择操作
Scanner scanner = new Scanner(System.in);
System.out.println("请选择操作:1.借书 2.还书 3.查询");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入借书数量:");
int borrowCount = scanner.nextInt();
card.borrowBook(borrowCount);
break;
case 2:
System.out.println("请输入还书数量:");
borrowCount = scanner.nextInt();
card.returnBook(borrowCount);
break;
default:
card.query();
}
}
```
阅读全文