【Java】图书管理系统-控制台输出

一只淡水鱼66 2024-07-04 14:35:03 阅读 64

项目原码Java项目: Java中一些常见的项目源码 (gitee.com)(在IDEA中运行,启动类在src -> Main 中运行)

图书管理系统

设计一个简单的控制台输出的图书管理系统,我们首先需要明确其基本功能、设计内容以及设计要求。这个系统可以包括以下几个核心模块:书籍管理、用户管理、借阅与归还管理。以下是一个基础的设计方案:

设计内容

数据模型设计

Book 类:包含书的属性,如 name(书名)、<code>author(作者)、price(价格)、type(类型)、borrowed(借出情况)等。User 类:包含用户属性,如 name(姓名)、Books(已借书籍列表,可以是Book对象的集合)。

功能模块设计

书籍管理

添加书籍删除书籍显示书籍信息查询书籍(按书名查找)

用户管理

查找图书借阅图书归还图书

设计要求

用户交互友好性

系统应提供清晰的菜单选项,方便用户进行操作。对于错误输入,系统应给出友好的提示信息,指导用户正确操作。

数据一致性与安全性

保证数据的一致性,例如在借书时检查书籍是否可借。虽然是控制台应用,也应考虑简单安全措施,比如对用户密码进行基本的加密处理。

 系统扩展性

设计时应考虑未来可能的功能扩展,如增加评论系统、推荐系统等,确保现有架构易于扩展。

在该项目中我们设计了两个菜单:

 用来各执行不同的操作。

项目操作步骤

1、建立包:

在该项目中我们建立三个包:1、book 2、operation 3、user 分别用来存放与书籍相关的类、与操作相关的类、与用户相关的类。

编写与书籍相关的类:

Book类的代码:

<code>package book;

import java.util.Objects;

/**

* Created with IntelliJ IDEA.

* Description:图书

* User: lenovo

* Date: 2024-06-04

* Time: 20:13

*/

public class Book {

private String name;//书名

private String auther;//作者

private int price;//价格

private String type;//书的类型

private boolean isBorrowed;//书是否被借出

public Book(String name, String auther, int price, String type) {

this.name = name;

this.auther = auther;

this.price = price;

this.type = type;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAuther() {

return auther;

}

public void setAuther(String auther) {

this.auther = auther;

}

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 isBorrowed() {

return isBorrowed;

}

public void setBorrowed(boolean borrowed) {

isBorrowed = borrowed;

}

@Override

public String toString() {

return "Book{" +

"书名:'" + name + '\'' +

", 作者:'" + auther + '\'' +

", 价格:" + price +

", 类型:'" + type + '\'' +

((isBorrowed == true)?",借出情况:已借出":",借出情况:未借出") +

'}';

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Book book = (Book) o;

return price == book.price && isBorrowed == book.isBorrowed && Objects.equals(name, book.name) && Objects.equals(auther, book.auther) && Objects.equals(type, book.type);

}

@Override

public int hashCode() {

return Objects.hash(name, auther, price, type, isBorrowed);

}

}

BookList类的代码:

package book;

/**

* Created with IntelliJ IDEA.

* Description:书架

* User: lenovo

* Date: 2024-06-04

* Time: 20:29

*/

public class BookList {

private Book[] booklist = new Book[10];//用来存放书

private int usedSize;//计数器,用来记录当前书架有多少书

public BookList() {

booklist[0] = new Book("三国演义","罗贯中",71,"小说");

booklist[1] = new Book("西游记","吴承恩",82,"小说");

booklist[2] = new Book("红楼梦","曹雪芹",79,"小说");

this.usedSize = 3;

}

public void setBooks(int pos, Book book){

booklist[pos] = book;

}

public Book getBook(int pos){

return booklist[pos];

}

public int getUsedSize() {

return usedSize;

}

public void setUsedSize(int usedSize) {

this.usedSize = usedSize;

}

}

编写与用户相关的类:

AdminUser 类的代码:

<code>package user;

import operation.*;

import java.util.Scanner;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 22:19

*/

public class AdminUser extends Users{

public AdminUser(String name) {

super(name);

this.books = new IBooks[]{

new ExitOperation(),

new FindBooks(),

new AddBooks(),

new DelBooks(),

new ShowBooks()

};

}

@Override

public int menu() {

System.out.println("=====================================");

System.out.println("hallo! "+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("请选择你要操作的序号:>");

System.out.println("=====================================");

Scanner scanner = new Scanner(System.in);

int choice = scanner.nextInt();

return choice;

}

}

Normaluser 类的编写:

package user;

import operation.*;

import java.util.Scanner;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 22:19

*/

public class NormalUser extends Users{

public NormalUser(String name) {

super(name);

this.books = new IBooks[]{

new ExitOperation(),

new FindBooks(),

new BorrowBooks(),

new ReturnBooks()

};

}

@Override

public int menu() {

System.out.println("========================================");

System.out.println("hallo! "+this.name+" 欢迎来到普通用户菜单菜单");

System.out.println("1.查找图书");

System.out.println("2.借阅图书");

System.out.println("3.归还图书");

System.out.println("0.退出系统");

System.out.println("请选择你要操作的序号:>");

System.out.println("=======================================");

Scanner scanner = new Scanner(System.in);

int choice = scanner.nextInt();

return choice;

}

}

Users 类的代码:

package user;

import book.BookList;

import operation.IBooks;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 22:13

*/

public abstract class Users {

protected String name;

public IBooks[] books;

public Users(String name) {

this.name = name;

}

public abstract int menu();

public void doOperation(int choice,BookList booklist){

books[choice].work(booklist);

}

}

编写与操作相关的类:

AddBooks 类代码的编写:

<code>package operation;

import book.Book;

import book.BookList;

import java.util.Scanner;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 21:54

*/

public class AddBooks implements IBooks{

@Override

public void work(BookList booklist){

Scanner scanner = new Scanner(System.in);

System.out.println("请输入书名");

String name = scanner.nextLine();

System.out.println("请输入作者");

String auther = scanner.nextLine();

System.out.println("请输入价格");

int price = scanner.nextInt();

scanner.nextLine();

System.out.println("请输入书的类型");

String type = scanner.nextLine();

Book book = new Book(name,auther,price,type);

int count = booklist.getUsedSize();

for (int i = 0; i < count; i++) {

Book book1 = booklist.getBook(i);

if(book1.getName().equals(name)){

System.out.println("已经存在这本书,不能在进行添加了");

return;

}

}

booklist.setBooks(count,book);

booklist.setUsedSize(count+1);

System.out.println("添加成功!");

}

}

BorrowBooks 类的代码编写:

package operation;

import book.Book;

import book.BookList;

import java.util.Scanner;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 22:09

*/

public class BorrowBooks implements IBooks{

@Override

public void work(BookList booklist) {

int count = booklist.getUsedSize();

if(count == 0){

System.out.println("图书馆没有图书!");

return;

}

Scanner scanner = new Scanner(System.in);

System.out.println("请输入你要借阅的书籍的名字:>");

String name = scanner.nextLine();

int tmp = -1;

for (int i = 0; i < count; i++) {

Book book = booklist.getBook(i);

if(book.getName().equals(name)){

tmp = i;

break;

}

}

if(tmp == -1){

System.out.println("图书馆没有该图书!");

return;

}

Book book1 = booklist.getBook(tmp);

if(book1.isBorrowed() == true){

System.out.println("该书籍已经被借出!");

return;

}

book1.setBorrowed(true);

System.out.println("借阅成功!");

return;

}

}

DelBooks 类的代码编写:

package operation;

import book.Book;

import book.BookList;

import java.util.Scanner;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 22:02

*/

public class DelBooks implements IBooks{

@Override

public void work(BookList booklist){

int count = booklist.getUsedSize();

if(count == 0){

System.out.println("图书馆没有图书!");

return;

}

Scanner scanner = new Scanner(System.in);

System.out.println("请输入你要删除的图书的名字:>");

String name = scanner.nextLine();

int tmp = -1;

for (int i = 0; i < count; i++) {

Book book = booklist.getBook(i);

if(book.getName().equals(name)){

tmp = i;

break;

}

}

if(tmp == -1){

System.out.println("没有找到该图书,请重新选择!");

return;

}else{

for (int i = tmp; i < count - 1; i++) {

Book book1 = booklist.getBook(i+1);

booklist.setBooks(i,book1);

}

}

booklist.setUsedSize(count - 1);

System.out.println("删除图书成功!");

return;

}

}

ExitOperation 类的代码编写:

package operation;

import book.BookList;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 22:06

*/

public class ExitOperation implements IBooks{

@Override

public void work(BookList booklist) {

int count = booklist.getUsedSize();

for (int i = 0; i < count; i++) {

booklist.setBooks(i,null);

}

booklist.setUsedSize(0);

System.out.println("成功退出图书管理系统");

System.exit(0);

}

}

FindBooks 类的代码:

package operation;

import book.Book;

import book.BookList;

import java.util.Scanner;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 21:57

*/

public class FindBooks implements IBooks{

@Override

public void work(BookList booklist){

Scanner scanner = new Scanner(System.in);

System.out.println("请输入你要查找的书名");

String name = scanner.nextLine();

int count = booklist.getUsedSize();

for (int i = 0; i < count; i++) {

Book book = booklist.getBook(i);

if(book.getName().equals(name)){

System.out.println(book);

return;

}

}

System.out.println("没有这本图书");

}

}

IBooks 类的代码:

package operation;

import book.BookList;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 22:00

*/

public interface IBooks {

void work(BookList booklist);

}

ReturnBooks 类的代码:

import book.BookList;

import java.util.Scanner;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 22:10

*/

public class ReturnBooks implements IBooks{

@Override

public void work(BookList booklist) {

int count = booklist.getUsedSize();

Scanner scanner = new Scanner(System.in);

System.out.println("请输入要归还书籍的名字::");

String name = scanner.nextLine();

int tmp = -1;

for (int i = 0; i < count; i++) {

Book book = booklist.getBook(i);

if(book.getName().equals(name)){

tmp = i;

break;

}

}

if(tmp == -1){

System.out.println("图书馆没有该图书");

return;

}

Book book2 = booklist.getBook(tmp);

book2.setBorrowed(false);

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

}

}

ShowBooks 类的代码:

package operation;

import book.Book;

import book.BookList;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 22:03

*/

public class ShowBooks implements IBooks{

@Override

public void work(BookList booklist) {

int count = booklist.getUsedSize();

if(count == 0){

System.out.println("图书馆没有图书!");

return;

}

for (int i = 0; i < count; i++) {

Book book = booklist.getBook(i);

System.out.println(book);

}

}

}

Main 类的代码:

import book.BookList;

import user.AdminUser;

import user.NormalUser;

import user.Users;

import java.util.Scanner;

/**

* Created with IntelliJ IDEA.

* Description:

* User: lenovo

* Date: 2024-06-04

* Time: 20:08

*/

public class Main {

public static Users login(){

System.out.println("请输入你的姓名:>");

Scanner scanner = new Scanner(System.in);

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();

Users users = login();

while(true){

int choice = users.menu();

users.doOperation(choice,booklist);

}

}

}

最后代码在Main类中启动。

运行结果如下图:



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。