编写一个JAVA的图书管理系统主干程序
时间: 2023-10-21 08:03:40 浏览: 136
这里给出一个简单的图书管理系统的主干程序,你可以根据实际需求进行修改和完善:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookManagementSystem {
private List<Book> books;
private User currentUser;
private Scanner scanner;
public BookManagementSystem() {
books = new ArrayList<>();
currentUser = null;
scanner = new Scanner(System.in);
}
public void start() {
boolean exit = false;
while (!exit) {
displayMenu();
int choice = scanner.nextInt();
switch (choice) {
case 1:
register();
break;
case 2:
login();
break;
case 3:
addBook();
break;
case 4:
deleteBook();
break;
case 5:
borrowBook();
break;
case 6:
returnBook();
break;
case 7:
displayBooks();
break;
case 8:
exit = true;
break;
default:
System.out.println("Invalid choice, please try again.");
}
}
}
private void displayMenu() {
System.out.println("Welcome to Book Management System!");
if (currentUser == null) {
System.out.println("1. Register");
System.out.println("2. Login");
} else {
System.out.println("3. Add Book");
System.out.println("4. Delete Book");
System.out.println("5. Borrow Book");
System.out.println("6. Return Book");
}
System.out.println("7. Display Books");
System.out.println("8. Exit");
System.out.print("Please enter your choice: ");
}
private void register() {
System.out.print("Please enter your username: ");
String username = scanner.next();
System.out.print("Please enter your password: ");
String password = scanner.next();
currentUser = new User(username, password);
System.out.println("Registration successful!");
}
private void login() {
System.out.print("Please enter your username: ");
String username = scanner.next();
System.out.print("Please enter your password: ");
String password = scanner.next();
if (currentUser != null && currentUser.getUsername().equals(username) && currentUser.getPassword().equals(password)) {
System.out.println("Login successful!");
} else {
System.out.println("Invalid username or password!");
}
}
private void addBook() {
if (currentUser == null) {
System.out.println("Please login first!");
return;
}
System.out.print("Please enter the book name: ");
String name = scanner.next();
System.out.print("Please enter the author name: ");
String author = scanner.next();
books.add(new Book(name, author));
System.out.println("Book added successfully!");
}
private void deleteBook() {
if (currentUser == null) {
System.out.println("Please login first!");
return;
}
System.out.print("Please enter the book name: ");
String name = scanner.next();
for (Book book : books) {
if (book.getName().equals(name)) {
books.remove(book);
System.out.println("Book deleted successfully!");
return;
}
}
System.out.println("Book not found!");
}
private void borrowBook() {
if (currentUser == null) {
System.out.println("Please login first!");
return;
}
System.out.print("Please enter the book name: ");
String name = scanner.next();
for (Book book : books) {
if (book.getName().equals(name)) {
if (!book.isBorrowed()) {
book.setBorrowed(true);
System.out.println("Book borrowed successfully!");
return;
} else {
System.out.println("Book already borrowed!");
return;
}
}
}
System.out.println("Book not found!");
}
private void returnBook() {
if (currentUser == null) {
System.out.println("Please login first!");
return;
}
System.out.print("Please enter the book name: ");
String name = scanner.next();
for (Book book : books) {
if (book.getName().equals(name)) {
if (book.isBorrowed()) {
book.setBorrowed(false);
System.out.println("Book returned successfully!");
return;
} else {
System.out.println("Book not borrowed!");
return;
}
}
}
System.out.println("Book not found!");
}
private void displayBooks() {
System.out.println("Book Name\tAuthor\tBorrowed");
for (Book book : books) {
System.out.println(book.getName() + "\t" + book.getAuthor() + "\t" + book.isBorrowed());
}
}
public static void main(String[] args) {
BookManagementSystem system = new BookManagementSystem();
system.start();
}
}
class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
class Book {
private String name;
private String author;
private boolean borrowed;
public Book(String name, String author) {
this.name = name;
this.author = author;
this.borrowed = false;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public boolean isBorrowed() {
return borrowed;
}
public void setBorrowed(boolean borrowed) {
this.borrowed = borrowed;
}
}
```
这个程序实现了一个简单的图书管理系统,包括用户注册、登录、添加、删除、借阅、归还图书等功能。其中,User类表示用户,Book类表示图书,BookManagementSystem类为主干程序,包含各种操作图书和用户的方法。
阅读全文