创作背景
介于很多人Javase学的比较快,很多知识还没来得及消化,需要练习来巩固和加深。在此背景下,一个具有一定难度,能够较为全面的练习我们学习知识的小项目便应运而生。
设计思路
1.book类
图书馆首先要有书,所以我们要设计书这个类,书嘛,要有书名,作者,价格,类型之类的,而且图书馆里的书还要有一个isBorrow的变量,用来确定书是否借出。通过构造方法,我们可以构造书。除此之外,我们还要有一个可以确定书本是否借出的变量isBorrow,设置为boolean类型,默认为false,当书本借出后,变为true。
package book; public class Book { private String name; private String author; private int price; private String type; private boolean isBorrowed; 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 isBorrowed() { return isBorrowed; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' +" "+ ((isBorrowed==true)?"已被借出":"未被借出")+ '}'; } }
2.booklist类
当书多后,我们就需要设计书架了,我们可以认为书架是书的数组,通过访问下表来增加或删除书,当然,我们也可以通过遍历下表的方法来查询书是否在书架中。废话不多说,请看代码。
package book; public class Booklist { private Book[] books = new Book[100]; private int cnt=0; public Booklist() { this.books[0] =new Book("三国演义","罗贯中",50,"小说"); this.books[1]=new Book("朝花夕拾","鲁迅",45,"杂文集"); this.books[2]=new Book("数据结构","安妮史蒂夫",40,"教科书"); this.cnt=3; } public Book getBooks(int pos) { return books[pos]; } public void addBook(Book b, int pos) { books[pos]=b; } public int getCount() { return cnt; } public void getSize(int size) { this.cnt=size; } } 我们在书架中初始了三本书,以便后续的功能检测。同时设计了可以通过下标查询书的getBook方法和添加书到指定位置的addBook方法。
3.针对不同用户设计的不同操作页面
针对图书管理员和普通用户,我们设计了不同的操作页面,具体页面如下。
管理员菜单
下面是实现的代码
package user; import operation.*; import java.util.Scanner; public class Admin extends User { public Admin(String name) { super(name); this.operations=new IOoperation[]{new Exit(),new Find(),new Add(),new Del(),new Show()}; } public int menu(){ System.out.println("管理员菜单:"); System.out.println("---------------------"); 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("---------------------"); System.out.println("请输入操作选项"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); return choice; } }
普通用户菜单
下面是具体代码实现
package user; import operation.*; import java.util.Scanner; public class Normal extends User { public Normal(String name) { super(name); this.operations=new IOoperation[]{new Exit(),new Find(),new Borrow(),new Return(),new Show()}; } public int menu(){ System.out.println("普通菜单"); System.out.println("---------------------"); 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("---------------------"); System.out.println("请输入操作选项"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); return choice; } }
4.功能实现
既然菜单都想好了,剩下的就是实现了,不过在实现之前,我们要定义一个接口IOopreation,用来规范各个功能。接口很简单,只需要设计好函数的返回类型(void),参数列表(Booklist)就行了。
package operation; import book.Booklist; public interface IOoperation { void work(Booklist book); }
1.新增图书
新增图书就是调用Book的构造方法构造一本书,然后再通过Booklist的addBook方法将构造好的书传入进去。最后书本本书加一就行了。
package operation; import book.Book; import book.Booklist; import java.util.Scanner; public class Add implements IOoperation { public void work(Booklist book) { System.out.println("新增图书!"); int cnt=book.getCount(); Scanner sc=new Scanner(System.in); System.out.println("名字"); String name=sc.nextLine(); System.out.println("作者"); String author=sc.nextLine(); System.out.println("价格"); int price=sc.nextInt(); sc.nextLine(); System.out.println("类型"); String type=sc.nextLine(); Book book1=new Book(name,author,price,type); book.getSize(cnt+1); book.addBook(book1,cnt); System.out.println("添加成功!"); } }
2.查找图书
开始时,我们说到可以认为Booklist就是Book的数组,那么我们可以类比数组遍历的方法,将书架遍历来查找我们所需的书,在设计Booklist时,我们就已经定义了记录书本的变量cnt,我们利用for循环就可以查询我们找的书是否在书架中。
package operation; import book.Book; import book.Booklist; import java.util.Scanner; public class Find implements IOoperation { public void work(Booklist book) { System.out.println("查找图书!"); Scanner sc = new Scanner(System.in); System.out.println("请输入你要查找的图书:"); String bookName = sc.nextLine(); int cnt= book.getCount(); for (int i = 0; i < cnt; i++) { Book book1 = book.getBooks(i); if(book1.getName().equals(bookName)){ System.out.println("找到了你查找的书--->"+bookName); return; } System.out.println("不存在该书--->"+bookName); break; } } }
3.删除图书
和查找图书类似,在通过for循环找到要删除的书后,通过变量index记录它的下标,然后再将这本书之后的书由前及后的覆盖到,在将cnt减一,从而实现删除操作。
package operation; import book.Book; import book.Booklist; import java.util.Scanner; public class Del implements IOoperation{ public void work(Booklist book) { System.out.println("删除图书!"); Scanner sc = new Scanner(System.in); System.out.println("请输入要删除书的书名"); String bookName = sc.nextLine(); int cnt= book.getCount(); int index=-1; for (int i = 0; i < cnt; i++) { Book book1 = book.getBooks(i); if(book1.getName().equals(bookName)){ index = i; } } if(index==-1){ System.out.println("不存在该书!"); } for (int i = index; i < cnt-1; i++) { Book book2=book.getBooks(i+1); book.addBook(book2,i); } book.getSize(cnt-1); System.out.println("删除成功!"); } }
4.显示图书
显示图书就是通过for循环读取下标,再通过getBook方法将书的信息打印出来。本身难度不太大。
package operation; import book.Book; import book.Booklist; public class Show implements IOoperation { public void work(Booklist book) { System.out.println("显示图书!"); int cnt= book.getCount(); for (int i = 0; i < cnt; i++) { Book book1 = book.getBooks(i); System.out.println(book1); } } }
5.借阅图书
借阅图书也是基于查找图书的基础上设计的,在找到要借阅的图书后,将其isBorrow设置为true即可。不过需要注意的是在借阅图书前,我们需要检查isBorrow的值,如果已经是true了,那就已经借出,无法再次借出了。
package operation; import book.Book; import book.Booklist; import java.util.Scanner; public class Borrow implements IOoperation { public void work(Booklist book){ System.out.println("借阅图书!"); Scanner sc = new Scanner(System.in); System.out.println("请输入借阅图书的书名"); String bookName = sc.nextLine(); int cnt = book.getCount(); for (int i = 0; i < cnt; i++) { Book book1 = book.getBooks(i); if(book1.getName().equals(bookName)){ if(book1.isBorrowed()==true) { System.out.println("该书已借出,无法再次借出!"); break; } System.out.println("已借出"+bookName); book1.setBorrowed(true); return; } } } }
6.归还系统
和借阅图书类似,在查找到书后,将isBorrow设置为false即可,但也需要在归还前检查一下是否已经归还。
package operation; import book.Book; import book.Booklist; import java.util.Scanner; public class Return implements IOoperation { public void work(Booklist book){ System.out.println("归还图书!"); Scanner sc = new Scanner(System.in); System.out.println("请输入归还图书的书名"); String bookName = sc.nextLine(); int cnt = book.getCount(); for (int i = 0; i < cnt; i++) { Book book1 = book.getBooks(i); if(book1.getName().equals(bookName)){ if(book1.isBorrowed()==false) { System.out.println("该书已归还,无法再次归还"); return; } System.out.println("已归还"+bookName); book1.setBorrowed(false); return; } } } }
7.退出系统
这个先需要将书架里的书都设置为null,然后退出即可。
package operation; import book.Book; import book.Booklist; public class Exit implements IOoperation { public void work(Booklist book) { System.out.println("退出系统!"); int cnt= book.getCount(); for (int i = 0; i < cnt; i++) { book.addBook(null,i); } book.getSize(0); System.exit(0); } }
5.Main函数
Main函数需要提供一个用户交互界面,而且还要对用户的错误输入进行处理,难度不大,但细节较多。
import book.Booklist; import user.Admin; import user.Normal; import user.User; import java.util.Scanner; public class Main { public static User login(){ Scanner in = new Scanner(System.in); System.out.println("请输入你的姓名:"); String name = in.nextLine(); System.out.println("请输入你的身份:--->1.管理员 0--->普通用户"); int choice = in.nextInt(); if(choice == 1) { Admin admin = new Admin(name); return admin; }else{ Normal normal = new Normal(name); return normal; } } public static void main(String[] args) { Booklist booklist = new Booklist(); User user = login(); while(true){ int ret = user.menu(); if(ret>4||ret<0){ System.out.println("选择错误!"); break; } user.dooperation(ret,booklist); } } }
下面是我的gitee仓库网址