Java实现图书馆系统

Java实现图书馆系统该博客介绍了一个简单的图书馆管理系统 包括 Book 类 BookList 类 IOperation 接口和 User 类

大家好,欢迎来到IT知识分享网。

首先让我们看一下这个代码的功能!

Java实现图书馆系统

Java实现图书馆系统

我们可以清楚的发现这个代码 有两个角色 管理员 普通用户 而且他们所能 实现的能力不同!

Java实现图书馆系统

那我们实现分为几个part 书 方法 用户 整合(main)

 Java实现图书馆系统

public class Book { private String name; private String author; private int price; private String type; private boolean borrow; public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Boolean getBorrow() { return borrow; } public void setBorrow(boolean borrow) { this.borrow = borrow; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' +","+ ((borrow == true)?" 已经借出":" 未被借出") + '}'; } }
public class BookList { private Book[] books = new Book[10]; private int usedSize; public BookList(){ books[0] = new Book("微积分","小明",49,"学习"); books[1] = new Book("线性代数","小张",79,"学习"); books[2] = new Book("离散数学","小芳",66,"学习"); usedSize = 3; } //位置的书 public Book getBook(int pos){ return books[pos]; } //你要放的书 public void setBook(int pos,Book book){ books[pos] = book; } //多少书 public int getUsedSize() { return usedSize; } //修改书的个数 public void setUsedSize(int size) { usedSize = size; } } 
public interface IOperation { void work(BookList bookList); }
public class FindOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("查找图书!"); System.out.println("请输入你想查找的书名:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int current = bookList.getUsedSize(); for (int i = 0; i < current; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)){ System.out.println("找到这本书了!"); System.out.println(book); return; } } System.out.println("没有你要找的这本书"); } }
public abstract class User { protected String name; protected IOperation[] iOperations; public User(String name) { this.name = name; } abstract int menu(); public void doOperation(int choice, BookList bookList){ this.iOperations[choice].work(bookList); } } 
public class NormalUser extends User{ public NormalUser(String name) { super(name); this.iOperations = new IOperation[]{ new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } public int menu(){ System.out.println("hello "+this.name+" 欢迎来到自助图书馆~"); System.out.println("1.查找图书!"); System.out.println("2.借阅图书!"); System.out.println("3.归还图书!"); System.out.println("0.退出系统!"); System.out.println("请输入你的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
public class AdminUser extends User{ public AdminUser(String name) { super(name); this.iOperations = new IOperation[]{ new ExitOperation(), new FindOperation(), new AddOperation(), new DelectOperation(), new DisplayOperation() }; } public int menu(){ System.out.println("hello "+this.name+" 欢迎来到自助图书馆~"); System.out.println("1.查找图书!"); System.out.println("2.增加图书!"); System.out.println("3.删除图书!"); System.out.println("4.显示图书!"); System.out.println("0.退出系统!"); System.out.println("请输入你的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
public class Main { public static User login(){ System.out.println("请输入姓名:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); System.out.println("请输入你的身份:1-》管理员.0-》普通用户"); int choice = scanner.nextInt(); if(choice == 1){ return new AdminUser(name); }else{ return new NormalUser(name); } } public static void main(String[] args) { BookList bookList = new BookList(); User user = login(); //User user == new AdminUser(name);1 //User user == new NormalUser(name);0 while(true){ int choice = user.menu(); user.doOperation(choice,bookList); } } }

总的来说 应该都能看懂 但有一个part 我觉得可以说说

public static void main(String[] args) { BookList bookList = new BookList(); User user = login(); while(true){ //根据身份打印菜单选择方法 int choice = user.menu(); user.doOperation(choice,bookList); } }
public void doOperation(int choice, BookList bookList){ this.iOperations[choice].work(bookList); }

上图片!直观一点

Java实现图书馆系统

红红火火恍恍惚惚哈哈哈哈哈 520 我突然发现了图书馆的更有趣的方式

Java实现图书馆系统

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/107242.html

(0)
上一篇 2026-02-12 09:15
下一篇 2026-02-12 18:27

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信