Java图书管理系统

文章目录

  • 图书管理系统
  • 对象
      • 代码
    • 书库
      • 代码
    • 用户
      • 抽象类代码
      • 管理者代码
      • 普通用户代码
    • 操作接口
      • 接口
      • AddOp
      • BorrowOp
      • DelOp
      • DisplayOp
      • ExitOp
      • ReturnOp
    • Main
  • 结果

图书管理系统

Java的多态和接口 面向对象的思想很强烈

对象

Java图书管理系统_第1张图片

代码

package book;


/**
 * @author myy
 */
public class Book {
    private String _name;//书名
    private String _author;
    private String _type;
    private double _price;

    public Book(String name, String author, String type, double price) {
        this._name = name;
        this._author = author;
        this._type = type;
        this._price = price;

    }

    public String get_name() {
        return _name;
    }

    public void set_name(String _name) {
        this._name = _name;
    }

    public String get_author() {
        return _author;
    }

    public void set_author(String _author) {
        this._author = _author;
    }

    public String get_type() {
        return _type;
    }

    public void set_type(String _type) {
        this._type = _type;
    }

    public double get_price() {
        return _price;
    }

    public void set_price(double _price) {
        this._price = _price;
    }

    @Override
    public String toString() {
        return
                "{ 书名='" + _name + '\'' +
                        ", 作者='" + _author + '\'' +
                        ", 类型='" + _type + '\'' +
                        ", 价格=" + _price + ','
                ;
    }


}

书库

Java图书管理系统_第2张图片

代码

package book;
import operator.IOperation;
import java.util.*;
import  javafx.util.*;
import  javafx.*;

import javax.swing.text.html.HTMLDocument;

/**
 * @author myy
 */ //书架
public class BookList  {
    //ArrayList al = new ArrayList();
    //单纯的数组是不行的,这里使用K-V的map
    //int 用于记录存量
    //这里使用内部类,但是不好看,可以自己封装一个kv键值对
    protected  Map<String, AbstractMap.SimpleEntry<Book,Integer>> _map= new HashMap<String, AbstractMap.SimpleEntry<Book,Integer> >();
    private int sz;//记录有效数据个数
    public BookList()//默认构造函数
    {
        _map.put("三国演义", new AbstractMap.SimpleEntry<>(new Book("三国演义", "罗贯中", "历史演义小说", 52.5), 5));
        _map.put("水浒传", new AbstractMap.SimpleEntry<>(new Book("水浒传", "施耐庵", "章回体长篇小说", 45.5), 5));
        _map.put("西游记", new AbstractMap.SimpleEntry<>(new Book("西游记", "吴承恩", "浪漫主义章回体长篇神魔小说", 56.5),6 ));
        _map.put("红楼梦",new AbstractMap.SimpleEntry<>(new Book("红楼梦", "曹雪芹", "章回体类长篇小说", 78.5), 7));
        _map.put("无职转生", new AbstractMap.SimpleEntry<>(new Book("无职转生", "理不尽な孫の手", "轻小说", 90.5), 10));
        sz = _map.size();
    }
   public  void Print()
   {
       for (Map.Entry<String, AbstractMap.SimpleEntry<Book, Integer>> entry : _map.entrySet()) {
           System.out.printf("%s",entry.getValue().getKey());
           System.out.printf("存量= "+ entry.getValue().getValue()+'}'+'\n');
       }
   }
    public Map<String, AbstractMap.SimpleEntry<Book,Integer>> Get_map()
    {
        return  _map;
    }
}


用户

Java图书管理系统_第3张图片

抽象类代码

package user;

import book.BookList;
import operator.IOperation;

/**
 * @author myy
 */
public abstract  class User {
    protected String _name;
    protected IOperation[] ioperations;//利用一个静态数组存放行为
    public User(String name)
    {
        _name=name;

    }
    public abstract int  menu();//菜单
    public  boolean DoOperation(int choice, BookList bookList)
    {

       return  ioperations[choice].work(bookList);
    }

}

管理者代码

package user;

import operator.*;

import java.util.Scanner;

/**
 * @author myy
 */

public class AdminUser extends User {
    public AdminUser(String name) {
        super(name);
        ioperations = new IOperation[]{
                new ExitOp(),//0
                new AddOp(),//1
                new DelOp(),//2
                new FindOp(),//3
                new DisplayOp(),//4
        };
    }


    //菜单
    @Override
    public int menu() {
        System.out.println("********图书管理系统**************");
        System.out.println("0.退出       ");
        System.out.println("1. 添加图书  2.删除图书");
        System.out.println("3. 查找图书  4.显示图书");
        System.out.println("*****************************");
        System.out.println("请输入你的操作:");

        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

普通用户代码

package user;

import book.BookList;
import operator.*;

import java.sql.SQLClientInfoException;
import java.util.Scanner;

/**
 * @author myy
 */
public class NormalUser extends User {

    public NormalUser(String name) {
        super(name);
        super.ioperations = new IOperation[]{
                new ExitOp(),//0
                new BorrowOp(),//1
                new ReturnOp(),//2
                new DisplayOp()//3
        };
    }

    //
    public int menu() {
        System.out.println("********图书管理系统************");
        System.out.println("0退出      ");
        System.out.println("1.借阅图书   2.归还图书");
        System.out.println("3.显示图书           ");
        System.out.println("*****************************");
        System.out.println("请输入你的操作:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

操作接口

Java图书管理系统_第4张图片

接口

package operator;

import book.Book;
import book.BookList;

/**
 * @author myy
 */
public interface IOperation {

    boolean work(BookList bookList);
}

AddOp

package operator;

import book.Book;
import book.BookList;

import java.util.AbstractMap;
import java.util.Scanner;

/**
 * @author myy
 */
public class AddOp implements IOperation {

    //   private String _name;//书名
//   private String _author;
//   private  String _type;
//   private  double _price;
//   private boolean _isBorrow;//是否借出,默认是false
    public boolean  work(BookList bookList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入图书名:");
        String name = sc.nextLine();
        System.out.println("请输入作者:");
        String author = sc.nextLine();
        System.out.println("请输入作品类型:");
        String type = sc.nextLine();
        System.out.println("请输入作品价格:");
        double price = sc.nextDouble();
        System.out.println("请输入书本数量:");
        int cnt=sc.nextInt();
        if (bookList.Get_map().containsKey(name)) {
            bookList.Get_map().get(name).setValue(bookList.Get_map().get(name).getValue() + cnt);

        } else {
            bookList.Get_map().put(name,new AbstractMap.SimpleEntry<>(new Book(name,author,type,price),cnt));
        }
        return  true;

    }
}

BorrowOp

package operator;

import book.BookList;

import java.util.Scanner;

/**
 * @author myy
 */
public class BorrowOp implements IOperation {
    public boolean  work(BookList bookList) {
        DisplayOp  displayOp =new DisplayOp();
        displayOp.work(bookList);
        System.out.println("请输入你要借的书名:");
        Scanner sc=new Scanner(System.in);
        String name=sc.nextLine();
        if(bookList.Get_map().containsKey(name)){
            Integer cnt= bookList.Get_map().get(name).getValue();
            if(cnt>0){
                bookList.Get_map().get(name).setValue(bookList.Get_map().get(name).getValue()-1);
            }else
            {
                System.out.println("很抱歉,该书没有存量了");

            }

        }else
        {
            System.out.println("很抱歉,书库中没有"+name+"的书");
        }
        return  true;

    }
}

DelOp

package operator;

import book.BookList;
import sun.awt.geom.AreaOp;

import java.util.Scanner;

/**
 * @author myy
 */
public class DelOp implements IOperation{

    public boolean  work(BookList bookList)
    {

        System.out.println("请输入书名:");
        Scanner sc=new Scanner(System.in);
        String name=sc.nextLine();
        if ( bookList.Get_map().containsKey(name))
        {
            Integer cnt= bookList.Get_map().get(name).getValue();
            if(cnt>0){
                bookList.Get_map().get(name).setValue(bookList.Get_map().get(name).getValue()-1);
            }else
            {
                //这里不删除该书的数据结构了,提醒书库缺书了
                System.out.println("该书,书本缺书了");
            }
        }else
        {
            System.out.println("书本不存在");
        }
        return  true;

    }

}

DisplayOp

package operator;

import book.BookList;

/**
 * @author myy
 */
public class DisplayOp implements IOperation {
    
    public boolean  work(BookList bookList) {
        System.out.println("***********书单****************");
        bookList.Print();
        System.out.println("******************************\n");
        return  true;

    }
}

ExitOp

package operator;

import book.BookList;

/**
 * @author myy
 */
public class ExitOp implements IOperation {
    public boolean  work(BookList bookList) {
        System.out.println("****************New_Young*******");
        return  false;


    }
}

FindOp

package operator;

import book.BookList;

import java.util.Scanner;

/**
 * @author myy
 */
public class FindOp  implements IOperation{
    public boolean  work(BookList bookList)
    {
        System.out.println("请输入你要找的书名:");
        Scanner sc=new Scanner(System.in);
        String name=sc.nextLine();

        if(bookList.Get_map().containsKey(name)){
            System.out.println("书本存在");
        }else
        {
            System.out.println("书本不存在");
        }
        return  true;
    }
}

ReturnOp

package operator;

import book.BookList;

import java.util.Scanner;

/**
 * @author myy
 */
public class ReturnOp implements IOperation {
    public boolean  work(BookList bookList) {

        System.out.println("请输入你要退还的书名:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        bookList.Get_map().get(name).setValue(bookList.Get_map().get(name).getValue() + 1);
        System.out.println("退还书本成功");
        return  true;
    }
}

Main

Java图书管理系统_第5张图片

import book.BookList;
import user.AdminUser;
import user.NormalUser;

import java.util.Scanner;

import user.*;

/**
 * @author myy
 */
public class Main {
    //向上转型
    public static User login() {
        System.out.println("请输入你的姓名");
        Scanner sc = new Scanner(System.in);
        String username = sc.nextLine();

        System.out.println("请输入你的身份 -> 0管理者  1普通用户 3.退出登录");
        int choice = sc.nextInt();
        if (choice == 0) {
            return new AdminUser(username);
        } else if (choice == 1) {
            return new NormalUser(username);
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        //0. 准备数据
        BookList bookList = new BookList();
        //1.登陆
        //*********精华内容:接口数组,多态*********
        while (true) {
            User user = login();
            if (user == null) {
                System.out.println("退出登录成功");
                System.exit(0);
            } else {
                while (true) {
                    int choice = user.menu();
                    if (user.DoOperation(choice, bookList)) {
                        continue;
                    } else {
                        break;
                    }
                }
            }
        }
    }
}

结果

Java图书管理系统_第6张图片

你可能感兴趣的:(Java,java,python,开发语言)