目录
😽个人主页: tq02的博客_CSDN博客-C语言,Java领域博主
🌈梦中理想:努力学习,向Java进发,拼搏一切,找到一份朝九晚五,有假期的工作,让 自己的未来不会有遗憾。
🎁欢迎各位→点赞👍 + 收藏⭐ + 评论📝+关注✨本章讲解内容:图书馆管理系统简略版
使用编译器:IDEA
注:本文有些长,请耐心观看,看完之后,不懂打我。
为图书馆管理人员实现一个图书管理系统,主要设计:实现对图书的管理、以及其它相关操作。

图书馆管理系统大概思路如上图,书籍放在书架当中,登陆系统有2个权限(管理员和普通用户)。
为了让代码有更高的可读性,我们可以将代码分为三个包,分别存储用户登录、书架书籍、用户操作。
用户登陆:判断用户为管理员还是普通用户。
用户操作:对书架的书籍进行操作。
书架书籍:在用户操作之后,记录此时书架的书籍状态。
如此图:

书架书籍,顾名思义,记录书架上的书和书本的信息。使用需要建立两个类,书架类和存放书本信息类。 也可以说,书架类,存放的是书本信息的数组。而数组大小取决于书的多少
书籍信息代码实现:
public class Book {
private String name; //书名
private String author; //作者
private int price; //价格
private String type; //类型
private boolean isBorrow; //是否借出
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "书名:"+name+" 价格:"+price+" 作者:"+author+" 类型:"+type+" 状态:"+
((this.isBorrow==true)?"已借出":"未借出");
}
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 isBorrow() {
return isBorrow;
}
public void setBorrow(boolean borrow) {
isBorrow = borrow;
}
}
书架信息实现:
public class BookList {
Book[] books=new Book[10]; //书架存放量;
public int size; //已存放量
public BookList()
{
books[0]=new Book("西游记","吴承恩",22,"小说");
books[1]=new Book("三国演义","罗贯中",20,"小说");
books[2]=new Book("水浒传","施耐庵",25,"小说");
books[3]=new Book("红楼梦","曹雪芹",28,"小说");
this.size=4;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Book getBook(int pos) {
return books[pos];
}
public void setBook(int pos,Book book) {
books[pos] = book;
}
}
用户操作,操作的是书架上的书籍,对其进行增删查改等操作。而这一切都同一个方法名和参数。
所以我们可以写一个接口,使所有的操作都继承于它,而这个接口的创建有利于用户登录后的选择操作。
接口:
public interface IOPeration {
void work(BookList bookList);
}
增加图书,得增加图书的各类信息
public class AddOperation implements IOPeration{
//将书架地址传入,对其修改
public void work(BookList bookList) {
System.out.println("新增图书!");//业务逻辑!!!
Scanner scanner = new Scanner(System.in);
System.out.println("请输入图书的名字:");
String name = scanner.nextLine();
System.out.println("请输入图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入图书的类型:");
String type = scanner.nextLine();
System.out.println("请输入图书的价格:");
int price = scanner.nextInt();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
System.out.println("新增图书成功!!");
}
}
当移除图书时,得先判断是否存在此书籍,然后删除,删除方式:从需要删除的书籍之后开始,将后面的每一个书籍信息覆盖在前面一个,并且将数组最后一个置空,书本量减一。
public class DelOperation implements IOPeration{
public void work(BookList bookList) {
System.out.println("删除图书!");
//1、找到你要删除的图书是否存在?
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要删除的图书名字:");
String name = scanner.nextLine();//水浒传
int currentSize = bookList.getUsedSize();
int delIndex = -1;
int i = 0;
for (; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
delIndex = i;
break;
}
}
if(i == currentSize) {
System.out.println("没有你删除的这本书!");
return;
}
for (int j = delIndex; j < currentSize-1; j++) {
//[j] = [j+1]
Book book = bookList.getBook(j+1);
bookList.setBooks(j,book);
}
bookList.setBooks(currentSize-1,null);
bookList.setUsedSize(currentSize-1);
System.out.println("删除图书成功!");
}
}
在书架上查找书籍,自然而然从第一步开始查找。当然也存在此书籍不存在的情况。
public class FindOperation implements IOPeration{
public void work(BookList bookList) {
System.out.println("查找图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要查找的图书姓名:");
String name = scanner.nextLine();//水浒传
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("找到这本书了!");
System.out.println(book);
return;
}
}
System.out.println("没有你要查找的这本书!");
}
}
public class ShowOperation implements IOPeration{
public void work(BookList bookList) {
System.out.println("打印所有图书!");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
当书籍借阅之后,需要进行备注,已借出。
public class BorrowOperation implements IOPeration{
public void work(BookList bookList) {
System.out.println("借阅图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要借阅的图书的名字:");
String name = scanner.nextLine();//水浒传
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
if(book.isBorrowed()) {
System.out.println("该书已经被借出!");
}else{
book.setBorrowed(true);
}
return;
}
}
System.out.println("没有你要借阅的图书!");
}
}
public class ReturnOperation implements IOPeration{
public void work(BookList bookList) {
System.out.println("归还图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要归还的图书的名字:");
String name = scanner.nextLine();//水浒传
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
book.setBorrowed(false);
return;
}
}
System.out.println("没有你要归还的图书!");
}
}
当操作完毕之后,需要退出系统。
public class ExitOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
用户,分为管理员和普通用户,不同的用户身份,拥有不同的权限,因此我们需要两个菜单来供不同的用户选择操作。又因为管理员和普通用户又有许多相同的内容,为了减少代码的冗余性,以及之后的主函数多态调用,所以可以进行继承的方式。
用户代码:
public abstract class User { protected String name; public IOPeration[] ioPerations;//这里我没有分配空间 public User(String name) { this.name = name; } public abstract int menu(); public void doOperation(int choice, BookList bookList){ this.ioPerations[choice].work(bookList); } }我们可以看见在用户操作时,创建的接口的作用:利用接口,创建数组存储着不同的用户的操作。而doOperation()方法,则是调用对应的用户操作方法
普通用户代码:
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("普通用户的菜单!"); System.out.println("************************************************"); System.out.println(" hello " + this.name +" 欢迎来到图书馆管理系统"); System.out.println("***********1. 查找图书 2. 借阅图书***********"); System.out.println("***********3. 归还图书 0. 退出系统!**********"); System.out.println("************************************************"); 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 DelOperation(), new ShowOperation() }; } public int menu() { System.out.println("管理员菜单!"); System.out.println("************************************************"); System.out.println(" hello " + this.name +" 欢迎来到图书馆管理系统"); System.out.println("***********1. 查找图书 2.新增图书 ***********"); System.out.println("***********3. 删除图书 0. 退出系统!**********"); System.out.println("************************************************"); System.out.println("请输入你的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
主函数的调用,重点在于 区分普通用户和管理员用户、创建书架书籍、调用用户操作等。
代码实例:
public class Main {
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名:");
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 user = login();
while (true) {
//这里调用谁的menu菜单???
int choice = user.menu();
//根据这个choice 来调用指定的 操作??
user.doOperation(choice, bookList);
}
}
}
1. 本文章是最为基础的图书馆管理系统,但是即使再简单,也得学会封装、继承、抽象、多态、接口等知识,因为此系统建立在这些知识的基础上。
如果想了解或者回忆这些知识点,可查询 http://t.csdn.cn/Y3UsQ
2. 如果你可以看懂,可以试着尝试,并且自己成功写出来之后,恭喜你Java的基础语法阶段你已经掌握的差不多了 ,可以开始你的新篇章了。
我正在使用i18n从头开始构建一个多语言网络应用程序,虽然我自己可以处理一大堆yml文件,但我说的语言(非常)有限,最终我想寻求外部帮助帮助。我想知道这里是否有人在使用UI插件/gem(与django上的django-rosetta不同)来处理多个翻译器,其中一些翻译器不愿意或无法处理存储库中的100多个文件,处理语言数据。谢谢&问候,安德拉斯(如果您已经在rubyonrails-talk上遇到了这个问题,我们深表歉意) 最佳答案 有一个rails3branchofthetolkgem在github上。您可以通过在Gemfi
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s
电脑0x0000001A蓝屏错误怎么U盘重装系统教学分享。有用户电脑开机之后遇到了系统蓝屏的情况。系统蓝屏问题很多时候都是系统bug,只有通过重装系统来进行解决。那么蓝屏问题如何通过U盘重装新系统来解决呢?来看看以下的详细操作方法教学吧。 准备工作: 1、U盘一个(尽量使用8G以上的U盘)。 2、一台正常联网可使用的电脑。 3、ghost或ISO系统镜像文件(Win10系统下载_Win10专业版_windows10正式版下载-系统之家)。 4、在本页面下载U盘启动盘制作工具:系统之家U盘启动工具。 U盘启动盘制作步骤: 注意:制作期间,U盘会被格式化,因此U盘中的重要文件请注