栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

<< Java >> 图书管理系统

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

<< Java >> 图书管理系统

目录

​​​​​1.Main

​​​​​​​​​​​​2.User包

❤️User父类 

2.1管理员子类

2.2普通用户子类

3Book包

3.1 Book

3.2BookList

4 .operation

❤️IOperation接口

4.1Add

4.2 Borrow

4.3 Delete

4.4 Display

 4.5 Exit

4.7 Return



1.Main
import book.BookList;
import com.sun.xml.internal.ws.wsdl.writer.document.Operation;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {

    private static User login(){
        System.out.println("请输入你的姓名");
        String name;
        Scanner scanner = new Scanner(System.in);
        name = scanner.nextLine();
        System.out.println("请输入你的身份---> 1.管理员  2.普通用户");
        int flag = scanner.nextInt();
        if(flag == 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);
        }
    }

}

2.User包

User父类 
package user;

import book.BookList;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import operation.IOperation;

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

2.1管理员子类
package user;

import book.BookList;
import operation.*;

import java.util.Scanner;

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.operation = new IOperation[]{new Exit(),new Find(),new Add(),new Delete(),new Display()};
    }
    @Override
    public int menu() {
        System.out.println("1.查找图书!");
        System.out.println("2.新增图书!");
        System.out.println("3.删除图书!");
        System.out.println("4.显示图书!");
        System.out.println("0.退出系统!");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int flag = scanner.nextInt();
        return flag;
    }
}

2.2普通用户子类
package user;

import operation.*;

import java.util.Scanner;

public class NormalUser extends  User{
    public NormalUser(String name) {
        super(name);
        this.operation = new IOperation[]{new Exit(),new Find(),new Borrow(),new Return()};
    }

    @Override
    public int menu() {
        System.out.println("1.查找图书!");
        System.out.println("2.借阅图书!");
        System.out.println("3.归还图书!");
        System.out.println("0.推出系统!");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int flag = scanner.nextInt();
        return flag;
    }
}

3Book包

3.1 Book
package book;

import java.util.Objects;

public class Book {
    private String name;//书名
    private String author;//作者
    private int price;//价格
    private String type;//类别
    private boolean isborrowed;//是否被借出

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
        this.isborrowed = false;
    }

    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 isIsborrowed() {
        return isborrowed;
    }

    public void setIsborrowed(boolean isborrowed) {
        this.isborrowed = isborrowed;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + ''' +
                ", author='" + author + ''' +
                ", price=" + price +
                ", type='" + type + ''' +
                ", isborrowed=" + isborrowed +
                '}';
    }


    @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(author, book.author) && Objects.equals(type, book.type);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, author, price, type, isborrowed);
    }
}

3.2BookList
package book;

public class BookList {
    //目前最多可以放十本书
    private Book[] books = new Book[10];
    private int usedSize;//用来记录现在有几本

    public BookList() {
        books[0]=new Book("三国演义","罗贯中",30,"四大名著");
        books[1]=new Book("西游记","吴承恩",40,"四大名著");
        books[2]=new Book("红楼梦","曹雪芹",40,"四大名著");
        books[3]=new Book("水浒传","施耐庵",30,"四大名著");
        usedSize = 4;
    }

    public void setBooks(Book[] books) {
        this.books = books;
    }

    public Book getBooks(int pos) {
        return books[pos];
    }
    public void setBooks(int pos,Book books) {//pos位置合法,把book书放到pos位置
        this.books[pos] = books;
    }

    public int getUsedSize() {
        return usedSize;
    }
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }


}

4 .operation

IOperation接口
package operation;

import book.BookList;

public interface IOperation {
     public void work(BookList bookList);
}

4.1Add
package operation;

import book.Book;
import book.BookList;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Scanner;

public class Add 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.nextLine();
        Book book = new Book(name,author,price,type);
        bookList.setBooks(bookList.getUsedSize(), book);
        //将书架的数目增加 1
        bookList.setUsedSize(bookList.getUsedSize()+1);
    }
}

4.2 Borrow
package operation;

import book.BookList;

import java.util.Scanner;

public class Borrow implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入你要借的书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name == bookList.getBooks(i).getName()){
                bookList.getBooks(i).setIsborrowed(true);
                System.out.println("借书成功!");
                return;
            }
        }
    }
}

4.3 Delete
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Delete implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要删除的书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name.equals(bookList.getBooks(i).getName())){

                for (int j = i; j 

4.4 Display
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Delete implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要删除的书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name.equals(bookList.getBooks(i).getName())){

                for (int j = i; j 

 4.5 Exit
package operation;

import book.BookList;

public class Exit implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("退出成功");
        System.exit(0);
    }
}

4.6Find

package operation;

import book.BookList;

import java.util.Scanner;

public class Find implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要查找的书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            System.out.println("i=="+i);
            if(name.equals(bookList.getBooks(i).getName())){
                System.out.println("这本书的位置是"+i);
                System.out.println("这本书的信息是"+bookList.getBooks(i).toString());
                return;
            }
        }
        System.out.println("没有这本书!!");
        return;
    }
}

4.7 Return
package operation;

import book.BookList;
import user.AdminUser;
import user.User;

import java.util.Scanner;

public class Return implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要归还的书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name == bookList.getBooks(i).getName()){
                bookList.getBooks(i).setIsborrowed(false);
                System.out.println("成功还书!");
                return;
            }
        }
        System.out.println("这本书不是图书馆的书,but已成功捐赠!!!");
        User user = new AdminUser("shan");
        user.doOperation(2,bookList);        
    }
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925058.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号