简介
哈喽哈喽大家好啊,之前作者也是讲了Java不少的知识点了,为了巩固之前的知识点再为了让我们深入Java面向对象这一基本特性,就让我们完成一个图书管理系统的小项目吧。
项目简介:通过管理员和普通用户的两种操作界面,利用其中的方法以及对象之间的交互,来实现对图书的管理。
源码
book包
主要包含book对象和book List对象以及Main方法
Book类
描述书的有关信息,构造方法和各种getter,setter方法
public class Book { //书的属性:名字,作者,价格,类型,借出情况 private String name; private String author; private int price; private String type; private boolean borrowed; public Book(){ } public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; } /** * 获取 * @return name */ public String getName() { return name; } /** * 设置 * @param name */ public void setName(String name) { this.name = name; } /** * 获取 * @return author */ public String getAuthor() { return author; } /** * 设置 * @param author */ public void setAuthor(String author) { this.author = author; } /** * 获取 * @return price */ public int getPrice() { return price; } /** * 设置 * @param price */ public void setPrice(int price) { this.price = price; } /** * 获取 * @return type */ public String getType() { return type; } /** * 设置 * @param type */ public void setType(String type) { this.type = type; } /** * 获取 * @return borrowed */ public boolean isBorrowed() { return borrowed; } /** * 设置 * @param borrowed */ public void setBorrowed(boolean borrowed) { this.borrowed = borrowed; } public String toString() { return "Book{name = " + name + ", author = " + author + ", price = " + price + ", type = " + type + "," +/*", borrowed = " + borrowed*/ ((isBorrowed() == true) ? " 已被借出" : " 未被借出")+ "}"; } }
BookList
作为书架,利用数组存放书籍.
public class BookList { //定义数组成员表示存放书的数组 public Book[] books; //表示书架上存放书的数量 private int useSize; //设置最大容量 private static final int DEFAULT_CAPACITY = 10; public BookList() { this.books = new Book[DEFAULT_CAPACITY]; //先提前放好三本书 this.books[0] = new Book("三国演义", "罗贯中", 10, "小说"); this.books[1] = new Book("西游记", "吴承恩", 9, "小说"); this.books[2] = new Book("红楼梦", "曹雪芹", 19, "小说"); this.useSize = 3; } //书架上书的数量的getter和setter方法 public int getUseSize() { return useSize; } public void setUseSize(int useSize) { this.useSize = useSize; } //通过下标获取对应书籍的getter方法 public Book getBook(int pos) { return books[pos]; } //通过下标和传入的书对象设置对应书籍的setter方法 public void setBooks(int pos, Book book) { books[pos] = book; } public Book[] getBooks() { return books; } }
Main方法
主要的操作逻辑
import user.AdminUser; import user.NormalUser; import user.User; import java.util.Scanner; public class Main { //可以利用返回值的向上转型 达到发挥的一致性 public static User Login() { System.out.println("请输入你的姓名:"); Scanner sc = new Scanner(System.in); String name = sc.nextLine(); System.out.println("请输入你的身份:1.管理员 2.普通用户 ->"); int choice = sc.nextInt(); if (choice == 1) { //管理员 return new AdminUser(name); } else if (choice == 2) { //普通用户 return new NormalUser(name); } return null; } public static void main(String[] args) { BookList booklist = new BookList(); //user指向哪个对象 就看返回值 User user = Login(); while(true) { int choice = user.menu(); //System.out.println("choice:" + choice); //根据choice来决定调用哪个方法 user.doOperation(choice, booklist); } } }
user包
主要包含user以及相关对象
父类User
包含基本属性:姓名,menu(菜单)方法的声明,doOperation(执行方法操作)方法的声明。
import book.BookList; import operation.IOPeration; public abstract class User { protected String name; protected IOPeration[] ioperations; public User(String name) { this.name = name; } public abstract int menu(); public void doOperation(int choice, BookList booklist) { ioperations[choice].work(booklist);bao } }
子类AdminUser
包含管理员使用方法的数组和管理员菜单。
import operation.*; import java.util.Scanner; 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("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; } }
子类NormalUser
包含普通用户使用方法的数组和普通用户菜单。
package user; import operation.*; import java.util.Scanner; public class NormalUser extends User{ public NormalUser(String name) { super(name); this.ioperations = new IOPeration[]{new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperatoin(), }; } //普通用户用菜单 public int menu() { System.out.println("*************普通用户界面*************"); 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("************************************"); System.out.println("请输入你的操作:"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); return choice; } }
Operation包
主要包含各种操作方法
IOPeration接口
后续使用所有方法继承这个接口,对bookList对象进行相关工作。
package operation; import book.BookList; public interface IOPeration { void work(BookList booklist); }
AddOperation方法
添加图书的方法
package operation; import book.BookList; import book.Book; import java.util.Scanner; public class AddOperation implements IOPeration{ @Override public void work(BookList booklist) { System.out.println("增加书籍"); Scanner sc = new Scanner(System.in); int currentSize = booklist.getUseSize(); //用户输入书籍信息 System.out.println("请输入书籍的名称:"); String name =sc.nextLine(); System.out.println("请输入书籍的作者:"); String author = sc.nextLine(); System.out.println("请输入书籍的类型:"); String type = sc.nextLine(); //注意:这里将回车放到最后,如果放在前面,下一个nextLine()会读入回车 System.out.println("请输入书籍的价格:"); int price = sc.nextInt(); //创建一个新的book对象导入刚才的信息 Book book = new Book(name, author, price, type); //检查书架当中是否有这本书 for (int i = 0; i < currentSize; i++) { Book book1 = booklist.getBook(i); //判断书架中的书与新导入书的引用是否相等 if(book1 == book) { System.out.println("书架上有这本书,添加失败"); return; } } //判断添入书是否超出书架最大容量,未超出则添入书籍 if(currentSize == booklist.getBooks().length) { System.out.println("书架已满,添加失败"); } else { //在书架中添入书籍 booklist.setBooks(currentSize, book); //存放书的数量+1 booklist.setUseSize(currentSize + 1); System.out.println("添加成功"); } } }
FindOperation方法
寻找图书的方法
package operation; import book.Book; import book.BookList; import java.util.Scanner; public class FindOperation implements IOPeration{ @Override public void work(BookList booklist) { System.out.println("寻找书籍"); Scanner sc = new Scanner(System.in); System.out.println("请输入您要寻找的书籍"); String name = sc.nextLine(); int currentSize = booklist.getUseSize(); 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("没有找到这本书"); } }
DelOperation方法
删除图书所使用的方法
package operation; import book.Book; import book.BookList; import java.util.Scanner; public class DelOperation implements IOPeration { @Override public void work(BookList bookList) { System.out.println("删除书籍"); //1.找到有没有这本书 System.out.println("请输入要删除书的名字"); Scanner sc = new Scanner(System.in); String name = sc.nextLine(); int currentSize = bookList.getUseSize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBook(i); if (book.getName().equals(name)) { //2.如果有,从这本书后面开始,从前往后向前覆盖 for (; i < currentSize; i++) { Book book1 = bookList.getBook(i + 1); bookList.setBooks(i, book1); } //3.将最后一本书置为空 bookList.setBooks(currentSize - 1, null); //数组最大容量减少 bookList.setUseSize(currentSize - 1); System.out.println("删除成功!"); return; } } //未删除成功的情况 System.out.println("该书不存在,删除失败"); return; } }
ShowOperation方法
显示所有图书的方法
package operation; import book.Book; import book.BookList; import java.util.Scanner; public class DelOperation implements IOPeration { @Override public void work(BookList bookList) { System.out.println("删除书籍"); //1.找到有没有这本书 System.out.println("请输入要删除书的名字"); Scanner sc = new Scanner(System.in); String name = sc.nextLine(); int currentSize = bookList.getUseSize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBook(i); if (book.getName().equals(name)) { //2.如果有,从这本书后面开始,从前往后向前覆盖 for (; i < currentSize; i++) { Book book1 = bookList.getBook(i + 1); bookList.setBooks(i, book1); } //3.将最后一本书置为空 bookList.setBooks(currentSize - 1, null); //数组最大容量减少 bookList.setUseSize(currentSize - 1); System.out.println("删除成功!"); return; } } //未删除成功的情况 System.out.println("该书不存在,删除失败"); return; } }
BorrowOperation方法
借入图书的方法
package operation; import book.Book; import book.BookList; import java.util.Scanner; public class BorrowOperation implements IOPeration{ @Override public void work(BookList booklist) { System.out.println("借出书籍"); /* * 1.你要借阅什么书 * 2.你要借阅的书存不存在 * 3.如何完成借阅过程 isBorrowed->true 已借出 isBorrowed->false 未借出 * */ System.out.println("请输入你要借阅的图书名字"); Scanner sc = new Scanner(System.in); String name = sc.nextLine(); int currentSize = booklist.getUseSize(); for (int i = 0; i < currentSize; i++) { Book book = booklist.getBook(i); //该图书存在的情况 if(book.getName().equals(name) && book.isBorrowed() == false) { book.setBorrowed(true); System.out.println("借出成功"); System.out.println(book); return; } } //未被借出的情况 System.out.println("该图书已被借出或者不存在,借阅失败"); return; } }
ReturnOperation方法
归还图书的方法
package operation; import book.Book; import book.BookList; import java.util.Scanner; public class ReturnOperatoin implements IOPeration{ @Override public void work(BookList booklist) { System.out.println("归还书籍"); System.out.println("请输入你要归还的图书"); Scanner sc = new Scanner(System.in); String name = sc.nextLine(); int currentSize = booklist.getUseSize(); for (int i = 0; i < currentSize; i++) { Book book = booklist.getBook(i); //该图书存在的情况 if(book.getName().equals(name) && book.isBorrowed() == true) { book.setBorrowed(false); System.out.println("归还成功"); System.out.println(book); return; } } //未被借出的情况 System.out.println("该图书已被归还或者不存在,归还失败"); return; } }
ExitOperation方法
退出系统所使用的方法。
package operation; import book.BookList; public class ExitOperation implements IOPeration{ @Override public void work(BookList booklist) { System.out.println("退出系统"); //退出系统指令 System.exit(0); } }
操作示例
这里展示一下管理员界面的部分操作逻辑:
补充
缺点:没有做到持久化存储。以后可以进行升级:将数据存储到数据库或者文件夹中
当前用到的只是数组。后期可以做成网页交互。
好了,图书管理系统就说到这里,大家再见!