Java学习记录之简单的图书管理系统(完善中)

电子书管理系统

功能划分

最核心的功能

  1. 提供图书管理的服务
    1. 添加图书
    2. 查询
      1. 检索所有的图书
      2. 按已知检索图书信息
    3. 图书的删除
    4. 图书信息的更新

用户

  1. 用户的类型
    1. 管理员:图书的添加、更新、删除操作
    2. 普通用户:检索
  2. 用户的登录

设计电子书的类,表示电子图书的信息

  1. 图书编号:
    1. 字符串:优点是可以用字母中文表示特殊的内容,缺点是排序效率
    2. 数字:int 、long
  2. 图书名称 : String
  3. 作者:String
  4. 价格:double
  5. get/set和打印
package org.demo.Bean;

public class Book {
    private Integer bookId;
    private String bookName;
    private String bookAutor;
    private double price;
    public Book(){};

    public Book(Integer bookId, String bookName, String bookAutor, double price) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.bookAutor = bookAutor;
        this.price = price;
    }

    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

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

    public String getBookAutor() {
        return bookAutor;
    }

    public void setBookAutor(String bookAutor) {
        this.bookAutor = bookAutor;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                ", bookAutor='" + bookAutor + '\'' +
                ", price=" + price +
                '}';
    }
}

用户类(暂未用到)

package org.demo.Bean;

public class User {
    private Integer userId;
    private String userName;
    private String passwd;
    private String phone;
    public User(){};

    public User(Integer userId, String userName, String passwd, String phone) {
        this.userId = userId;
        this.userName = userName;
        this.passwd = passwd;
        this.phone = phone;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", passwd='" + passwd + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

对图书操作的图书功能类

package org.demo.Service;

import org.demo.Bean.Book;

import java.util.ArrayList;
import java.util.List;

public class BookService {

    //存数据
    private static List<Book> bookList = new ArrayList<>();

    //预存几个图书对象
    static {
        bookList.add( new Book(100,"安徒生童话","安徒生",18));
        bookList.add( new Book(101,"安列卡列尼娜","列夫托尔斯泰",48));
        bookList.add( new Book(102,"流浪地球","刘慈欣",50));
        bookList.add( new Book(103,"三体","刘慈欣",54));
        bookList.add( new Book(104,"唐诗三百首","唐",24));
        bookList.add( new Book(105,"唐诗三百首","李白",34));

    }

    /**
     * 添加书籍信息
     * @param book 图书对象
     */
    public void addBook(Book book){

        //书籍是否重复标记
        boolean bol = true;
        //列表长度即现存几本图书
        int len = bookList.size();
        for (int i = 0; i < len; i++) {
            Book tem = bookList.get(i);
            if (tem.getBookId().equals(book.getBookId())){
                bol = false;
                break;
            }
        }
        if (bol){
            bookList.add(book);
            System.out.println("添加成功!");
        }
        else{
            System.err.println("编号已存在,添加失败!");
        }
    }

    /**
     * 获取所有图书信息
     * @return
     */
    public List<Book> allBookInfo(){
        return bookList;
    }

    /**
     * 获取指定编号的图书信息
     * @param bookId 图书编号
     * @return
     */
    public Book getBookInfo(Integer bookId){
        Book book = null;

        //遍历列表
        int len = bookList.size();
        for (int i = 0; i < len; i++) {
            if (bookList.get(i).getBookId().equals(bookId)){
                book = bookList.get(i);
                break;
            }
        }
        return book;
    }

    /**
     * 获取指定书名的图书信息
     * @param bookAuthor 图书作者
     * @return
     */
    public List<Book> getBookInfo(String bookAuthor){

        List<Book> list = new ArrayList<>();
        //遍历列表
        int len = bookList.size();
        for (int i = 0; i < len; i++) {
            if (bookList.get(i).getBookAutor().equals(bookAuthor)){
                list.add(bookList.get(i));
            }
        }
        return list;
    }

    /**
     * 获取指定书名的图书信息
     * @param bookName
     * @return
     */
    public List<Book> getInfo(String bookName){

        List<Book> list = new ArrayList<>();
        //遍历列表
        int len = bookList.size();
        for (int i = 0; i < len; i++) {
            if (bookList.get(i).getBookName().equals(bookName)){
                list.add(bookList.get(i));
            }
        }
        return list;
    }

    /**
     * 通过图书编号删除对应图书
     * @param bookId 图书编号
     */
    public void delBook(Integer bookId){

        Book book = getBookInfo(bookId);
        if (book != null) {
            bookList.remove(book);
            System.out.println("删除成功!");
        } else {
            System.out.println("删除失败!");
        }
    }
}


菜单类即功能展示和具体操作

package org.demo.Service;

import org.demo.Bean.Book;

import java.util.List;
import java.util.Scanner;

public class MenuService {
    private BookService bookService = new BookService();

    private Scanner sc = new Scanner(System.in);
    //初始菜单
    public void initMenu(){
        System.out.println("#########################");
        System.out.println("####欢迎使用图书管理系统####");
        System.out.println("#########################");
        System.out.println("请输入操作序号选择所需操作:");
        System.out.println("1、添加图书");
        System.out.println("2、查询图书");
        System.out.println("3、删除图书");
        System.out.println("4、退出");

        menu();
    }

    public void menu(){
        //输入选择
        int choose = sc.nextInt();

        switch (choose){
            case 1:
                addBook();
                initMenu();
                break;
            case 2:
                getBookInfo();
                initMenu();
                break;
            case 3:
                delBook();
                initMenu();
                break;
            case 4:
            default:
                System.exit(0);
                break;
        }
    }

    /**
     * 添加图书
     */
    private void addBook() {
        System.out.println("请输入图书编号:");
        Integer bookId = sc.nextInt();
        sc.nextLine();
        System.out.println("请输入图书名称:");
        String bookName = sc.nextLine();
        System.out.println("请输入图书作者:");
        String Author = sc.nextLine();
        System.out.println("请输入图书价格:");
        double price = sc.nextDouble();

        Book book = new Book(bookId ,bookName ,Author ,price);
        bookService.addBook(book);
    }

    /**
     *获取图书信息
     */
    private void getBookInfo() {
        System.out.println("请选择:\n1、查询所有图书;\n2、按图书编号查询;\n3、按作者查询;\n4、按书名查询。");
        int choose = sc.nextInt();
        switch (choose){
            case 1:
                List<Book> books = bookService.allBookInfo();
                for (int i = 0; i < books.size(); i++) {
                    System.out.println( books.get(i));
                }
                break;
            case 2:
                System.out.println("请输入图书编号:");
                int bookId = sc.nextInt();
                Book bookInfo = bookService.getBookInfo(bookId);
                System.out.println( bookInfo );
                break;
            case 3:
                System.out.println("请输入图书作者:");
                sc.nextLine();
                String author = sc.nextLine();
                List<Book> bookList = bookService.getBookInfo(author);
                for (int i = 0; i < bookList.size(); i++) {
                    System.out.println( bookList.get(i));
                }
                break;
            case 4:
                System.out.println("请输入图书名称:");
                sc.nextLine();
                String name = sc.nextLine();
                List<Book> bookNameList = bookService.getInfo(name);
                for (int i = 0; i < bookNameList.size(); i++) {
                    System.out.println( bookNameList.get(i));
                }
                break;
            default:
                System.out.println("功能待补充。。。");
        }
    }

    private void delBook() {
        System.out.println("请输入需要删除的图书编号:");
        Integer bookId = sc.nextInt();
        bookService.delBook(bookId);
    }

}


测试类

import org.demo.Service.MenuService;

public class Test {
    public static void main(String[] args) {
        MenuService menuService = new MenuService();
        menuService.initMenu();
    }
}

注:开学之后较忙且懒,之后的学习记录会简写,不会像之前那么详细。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值