以下是一个稍微复杂一些的Java代码,它的作用是实现一个简单的在线图书馆系统,可以添加、删除、借阅、归还和查询图书信息:
import java.util.ArrayList;
import java.util.Scanner;
public class LibrarySystem {
private ArrayList<Book> books;
private ArrayList<User> users;
private Scanner scanner;
public LibrarySystem() {
books = new ArrayList<>();
users = new ArrayList<>();
scanner = new Scanner(System.in);
}
public void run() {
int choice = 0;
do {
System.out.println("Welcome to the Library System!");
System.out.println("1. Add book");
System.out.println("2. Remove book");
System.out.println("3. Borrow book");
System.out.println("4. Return book");
System.out.println("5. Search book");
System.out.println("6. Register user");
System.out.println("7. Remove user");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
addBook();
break;
case 2:
removeBook();
break;
case 3:
borrowBook();
break;
case 4:
returnBook();
break;
case 5:
searchBook();
break;
case 6:
registerUser();
break;
case 7:
removeUser();
break;
case 8:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 8);
}
private void addBook() {
System.out.print("Enter book title: ");
String title = scanner.next();
System.out.print("Enter book author: ");
String author = scanner.next();
System.out.print("Enter book ISBN: ");
String isbn = scanner.next();
Book book = new Book(title, author, isbn);
books.add(book);
System.out.println("Book added successfully!");
}
private void removeBook() {
System.out.print("Enter book ISBN: ");
String isbn = scanner.next();
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if (book.getIsbn().equals(isbn)) {
books.remove(i);
System.out.println("Book removed successfully!");
return;
}
}
System.out.println("Book not found!");
}
private void borrowBook() {
System.out.print("Enter book ISBN: ");
String isbn = scanner.next();
System.out.print("Enter user ID: ");
int userId = scanner.nextInt();
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if (book.getIsbn().equals(isbn)) {
if (book.isBorrowed()) {
System.out.println("Book already borrowed!");
return;
} else {
book.setBorrowed(true);
book.setBorrowedBy(userId);
System.out.println("Book borrowed successfully!");
return;
}
}
}
System.out.println("Book not found!");
}
private void returnBook() {
System.out.print("Enter book ISBN: ");
String isbn = scanner.next();
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if (book.getIsbn().equals(isbn)) {
if (book.isBorrowed()) {
book.setBorrowed(false);
book.setBorrowedBy(-1);
System.out.println("Book returned successfully!");
return;
} else {
System.out.println("Book not borrowed!");
return;
}
}
}
System.out.println("Book not found!");
}
private void searchBook() {
System.out.print("Enter book title or author: ");
String keyword = scanner.next();
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
System.out.println(book);
}
}
}
private void registerUser() {
System.out.print("Enter user ID: ");
int id = scanner.nextInt();
System.out.print("Enter user name: ");
String name = scanner.next();
User user = new User(id, name);
users.add(user);
System.out.println("User registered successfully!");
}
private void removeUser() {
System.out.print("Enter user ID: ");
int id = scanner.nextInt();
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
if (user.getId() == id) {
users.remove(i);
System.out.println("User removed successfully!");
return;
}
}
System.out.println("User not found!");
}
}
这个程序实现了一个简单的在线图书馆系统,用户可以执行以下操作:
- 添加图书
- 删除图书
- 借阅图书
- 归还图书
- 查询图书
- 注册用户
- 删除用户
- 退出系统
这个程序使用了许多面向对象编程的概念,包括类、对象、继承、封装和多态。它还演示了Java中许多常用类和方法的使用,例如ArrayList、Scanner和switch语句。这个程序比之前的例子更加复杂,但它可以作为一个更高级的Java应用程序的基础。
标签:scanner,在线,Book,系统,System,图书馆,book,println,out From: https://blog.51cto.com/u_16096459/6411737