javaSE之图书管理系统

张开发
2026/4/12 1:09:45 15 分钟阅读

分享文章

javaSE之图书管理系统
思路一个图书管理系统项目的构建本次的代码是实现一个图书管理系统要求有登入系统和用户选择系统之后还有用户操作交换系统和图书管理系统具体思路如下创建以下类加粗部分为包testTestbookBook BookshlefopertionIOpeation (接口ExitOpeartion ShowOpeartion FindOpeartion DelOperation AddOpeartion BorrowOpeartionuserAdminUser NormalUser接下来我们先实现登陆和用户选择系统public class Test { public static User back(){ Scanner sc new Scanner(System.in); System.out.println(请输入你的名字); String username sc.nextLine(); System.out.println(请输入你的身份 1.普通用户 2.管理员); int result sc.nextInt(); if(result 1){ return new NormalUser(username); } else if(result 2){ return new AdminUser(username); } else return null; }public static void main(){ //这里用了多态中的向上转型从而调用了子类重写父类的方法用User数据类型接受User 方法创建user对象 User user back(); int choice user.meau(); }接下来我们来写用户包public class AdminUser exdents User{ Override public int menu() { Scanner sc new Scanner(System.in); System.out.println(欢迎登陆this.name.); System.out.println(*********************); System.out.println(1.查找图书); System.out.println(2.新增图书); System.out.println(3.减少图书); System.out.println(4,显示图书); System.out.println(0,推出系统); System.out.println(***********************); System.out.println(请输入你的操作。。。。。); int choice sc.nextInt(); return choice; } }public class NormalUser extends User{ Override public int menu() { Scanner sc new Scanner(System.in); System.out.println(欢迎登陆this.name.); System.out.println(*********************); System.out.println(1.查找图书); System.out.println(2.借阅图书); System.out.println(3.归还图书); System.out.println(0,推出系统); System.out.println(***********************); System.out.println(请输入你的操作。。。。。); int choice sc.nextInt(); return choice; } }public abstract class User{ protected String name; //这种对象数组用来存要选择的操作类对象 public IOPeration[] ioperation; public User(String name) { this.name name; } //抽象方法子类重写 abstract public int menu(); }Book包public class Book { private String title; private String author; private int price; private String type; private boolean isborrowed; public boolean isIsborrowed() { return isborrowed; } public void setIsborrowed(boolean isborrowed) { this.isborrowed isborrowed; } public Book(String title, String author, int price, String type) { this.title title; this.author author; this.price price; this.type type; } public String getTitle() { return title; } public void setTitle(String title) { this.title title; } 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; } Override //重写toString方法方便观察 public String toString() { return Book{ title title \ , author author \ , price price , type type \ //判断是否借出 ( (isborrowed true) ? 已借出 : 未借出) }; } }public class Bookshelf{ private Book[] book new Book[10]; private int usersize; //构造方法通过创建对象来赋初始值 public Bookshelf(){ this.book[0] new Book(三国杀,狗卡,6480,卡牌); this.book[1] new Book(原神,大伟哥,648,开放世界); this.book[2] new Book(三角洲行动,张姐,10,fps); this.book[3] new Book(鸣潮,松轮,10,开放世界); this.usersize4; } public int getUsersize() { return usersize; } public void setUsersize(int usersize) { this.usersize usersize; } //重写getbook方法和setbook方法其中pos为数组的下标 public Book getBook(int pos){ return book[pos]; } public Book[] getBook() { return book; } public void setBook(int pos, Book book) { this.book[pos] book; } }接着是操作方法和接口接口public interface IOPeration { void work(Bookshelf bookshelf); }每一个操作方法都要接上接口确保代码的一致性查找书籍public class FindOperation implements IOPeration{ Override public void work(Bookshelf bookshelf) { System.out.println(查找图书。。。); Scanner sc new Scanner(System.in); System.out.println(请输入你要找的书的书名字); String name sc.nextLine(); int size bookshelf.getUsersize(); //遍历数组 for(int i0;isize;i){ //调用getbook方法将值赋予book对象 Book book bookshelf.getBook()[i]; //使用Object类的equals方法比较名字 if(book.getTitle().equals(name)){ System.out.println(找到了这本书); System.out.println(book); return; } } System.out.println(没有你要找的这本书); } }退出系统public class ExitOperation implements IOPeration{ public void work(Bookshelf bookshelf){ System.out.println(退出系统); //0为编译器最终停止的标志 System.exit(0); } }借出书籍public class BorrowOperation implements IOPeration{ Override public void work(Bookshelf bookshelf){ System.out.println(借阅图书。。。); Scanner sc new Scanner(System.in); System.out.println(请输入你要找的书的书名字); String name sc.nextLine(); int size bookshelf.getUsersize(); for(int i0;isize;i){ Book book bookshelf.getBook()[i]; if(book.getTitle().equals(name)){ //调用了isIsborrowed方法但Isborrowed返回值是布尔类型,默认值为false if(book.isIsborrowed()){ System.out.println(这本书已经被借出了); return; } book.setIsborrowed(true); System.out.println(借阅成功); return; } } System.out.println(没有你要找的这本书); } }归还书籍public class ReturnOperation implements IOPeration{ Override public void work(Bookshelf bookshelf) { System.out.println(归还图书); Scanner sc new Scanner(System.in); System.out.println(请输入你要归还的图书的名字); String name sc.nextLine(); int size bookshelf.getUsersize(); for(int i0;isize;i){ Book book bookshelf.getBook()[i]; if(book.getTitle().equals(name)){ if(book.isIsborrowed()) { book.setIsborrowed(false); System.out.println(归还成功); return; } } } System.out.println(没有你要归还的图书); } }显示图书public class ShowOperation implements IOPeration{ Override public void work(Bookshelf bookshelf) { System.out.println(显示图书); int sizebookshelf.getUsersize(); for (int i0;isize;i) { Book bookbookshelf.getBook(i); System.out.println(book); } } }增加图书public class JoinOperation implements IOPeration{ public void work(Bookshelf bookshelf) { System.out.println(增加图书。。。); Scanner sc new Scanner(System.in); int size bookshelf.getUsersize(); //判断有没有插满 if (size bookshelf.getBook().length) { System.out.println(书架满了); return; } System.out.println(请输入标题); String name sc.nextLine(); System.out.println(请输入作者); String author sc.nextLine(); System.out.println(请输入类型); String type sc.nextLine(); System.out.println(请输入价格); int price sc.nextInt(); Book newbook new Book(name,author,price,type); //判断有没有这本书 int i0; for(;isize;i){ Book book bookshelf.getBook()[i]; if(book.getTitle().equals(name)){ System.out.println(有这本书); System.out.println(book); return; } } //插入这本书 bookshelf.setBook(i,newbook); bookshelf.setUsersize(bookshelf.getUsersize() 1); System.out.println(添加成功); } }减少图书public class DelectOperation implements IOPeration{ Override public void work(Bookshelf bookshelf) { System.out.println(减少图书); Scanner sc new Scanner(System.in); int size bookshelf.getUsersize(); System.out.println(请选择你要删除书的名字); String name sc.nextLine(); //找书的下标 int pos 999; int i0; for(;isize;i) { Book book bookshelf.getBook(i); if (book.getTitle().equals(name)) { pos i; break; } } if(isize){ System.out.println(没有找到你要删除的书); return; } //删除书的操作 for (int jpos;jsize;j) { Book book bookshelf.getBook(j); book bookshelf.getBook(j1); bookshelf.setBook(j,book); } bookshelf.setUsersize(bookshelf.getUsersize() - 1); System.out.println(删除成功); } }这里有些超纲说一下思路找到要删除的下标的图书用之后的一个下标的图书覆盖掉这个图书以此类推之后下一个下标再被下下个下标给覆盖注意jsize不然可能会造成数组越界异常接下来我们就要给用户类添加一个构造方法用来选择代码具体如下管理员类public class AdminUser extends User { public AdminUser(String name){ super(name); //引用父类对象数组创建对象利用下标来实现选择 this.ioperation new IOPeration[] { new ExitOperation(), new FindOperation(), new JoinOperation(), new DelectOperation(), new ShowOperation() }; } Override public int menu() { Scanner sc new Scanner(System.in); System.out.println(欢迎登陆this.name.); System.out.println(*********************); System.out.println(1.查找图书); System.out.println(2.新增图书); System.out.println(3.减少图书); System.out.println(4,显示图书); System.out.println(0,推出系统); System.out.println(***********************); System.out.println(请输入你的操作。。。。。); int choice sc.nextInt(); return choice; } }用户类public class NormalUser extends User { public NormalUser(String name) { super(name); //引用父类对象数组创建对象利用下标来实现选择 this.ioperation new IOPeration[] { new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation(), }; } Override public int menu() { Scanner sc new Scanner(System.in); System.out.println(欢迎登陆this.name.); System.out.println(*********************); System.out.println(1.查找图书); System.out.println(2.借阅图书); System.out.println(3.归还图书); System.out.println(0,推出系统); System.out.println(***********************); System.out.println(请输入你的操作。。。。。); int choice sc.nextInt(); return choice; } }User父类public abstract class User implements IOPeration{ protected String name; //这种对象数组用来存要选择的操作类对象 public IOPeration[] ioperation; public User(String name) { this.name name; } //抽象方法子类重写 abstract public int menu(); //重写接口方法 Override public void work(Bookshelf bookshelf) { } //重载接口方法引用操作类的work方法 public void work(int choice, Bookshelf bookshelf) { ioperation[choice].work(bookshelf); } }这下我们来写Test类public static void main() { Bookshelf bookshelf new Bookshelf(); //创建use对象 User user back(); while(true){ //choice来接受要选择的操作 int choice user.menu(); user.work(choice,bookshelf); } }这样写的话是不是代码架构特别清晰这样也方便维护和接入新的操作方法和其他如用户账户密码的系统有兴趣的话读者可以自己尝试增加新的系统如果要存储数据的话还可以使用mysql语言来编写数据库存储数据或者用io流存储到服务器当然这些都是后话完

更多文章