import java.util.Scanner;
public class LibraryClient {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
RealLibraryService realService = new RealLibraryService();
BorrowProxy borrowProxy = new BorrowProxy(realService);
ReturnProxy returnProxy = new ReturnProxy(realService);
LibraryTransactionCenter center = new LibraryTransactionCenter(borrowProxy, returnProxy);
System.out.print("请输入用户ID:");
String userId = scanner.nextLine();
System.out.print("请输入图书ID:");
String bookId = scanner.nextLine();
System.out.println("\n--- 开始借书流程 ---");
center.processBorrow(userId, bookId);
System.out.println("\n--- 开始还书流程 ---");
center.processReturn(userId, bookId);
scanner.close();
}
}
interface Borrowable {
void borrow(String userId, String bookId);
}
interface Returnable {
void returnItem(String userId, String bookId);
}
class RealLibraryService implements Borrowable, Returnable {
@Override
public void borrow(String userId, String bookId) {
System.out.println("→ 正在处理图书借阅:用户 " + userId + " 借书 " + bookId);
}
@Override
public void returnItem(String userId, String bookId) {
System.out.println("→ 正在处理图书归还:用户 " + userId + " 归还书籍 " + bookId);
}
}
class BorrowProxy implements Borrowable {
private RealLibraryService realService;
public BorrowProxy(RealLibraryService service) {
this.realService = service;
}
@Override
public void borrow(String userId, String bookId) {
System.out.println(" 查找用户:" + userId + " 和图书:" + bookId);
realService.borrow(userId, bookId);
System.out.println(" 借书成功!记录已生成,短信通知已发送。");
}
}
class ReturnProxy implements Returnable {
private RealLibraryService realService;
public ReturnProxy(RealLibraryService service) {
this.realService = service;
}
@Override
public void returnItem(String userId, String bookId) {
System.out.println(" 验证归还信息:用户 " + userId + " 归还书籍 " + bookId);
realService.returnItem(userId, bookId);
System.out.println(" 归还完成!系统状态已更新,感谢您的配合。");
}
}
class LibraryTransactionCenter {
private Borrowable borrowService;
private Returnable returnService;
public LibraryTransactionCenter(Borrowable borrowService, Returnable returnService) {
this.borrowService = borrowService;
this.returnService = returnService;
}
public void processBorrow(String userId, String bookId) {
borrowService.borrow(userId, bookId);
}
public void processReturn(String userId, String bookId) {
returnService.returnItem(userId, bookId);
}
}