当前位置: 首页 > news >正文

javaSE之图书管理系统

思路:一个图书管理系统项目的构建

本次的代码是实现一个图书管理系统要求,有登入系统和用户选择系统,之后还有用户操作交换系统,和图书管理系统,具体思路如下

创建以下类,加粗部分为包

test:Test

book:Book Bookshlef

opertion:IOpeation (接口)ExitOpeartion ShowOpeartion FindOpeartion DelOperation AddOpeartion BorrowOpeartion

user:AdminUser 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.usersize=4; } 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 i=0;i<size;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 i=0;i<size;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 i=0;i<size;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 size=bookshelf.getUsersize(); for (int i=0;i<size;i++) { Book book=bookshelf.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 i=0; for(;i<size;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 i=0; for(;i<size;i++) { Book book = bookshelf.getBook(i); if (book.getTitle().equals(name)) { pos = i; break; } } if(i==size){ System.out.println("没有找到你要删除的书"); return; } //删除书的操作 for (int j=pos;j<size;j++) { Book book = bookshelf.getBook(j); book = bookshelf.getBook(j+1); bookshelf.setBook(j,book); } bookshelf.setUsersize(bookshelf.getUsersize() - 1); System.out.println("删除成功"); } }

这里有些超纲说一下思路:找到要删除的下标的图书,用之后的一个下标的图书覆盖掉这个图书,以此类推,之后下一个下标再被下下个下标给覆盖,注意j<size,不然可能会造成

数组越界异常

接下来我们就要给用户类添加一个构造方法用来选择代码具体如下

管理员类

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流存储到服务器,当然这些都是后话

http://www.jsqmd.com/news/626304/

相关文章:

  • 【2026奇点大会AI语音交互终极指南】:3大原生架构、5类落地陷阱与2026Q2商用部署清单
  • 嵌入式上位机开发入门(十八):修复首次连接超时问题
  • Triton + RISC-V毓
  • Spring IOC 源码学习 声明式事务的入口点冻
  • ESP32/ESP8266工业级WiFi配置门户库
  • 什么年代了怎么还在用bash啊?现代化shell开箱体验: fish, nu, elvish桨
  • 深度解析Agent技术演进路径与未来趋势
  • IOFILE结构体的介绍与House of orange欠
  • MediaCreationTool.bat 深度解析:Windows 11硬件限制突破的技术原理与实战指南
  • SALSA Series Report
  • BMD26M088 RGB点阵模块I²C驱动与寄存器级开发指南
  • 2026年临江鳝丝必吃品牌筛选:正宗乐山临江鳝丝推荐/老字号临江鳝丝店/老牌临江鳝丝店/临江哪家鳝丝最正宗/选择指南 - 优质品牌商家
  • 代码随想录一刷记录Day25——leetcode491.递增子序列
  • 美国能源部(DOE)发布“关键矿产与材料加速器”资助机会
  • Docker化多服务共存:Nginx 443 SNI 实现多 HTTPS 站点与加密通信无缝部署
  • 初步学习c语言指针的一些简单理解
  • 告别调参玄学:手把手教你用TransNeXt-Tiny在ImageNet上复现84.0%的准确率
  • atomic原子操作实现无锁队列
  • 2026年OpenClaw怎么搭建?阿里云6分钟新手部署OpenClaw,千问大模型安装指南
  • NGLedFlasher:嵌入式多LED非阻塞时序控制库
  • 材料冶金是“天坑”?就业超99%,深造超70%,北京科技大学王牌专业正被新能源巨头疯抢!
  • 2026单位复印机租赁服务商盘点:品牌打印机租赁/学校复印机租赁/学校打印机租赁/彩色复印机租赁/选择指南 - 优质品牌商家
  • TA8428双通道H桥驱动芯片硬件设计与mbed底层驱动实现
  • ComfyUI面部修复FaceDetailer参数调优实战
  • Android显示机制深度解析:Surface、SurfaceFlinger与Choreographer如何协同工作
  • MES / WMS / AGV 交互时序图及生产管理模块界面设计清单
  • 当大模型开始控制设备:我是怎么理解 Agent 架构的勤
  • Arduino嵌入式信号处理库:轻量级LPF/HPF/积分/微分滤波器
  • AI代理受限于人类设计的工具边界和孤立体
  • 揭秘MySQL索引分类档