【JavaSE】图书管理系统源码

您所在的位置:网站首页 java项目图书管理系统源码 【JavaSE】图书管理系统源码

【JavaSE】图书管理系统源码

2023-07-25 07:37| 来源: 网络整理| 查看: 265

一:简介

此图书管理系统项目是利用类,抽象类,封装,继承,多态,接口等进行的一个代码练习。

二:核心需求

1、简单的用户界面(控制台界面) 登录界面 管理员界面 用户界面

2、管理员需要实现的功能 查阅书籍 增加书籍 删除书籍 打印书籍列表 退出

3、普通用户需要实现的功能 查询书籍 借阅书籍 归还书籍 退出

三: 类的设计 1. 创建图书相关的类

先创建 package book

创建 Book 类, 表示一本书

package book; public class Book { private String name; private String author; private int price; private String type; private boolean status; public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getType() { return type; } public void setType(String type) { this.type = type; } public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ", status=" + ((status == true) ? " 借出 " : " 未借出 ") + '}'; } }

创建 BookList 类, 用来保存 N 本书

package book; public class BookList { private Book[] books; private int usedSize; public BookList() { this.books = new Book[10]; books[0] = new Book("三国演义","罗贯中",72,"小说"); books[1] = new Book("西游记","吴承恩",32,"小说"); books[2] = new Book("水浒传","施耐庵",52,"小说"); this.usedSize = 3; } public void setBooks(int pos,Book book) { this.books[pos] = book; } public Book getBooks(int pos) { return this.books[pos]; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; } } 2. 创建操作相关的类

先创建 package operation

package operation; import book.BookList; public interface IOperation { void work(BookList bookList); }

接下来创建一组操作类, 每个类对应一个用户的动作 1.添加书籍

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class AddOperation implements IOperation{ @Override public void work(BookList bookList) { Scanner scanner = new Scanner(System.in); System.out.println("请输入图书的名字"); String name = scanner.nextLine(); System.out.println("请输入图书的作者"); String author = scanner.nextLine(); System.out.println("请输入图书的价格"); int price = scanner.nextInt(); System.out.println("请输入图书的类型"); String type = scanner.next(); Book book = new Book(name,author,price,type); bookList.setBooks(bookList.getUsedSize(),book); bookList.setUsedSize(bookList.getUsedSize()+1); } }

2.借阅书籍

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class BorrowOperation implements IOperation{ @Override public void work(BookList bookList) { Scanner scanner = new Scanner(System.in); System.out.println("请输入要借阅的图书的名字"); String name = scanner.nextLine(); int i = 0; for (; i book.setStatus(true); break; } } if(i >= bookList.getUsedSize()) { System.out.println("没有要借阅的这本书!"); return; } } }

3.删除书籍

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class DelOperation implements IOperation { @Override public void work(BookList bookList) { Scanner scanner = new Scanner(System.in); System.out.println("请输入要删除的图书的名字"); String name = scanner.nextLine(); int i = 0; for (; i break; } } if(i >= bookList.getUsedSize()) { System.out.println("没有要删除的这本书!"); return; } int pos = i; for (int j = pos; j @Override public void work(BookList bookList) { System.out.println("展示图书"); for (int i = 0; i @Override public void work(BookList bookList) { System.exit(1); System.out.println("成功退出系统!"); } }

6.查找书籍

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class FindOperation implements IOperation { @Override public void work(BookList bookList) { Scanner scanner = new Scanner(System.in); System.out.println("请输入要查找的图书的名字"); String name = scanner.nextLine(); int i = 0; for (; i System.out.println(book); break; } } if(i >= bookList.getUsedSize()) { System.out.println("没有要查找的这本书!"); return; } } }

7.归还书籍

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class ReturnOperation implements IOperation { @Override public void work(BookList bookList) { Scanner scanner = new Scanner(System.in); System.out.println("请输入要归还的图书的名字"); String name = scanner.nextLine(); int i = 0; for (; i book.setStatus(false); break; } } if(i >= bookList.getUsedSize()) { System.out.println("没有要归还的这本书!"); return; } } } 3. 创建用户相关的类

先创建 package user

创建 User 类, 这是一个抽象类

package user; import book.BookList; import operation.IOperation; public abstract class User { public String name; public IOperation[] operations; public User(String name) { this.name = name; } public abstract int menu(); public void doOperation(int choice, BookList bookList) { this.operations[choice].work(bookList); } }

创建普通用户类, 是 User 的子类

package user; import operation.*; import java.util.Scanner; public class NormalUser extends User { public NormalUser(String name) { super(name); this.operations = new IOperation[]{ new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } @Override public int menu() { Scanner scanner = new Scanner(System.in); System.out.println("普通用户菜单"); 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("============================"); int choice = scanner.nextInt(); return choice; } }

创建管理员用户类

package user; import operation.*; import java.util.Scanner; public class AdminUser extends User { public AdminUser(String name) { super(name); this.operations = new IOperation[] { new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation() }; } @Override public int menu() { Scanner scanner = new Scanner(System.in); System.out.println("管理员菜单"); 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("============================"); int choice = scanner.nextInt(); return choice; } } 4. 进行整合

创建 Main 类和 main 方法, 搭建整体逻辑

import book.BookList; import user.AdminUser; import user.NormalUser; import user.User; import java.util.Scanner; public class Main { public static User login() { Scanner scanner = new Scanner(System.in); System.out.println("请输入你的姓名:"); String name = scanner.nextLine(); System.out.println("请输入你的身份:1-》管理员 0-》普通用户"); int choice = scanner.nextInt(); if (choice == 1) { return new AdminUser(name); } else { return new NormalUser(name); } } public static void main(String[] args) { BookList bookList = new BookList(); User user = login(); while (true) { int choice = user.menu(); user.doOperation(choice,bookList); } } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3