赞
踩
图书管理系统是java知识的一个小总结,运用了类和对象,构造方法,方法调用,数组,接口,重载,接口,抽象类等知识。是一个检验知识的好机会
完成代码:test-C: CCCCCCCCCCCCCCCCCCCCC
写一个图书系统,我们需要先需求分析,然后结构分析,最后再从分析的结果一步步写代码,这些是循环渐进的,不会是直接随手写出来一个系统。
通过分析我们创造相关的包和类
在Book类中,我们需要创建书的属性,比如说:书名、作者名、书的价格、书的类型、书的状态(是否借出)
再通过构造方法,获取get和set方法,然后重写toString方法
在这里我们要注意在显示功能中用到的toString方法,我们可以将书的状态做一个判断如下代码,
图1是正常的toString,会有些不好看。图2就是修改之后的方法
1.
2.
在BookList类中,我们需要将图书的属性存放在一个数组中。
- public class BookList {
- private Book[] books = new Book[10];
- private int usedSize;//记录当前books数组中 有多少本书
- public Book getBook(int pos){
- return this.books[pos];//数组图书下标
- }
- public void setBook(Book book){
- this.books[usedSize] = book;
- }
- public void setBook(int pos,Book book){
- this.books[pos] = book;
- }
- public int getUsedSize() {
- return usedSize;
- }
-
- public void setUsedSize(int usedSize) {
- this.usedSize = usedSize;
- }
-
- public BookList(){
- books[0] = new Book("西游记","吴承恩",45,"小说");
- books[1] = new Book("三国演义","罗贯中",45,"小说");
- books[2] = new Book("红楼梦","曹雪芹",45,"小说");
- this.usedSize = 3;
- }
-
-
- }

需要注意的是在身份输出后,增加了一个判断用户的操作,以显示不同的操作菜单
并添加一个while循环,以便可以使用多个操作。
- public class Main {
- public static User login(){
- System.out.println("请输入你的姓名");
- Scanner sc = new Scanner(System.in);
- String name = sc.nextLine();
- System.out.println("请输入你的身份 1->管理员 0->普通用户");
- int id = sc.nextInt();
- if (id == 1){
- return new AdminUser(name);
- }else{
- return new NormalUser(name);
- }
- }
- public static void main(String[] args) {
- User user = login();
- BookList bookList = new BookList();
- while(true) {
- int choose = user.menu();
- user.doWork(choose, bookList);
- }
- }
- }

创建抽象类User,抽象方法menu菜单和IOperation数组的初始化(在AdminUser类和NormalUser类中会重要体现到)
因为我们的管理员和普通用户的操作菜单不同,所以可以通过重写menu方法来显示不同的菜单
- public abstract class User {
- protected String name;
-
- protected IOperation[] iOperations;
-
- public User(String name){
-
- this.name = name;
- }
- public abstract int menu();
-
- public void doWork(int choose, BookList bookList){
- this.iOperations[choose].work(bookList);
- }
- }
通过继承User类,调用到IOperation功能接口以调用管理员所需的功能
- 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 " + 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 choose = sc.nextInt();
- return choose;
- }
- }

通过继承User类,调用到IOperation功能接口以调用普通用户所需的功能
- 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("Hello " + 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 choose = sc.nextInt();
- return choose;
- }
- }

这里实现一个功能的接口,方便之后的各种操作类执行该接口,利用多态进行相关操作
- public interface IOperation {
- public void work(BookList bookList);
- }
创建currentSize接受BookList的UsedSize(书的数量)
通过for循环遍历数组得到所有的信息
- 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);
- }
- }
-
-
- }
通过for循环遍历数组得到所有的信息,实例化对象book,if判断查找输入的书名是否在BookList中,如果不存在就输出没有该图书,反之,查询成功并输出该图书的信息(注意equals方法)
- public class FindOperation implements IOperation{
- public void work(BookList bookList){
- System.out.println("查询图书");
- System.out.println("请输入您要查找图书的名字");
- Scanner sc = new Scanner(System.in);
- String name = sc.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("没有该图书");
- }
-
- }

如果要新增一个图书,就要将所有图书的属性输入,并利用if equals进行重名检测
并通过实例化对象bookList中set添加到BookList数组中,将书的数量+1
- public class AddOperation implements IOperation{
- public void work(BookList bookList){
- System.out.println("新增图书");
- 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();
- System.out.println("请输入类型");
- String type = sc.nextLine();
-
- Book book = new Book(name,author,price,type);
- int currentSize = bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book b = bookList.getBook(i);
- if (b.getName().equals(name)){
- System.out.println("该书已存在");
- return;
- }
- }
- bookList.setBook(book);
- bookList.setUsedSize(currentSize + 1);
- }
- }

设置index表明需要删除图书的位置,进行if判断图书存在,如果存在,就将数组中i的下标传给index,再进行for对currentSize(现有书的数量)进行遍历,将index下标删除后,将之后的下标前移,并将currentSize-1
- public class DelOperation implements IOperation{
- public void work(BookList bookList){
- System.out.println("删除图书");
- System.out.println("请输入删除图书的书名:");
- Scanner sc = new Scanner(System.in);
- String name = sc.nextLine();
- int currentSize = bookList.getUsedSize();
- int index = -1;
- for (int i = 0; i < currentSize; i++) {
- Book book = bookList.getBook(i);
- if (book.getName().equals(name)){
- index = i;
- break;
- }
- }
- for (int j = index; j < currentSize - 1; j++) {
- Book book = bookList.getBook(j+1);
- bookList.setBook(j,book);
- }
-
- bookList.setUsedSize(currentSize - 1);
- bookList.setBook(currentSize-1,null);
- System.out.println("删除成功");
- }
- }

在借阅功能中,我们不仅要查询图书是否存在,也要查询图书的状态(是否借出)
- public class BorrowOperation 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.getUsedSize();
- 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("借阅成功");
- return;
- }
- }
- }
- }

在归还功能中,我们不仅要查询图书是否存在,也要查询图书的状态(是否借出)
- public class ReturnOperation 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.getUsedSize();
- 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("归还成功");
- return;
- }
- }
- System.out.println("没有该图书");
- }
- }

在通常的编译运行后一般会有返回0,所以调用System的exit方法,传入参数0进行退出运行
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- System.exit(0);
- System.out.println("已成功退出系统");
- }
- }
在实现图书管理系统的同时,我们发现继承,抽象,接口,多态的重要性,以及重载,重写的方便性,每次动手写这样的一个系统的时候,都有不同的感悟和发现,这会巩固之前知识。
最后感谢各位码友的浏览,希望对每一个人都有所帮助!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。