用Java实现一个简单的图书管理系统

8 篇文章 1 订阅

效果展示:

client   入口包

Client.java

package com.lh.client;

import com.lh.manager.UserManager;

/**
 * 入口
 * @author 水越帆
 * @date 2018年11月20日 上午10:45:14
 */
public class Client {

    public static void main(String[] args) {
        
        new UserManager().start();
    }

}

bean包

User.java

package com.lh.bean;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.lh.utils.InputUtils;
/**
 * 用户类
 * @author 水越帆
 * @date 2018年11月20日 下午2:49:45
 */
public class User {

    private String username;
    private String password;

    public String getUsername() {
            return username;
    }

    public void setUsername(String username) {
            this.username = username;
    }

    public String getPassword() {
            return password;
    }

    public void setPassword(String password) {
            this.password = password;
    }

    public User() {
    }

    public User(String username, String password) {
            this.username = username;
            this.password = password;
    }

    @Override
    public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((password == null) ? 0 : password.hashCode());
            result = prime * result + ((username == null) ? 0 : username.hashCode());
            return result;
    }

    @Override
    public boolean equals(Object obj) {
            if (this == obj)
                    return true;
            if (obj == null)
                    return false;
            if (getClass() != obj.getClass())
                    return false;
            User other = (User) obj;
            if (password == null) {
                    if (other.password != null)
                            return false;
            } else if (!password.equals(other.password))
                    return false;
            if (username == null) {
                    if (other.username != null)
                            return false;
            } else if (!username.equals(other.username))
                    return false;
            return true;
    }

    // 存书的书包

    private HashMap<Book, Integer> hashMap = new HashMap<>();


    /**
     * 保存书籍
     * @author 水越帆
     * @date 2018年11月20日 下午3:49:21
     */
    public void saveBook(HashMap<Book, Integer> map) {

            Set<Entry<Book, Integer>> entrySet = map.entrySet();

            for (Entry<Book, Integer> entry : entrySet) {
                   
                    Book book = entry.getKey();
                    Integer count = entry.getValue();
 
                    Integer num = hashMap.get(book);

                    // 装到 用户的书包中
                    hashMap.put(book, num == null ? count : num + count);

            }

            System.out.println("借书 成功");

    }

    /**
     * 还书
     * @author 水越帆
     * @date 2018年11月20日 下午3:46:43
     */
    public Map<Book, Integer> returnBook() {

            while (true) {

                    String bname = InputUtils.getStr("请输入书籍的名称:");
                    Book book = new Book(bname);

                    boolean containsKey = hashMap.containsKey(book);

                    if (!containsKey) {
                            System.out.println("没有这个本书,请重新输入");
                            continue;
                    }

                    int num = InputUtils.getInt("请输入还书的数量:");

                    // 书包中书的数量
                    Integer count = hashMap.get(book);

                    if (num > count) {
                            System.out.println("书包中没有这么多书");
                            return null;
                    }

                    
                    hashMap.put(book, count - num);

                    Map<Book, Integer> map = new HashMap<>();
                    map.put(book, num);
                    return map;
            }
    }

    /**
     * 
     * @author 水越帆
     * @date 2018年11月20日 下午3:47:18
     */
    public void showBook() {

            Set<Entry<Book, Integer>> entrySet = hashMap.entrySet();

            System.out.println("图书\t数量");
            for (Entry<Book, Integer> entry : entrySet) {

                    System.out.println(entry.getKey().getBookName() + "\t" + entry.getValue());
            }

    }
}

Book.java

package com.lh.bean;

/**
 * 书籍类
 * @author 水越帆
 * @date 2018年11月20日 下午2:49:20
 */
public class Book {

    private String bookName;

    public Book(String bookName) {
        super();
        this.bookName = bookName;
    }

    public Book() {
        super();
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((bookName == null) ? 0 : bookName.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Book other = (Book) obj;
        if (bookName == null) {
            if (other.bookName != null)
                return false;
        } else if (!bookName.equals(other.bookName))
            return false;
        return true;
    }

}

manager管理包

UserManager.java

package com.lh.manager;

import java.util.HashSet;

import java.util.Set;

import com.lh.bean.User;
import com.lh.utils.InputUtils;
import com.lh.utils.MenuUtils;


public class UserManager {

    private Set<User> users = new HashSet<>();

    public UserManager() {

        users.add(new User("admin", "root"));
    }

    /**
     * 开始菜单
     * @author 水越帆
     * @date 2018年11月20日 下午2:19:35
     */

    public void start() {
        while (true) {

            MenuUtils.welcome();
            int choose = InputUtils.getInt(("请输入您的选择:"));
            switch (choose) {
            case 1:
                User lUser = login();
                if (lUser != null) {
                    BookManager.getInstance().start(lUser);
                } else {
                    System.out.println("登录失败");
                }
                break;

            case 2:
                boolean reg = register();
                System.out.println(reg ? "注册成功" : "注册失败");
                break;

            case 3:
                System.out.println("退出程序");
                System.exit(0);
                break;

            default:
                break;
            }
        }

    }

    /***
     * 登录
     * 
     * @author 水越帆
     * @date 2018年11月20日 下午12:08:47
     */

    private User login() {

        while (true) {
            String username = InputUtils.getStr(("请输入用户名:(exit退出)"));
            if (username.equals("exit")) {
                start();
            }
            
            boolean checkUserName = checkUserName(username);
            
            if (!checkUserName) {
                System.out.println("用户名不存在,请重新输入:");
                continue;
            }
            
            String password = InputUtils.getStr(("请输入您的密码:"));
            for (User user : users) {
                if (user.getUsername().equals(username) && user.getPassword().equals(password)) {

                    // 返回用户
                    return user;
                }

            }

            return null;
        }

    }
    /***
     * 注册**
     * 
     * @author 水越帆
     * @date 2018年11月20日 下午12:09:04
     */

    private boolean register() {
        while (true) {
            String username = InputUtils.getStr("请输入你的用户名:(exit退出)");
            if (username.equals("exit")) {
                return false;
            }

            boolean isCheck = checkUserName(username);
            if (isCheck) {
                System.out.println("用户存在,请重新输入:");
                continue;
            }

            String password = InputUtils.getStr("请输入你的密码:");

            // 添加到 用户管理的集合中
            return users.add(new User(username, password));
        }
    }

    /***
     * 验证用户是否存在**
     * 
     * @author 水越帆
     * @date 2018年11月20日 下午12:05:01
     */

    private boolean checkUserName(String username) {

        for (User user : users) {
            if (user.getUsername().equals(username)) {
                return true;
            }
        }

        return false;

    }
}

BookManager.java

package com.lh.manager;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.lh.bean.Book;
import com.lh.bean.User;
import com.lh.utils.InputUtils;
import com.lh.utils.MenuUtils;

public class BookManager {

    // 饿汉式
    private Map<Book, Integer> books = new HashMap<>();

    {
        books.put(new Book("java"), 5);
        books.put(new Book("mysql"), 5);
        books.put(new Book("elasticsearch"), 5);
        books.put(new Book("springboot"), 5);
        books.put(new Book("jvm"), 5);
    }

    private BookManager() {

    }

    private static BookManager instance = new BookManager();

    public static BookManager getInstance() {

        if (instance == null) {

            instance = new BookManager();

        }

        return instance;

    }

    /**
     * 图书馆菜单
     * 
     * @author 水越帆
     * @date 2018年11月20日 下午2:20:54
     */
    public void start(User u) {

        // 开始菜单
        while (true) {

            MenuUtils.book();
            int choose = InputUtils.getInt("请输入你的选择:(输入数字)");

            switch (choose) {
            case 1:

                showBooks();

                break;
            case 2:

                HashMap<Book, Integer> map = borrowBooks();

                // 把 借阅的书籍 放到 具体的用户中
                if (map != null) {
                    u.saveBook(map);
                } else {
                    System.out.println("借阅失败");
                }

                break;
            case 3:

                Map<Book, Integer> uMap = u.returnBook();

                if (uMap != null) {

                    returnBookManager(uMap);

                } else {
                    System.out.println("还书失败");
                }

                break;
            case 4:

                u.showBook();

                break;
            case 5:
                return;
            }
        }
    }

    /**
     * 还书
     * 
     * @author 水越帆
     * @date 2018年11月20日 下午2:22:49
     */
    private void returnBookManager(Map<Book, Integer> uMap) {

        Set<Entry<Book, Integer>> entrySet = uMap.entrySet();

        for (Entry<Book, Integer> entry : entrySet) {

            Integer count = entry.getValue();
            Book book = entry.getKey();

            // 拿到 图书馆书架上的书籍
            Integer num = books.get(book);

            // 书包中的 书要放到书架上去
            books.put(book, num == null ? count : num + count);

        }

        System.out.println("还书成功");

    }

    /**
     * 借书
     * 
     * @author 水越帆
     * @date 2018年11月20日 下午2:23:23
     */
    private HashMap<Book, Integer> borrowBooks() {
        while (true) {

            String bname = InputUtils.getStr("请输入书名:");

            Book book = new Book(bname);
            // 判断 书名是否存在
            boolean containsKey = books.containsKey(book);

            if (!containsKey) {
                System.out.println("书架上没有这本书,请重新输入:");
                continue;
            }

            int num = InputUtils.getInt("请输入你要借阅的数量:");

            // 图书馆书架上的数量
            Integer count = books.get(book);

            if (num > count) {
                System.out.println("书架上没有那么多书");
                return null;
            }

            // 总数 - 借阅的数量
            books.put(book, count - num);

            // 借阅的书籍 装到自己仓库 返回书包

            HashMap<Book, Integer> map = new HashMap<>();

            map.put(book, num);

            return map;
        }
    }

    private void showBooks() {
        Set<Entry<Book, Integer>> entrySet = books.entrySet();

        System.out.println("图书\t数量");
        for (Entry<Book, Integer> entry : entrySet) {

            System.out.println(entry.getKey().getBookName() + "\t" + entry.getValue());
        }

    }

}

util工具包

InputUtils.java

package com.lh.utils;

import java.util.Scanner;

/**
 * 获取键盘值
 * 
 * @author 水越帆
 * @date 2018年11月20日 下午2:45:41
 */
public class InputUtils {

    private static Scanner scanner = new Scanner(System.in);

    // 获取int值
    public static int getInt(String msg) {

        System.out.println(msg);
        while (true) {
            try {
                return Integer.parseInt(scanner.next());
            } catch (NumberFormatException e) {
                System.out.println("输入错误,请重新输入:");
            }
        }

    }

    // 获取String

    public static String getStr(String string) {
        System.out.println(string);
        return scanner.next();
    }

}

MenuUtils.java

package com.lh.utils;

public class MenuUtils {


    /**
     * 一级菜单
     * @author 水越帆
     * @date 2018年11月20日 上午11:24:03
     */
    public static void welcome() {

        System.out.println("***欢迎来到图书馆***");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出");

    }

    /**
     * 进入后目录
     * @author 水越帆
     * @date 2018年11月20日 上午11:25:25
     */
    public static void book() {

        System.out.println("***欢迎来到图书馆***");
        System.out.println("1.查看图书");
        System.out.println("2.借书");
        System.out.println("3.还书");
        System.out.println("4.查看借阅的书籍");
        System.out.println("5.返回上一级");

    }
}
package com.lh.utils;

public class MenuUtils {


    /**
     * 一级菜单
     * @author 水越帆
     * @date 2018年11月20日 上午11:24:03
     */
    public static void welcome() {

        System.out.println("***欢迎来到图书馆***");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出");

    }

    /**
     * 进入后目录
     * @author 水越帆
     * @date 2018年11月20日 上午11:25:25
     */
    public static void book() {

        System.out.println("***欢迎来到图书馆***");
        System.out.println("1.查看图书");
        System.out.println("2.借书");
        System.out.println("3.还书");
        System.out.println("4.查看借阅的书籍");
        System.out.println("5.返回上一级");

    }
}

 

  • 29
    点赞
  • 218
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值