图书管理系统

类的创建

在这里插入图片描述

主函数

import book.BookList;

import user.User;
import java.util.Scanner;

public class Main {
    public static int func1() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你的名字:  ");
        String sc = scanner.nextLine();
        System.out.println("==================================");
        System.out.println("hello! " + sc);
        System.out.print("请输入你的身份: 0. 管理员, 1. 顾客 ");
        return scanner.nextInt();
    }
    public static void main(String[] args) {
        User user = new User(func1());
        BookList bookList = new BookList();
        for (;user.id == 1 ||user.id == 0;){
            if (user.uJudge(user.id,bookList) == 0) break;
        }
    }
}

两种用户及用户

用户

package user;

import book.BookList;

public class User {
    public int id;
    public User(int id) {
        this.id = id;
    }
    public int uJudge(int id, BookList bookList){
        if (id == 1){
            CustomerUser myUser = new CustomerUser();
            return myUser.menu(bookList);
        }else{
            AdminUser myUser = new AdminUser();
            return myUser.menu(bookList);
        }
    }
}

Admin

package user;

import book.BookList;
import func.*;
import java.util.Scanner;

public class AdminUser {
    public int menu(BookList bookList){
        Scanner scanner = new Scanner(System.in);
        System.out.println("=========请选择你要进行的操作=========");
        System.out.println("=============1.查找图书=============");
        System.out.println("=============2.新增图书=============");
        System.out.println("=============3.删除图书=============");
        System.out.println("=============4.显示图书=============");
        System.out.println("=============0.退出系统=============");
        int id = scanner.nextInt();
        switch (id){
            case 1:
                Find find = new Find(bookList);
                break;
            case 2:
                Add add = new Add(bookList);
                break;
            case 3:
                Delete delete = new Delete(bookList);
                break;
            case 4:
                Show show = new Show(bookList);
            case 0:
                break;
            default:
                System.out.println("操作数输入异常!!!");
                menu(bookList);
        }
        return id;
    }
}

Customer

package user;

import book.BookList;
import func.Borrow;
import func.Find;
import func.Still;
import java.util.Scanner;

public class CustomerUser {
    public int menu(BookList bookList){
        Scanner scanner = new Scanner(System.in);
        System.out.println("==========请输入要进行的操作==========");
        System.out.println("=============1.查找图书=============");
        System.out.println("=============2.借阅图书=============");
        System.out.println("=============3.归还图书=============");
        System.out.println("=============0.退出系统=============");
        int id = scanner.nextInt();
        switch (id){
            case 1:
                Find find = new Find(bookList);
                break;
            case 2:
                Borrow borrow = new Borrow(bookList);
                break;
            case 3:
                Still still = new Still(bookList);
                break;
            case 0:
                break;
            default:
                System.out.println("操作数异常!!!");
                menu(bookList);
        }
        return id;
    }
}

功能

Add

package func;

import book.Book;
import book.BookList;
import java.util.Scanner;

public class Add {
    public Add(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("添加图书");
        System.out.println("请输入图书名:");
        String str1 = scanner.nextLine();
        System.out.println("请输入作者:");
        String str2 = scanner.nextLine();
        System.out.println("请输入种类:");
        String str4 = scanner.nextLine();
        System.out.println("请输入价格:");
        int str3 = scanner.nextInt();
        Book[] books = bookList.getBooks();
        books[bookList.getNumber()] = new Book(str1,str2,str3,str4);
        bookList.setNumber(bookList.getNumber()+1);
        System.out.println("添加成功");

    }
}

Borrow

package func;

import book.Book;
import book.BookList;
import java.util.Scanner;

public class Borrow {
    public Borrow(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("借阅图书");
        System.out.println("请输入图书名:");
        String str = scanner.nextLine();
        Book[] books =bookList.getBooks();
        int i=0;
        for (; i < bookList.getNumber() ;i++ ){
            if (str.equals(books[i].getName())){
                if (books[i].getI() == 0){
                    books[i].setI(1);
                    System.out.println("借阅成功");
                    books[i].toString();
                    System.out.println("按enter建继续");
                    scanner.nextLine();
                    return;
                }else {
                    System.out.println("借阅失败,此书已被借阅!!");
                    books[i].toString();
                    System.out.println("按enter建继续");
                    scanner.nextLine();
                    return;
                }
            }
        }
        if (i == bookList.getNumber()){
            System.out.println("借阅失败,没有找到这本书!!");
            System.out.println("按enter建继续");
            scanner.nextLine();
        }
    }
}

Delete

package func;

import book.Book;
import book.BookList;
import java.util.Scanner;

public class Delete {
    public Delete(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("删除图书");
        System.out.println("请输入图书名:");
        String str = scanner.nextLine();
        Book[] books =bookList.getBooks();
        int i=0;
        for (; i < bookList.getNumber() ;i++ ){
            if (str.equals(books[i].getName())){
                del(books,bookList,i);
                System.out.println("删除成功");
                new Show(bookList);
                return;
            }
        }
        if (i == bookList.getNumber()){
            System.out.println("删除失败,没有找到这本书!!");
            System.out.println("按enter建继续");
            scanner.nextLine();
            return;
        }
    }
    public void del(Book[] books,BookList bookList ,int i){
        for (;i < bookList.getNumber()-1;) {
            books[i] = books[i+1];
            i++;
        }
        bookList.setNumber(bookList.getNumber()-1);
    }
}

Find

package func;

import book.Book;
import book.BookList;
import java.util.Scanner;

public class Find {
    public Find(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("查找图书");
        System.out.println("请输入图书名:");
        String str = scanner.nextLine();
        Book[] books =bookList.getBooks();
        int i=0;
        for (; i < bookList.getNumber() ;i++ ){
            if (str.equals(books[i].getName())){
                System.out.println("查找成功");
                System.out.println(books[i].toString());
                System.out.println("按enter建继续");
                scanner.nextLine();
                return;
            }
        }
        if (i == bookList.getNumber()){
            System.out.println("查找失败,没有找到这本书!!");
            System.out.println("按enter建继续");
            scanner.nextLine();
            return;
        }
    }
}

Show

package func;

import book.Book;
import book.BookList;
import java.util.Scanner;

public class Show {
    public Show(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("显示图书");
        Book[] books =bookList.getBooks();
        int i=0;
        for (; i < bookList.getNumber() ;i++ ) {
            System.out.println(books[i].toString());
        }
        System.out.println("所有书都在这儿哦~~");
        System.out.println("按enter建继续");
        scanner.nextLine();
    }
}

Still

package func;

import book.Book;
import book.BookList;
import java.util.Scanner;

public class Still {
    public Still(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("归还图书");
        System.out.println("请输入图书名:");
        String str = scanner.nextLine();
        Book[] books =bookList.getBooks();
        int i=0;
        for (; i < bookList.getNumber() ;i++ ){
            if (str.equals(books[i].getName())){
                if(books[i].getI() == 1) {
                    books[i].setI(0);
                    System.out.println("归还成功");
                    books[i].toString();
                    System.out.println("按enter建继续");
                    scanner.nextLine();
                    return;
                }else {
                    System.out.println("归还失败,此书未借出!!");
                    books[i].toString();
                    System.out.println("按enter建继续");
                    scanner.nextLine();
                    return;
                }
            }
        }
        if (i == bookList.getNumber()){
            System.out.println("归还失败,没有找到这本书!!");
            System.out.println("按enter建继续");
            scanner.nextLine();
            return;
        }
    }
}

书及书架

Book

package book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private int i = 0;
    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 int getI() {
        return i;
    }
    public void setI(int i) {
        this.i = i;
    }
    @Override
    public String toString() {
        return "Book{" +
                "书名:'" + name + '\'' +
                ", 作者:'" + author + '\'' +
                ", 价格:" + price +
                ", 类型:'" + type + '\'' +
                ", 状态:" + ( (getI()==0) ? "未借出" : "已借出" ) +
                '}';
    }
}

BookList

package book;

public class BookList {
    private Book[] books = new Book[20];
    private int number=0 ;
    public BookList() {
        books[0] = new Book("三国演义","罗贯中",20,"小说");
        books[1] = new Book("水浒传","施耐庵",21,"小说");
        books[2] = new Book("西游记","吴承恩",13,"小说");
        books[3] = new Book("红楼梦","曹雪芹",14,"小说");
        number = 4;
    }
    public Book[] getBooks() {
        return books;
    }
    public void setBooks(Book[] books) {
        this.books = books;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
}

实现

管理员的实现

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

顾客的实现

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 38
    点赞
  • 164
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值