Java-实现图书管理系统

目录

运行部分的头文件

运行部分(Main)

关于书的类

每本书的类Book

书集合的类Booktest

关于用户的类

父类User

管理员子类ManagerUser

一般用户子类NormalUser

方法的类

总接口

查找图书

新增图书

删除图书

显示图书

借阅图书

归还图书

退出程序


java实现图书管理系统,要求两种运行方式1.管理员 2.使用者
管理员功能:查找图书,新增图书,删除图书,显示图书,退出系统
使用者功能:查找图书,借阅图书,归还图书,退出系统

运行部分的头文件

import java.util.Scanner;

运行部分(Main)


public class test {
    public static User login(){//这一切的实现前提都是因为继承的向上转型

        System.out.println("请选择模式"+"0.管理员模式"+"1.用户模式");
        Scanner sc = new Scanner(System.in);
        int choose = sc.nextInt();
        sc.nextLine();
        System.out.println("请输入姓名:>");
        String name = sc.nextLine();//是nextLine啊
        if(choose==0){
            return new ManagerUser(name);
        }
        else{
            return new NormalUser(name);
        }
    }

    public static void main(String[] args) {
        User user = login();//别忘了一般方法的定义
        while(true){
            user.menu();//在外面没办法调用子类代码块 但是可以通过被重写的方法调用 所以接口数组的创建也直接放在里面
        }
    }
}

关于书的类

每本书的类Book

class Book{
    private String name;
    private String author;
    private String type;
    private int price;
    private boolean state;

    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 String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

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

    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }

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

    @Override
    public String toString() {

        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", type='" + type + '\'' +
                ", price=" + price +
                ((state==true)?" 已借出":" 未借出")//为啥一定要括号?
                +'}';
    }
}

书集合的类Booktest

class BookTest{
    private Book [] books = new Book[10];//这个是数组的创建,此时还没初始化
    private int size;
    public BookTest(){
        books[0] = new Book("三国演义","罗贯中","小说",40);//这个才是初始化 实例化分成两步了 起名字和new 上一步的new不是开辟空间
        books[1] = new Book("红楼梦","曹雪芹","小说",35);
        books[2] = new Book("西游记","吴承恩","小说",38);
        books[3] = new Book("水浒传","施耐庵","小说",41);
        size = 4;
    }

    public Book getBooks(int size) {
        return books[size];//类方法是可以直接调用类内成员的
    }

    public Book[] getBooks1() {
        return books;
    }

    public void setBooks(Book book, int size) {
        this.books[size] = book;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
    public void setstate(int size,boolean n){
        books[size].setState(n);
    }
}

关于用户的类

父类User

abstract class User{
    String name;
    BookTest bookTest = new BookTest();
    abstract public void menu();
    public User(String name){
        this.name = name;
    }

}

管理员子类ManagerUser

class ManagerUser extends User{
    public ManagerUser(String name){//如果父类没有构造方法 就不需要
        super(name);//只能实现一个构造方法的前提,要先给name初始化,如果给name初始化则需调用super,且如果父类有非默认构造方法,一定要super
        System.out.println("管理员"+name);//就是父类构造方法要有参数,就得给他传参
    }
    public void menu(){
        System.out.println("1.查找图书");
        System.out.println("2.新增图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        System.out.println("0.退出程序");
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        I0Operation [] iooperations = {
                new ExitOPeration(),new FindOPeration(),new AddOPeration(),new DelOPeration(),new DisplayOPeration()
        };
        iooperations[input].work(bookTest);
    }


}

一般用户子类NormalUser

class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        System.out.println("用户"+name);
    }

    public void menu(){
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出程序");
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        I0Operation [] iooperations = {
                new ExitOPeration(),new FindOPeration(),new BorOPeration(),new ReturnOPeration()
        };
        iooperations[input].work(bookTest);//不用担心内存浪费,反正运行完menu方式就释放了
    }
    
}

方法的类

总接口 I0Operation

public interface I0Operation {
    void work(BookTest bookTest);
}

查找图书 FindOPeration

import java.util.Scanner;

public class FindOPeration implements I0Operation{
    @Override
    public void work(BookTest bookTest) {
        System.out.println("查找图书");
        int size = bookTest.getSize();
        System.out.println("请输入被查找书名");
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int t = 0;
        for (; t < size-1; t++) {
            if(s.equals(bookTest.getBooks(t).getName())){
                break;
            }
        }
        if(t==size){
            System.out.println("没有这本书");
        }
        else{
            System.out.println(bookTest.getBooks(t));
        }
    }

}

新增图书 AddOPeration

import java.util.Scanner;


public class AddOPeration implements I0Operation{

    @Override
    public void work(BookTest bookTest) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = sc.nextLine();
        System.out.println("请输入作者名");
        String textname = sc.nextLine();
        System.out.println("请输入类型");
        String state = sc.nextLine();
        System.out.println("请输入价格");
        int price = sc.nextInt();
        Book book = new Book(name,textname,state,price);
        bookTest.setBooks(book,bookTest.getSize());
        bookTest.setSize(bookTest.getSize()+1);
        System.out.println("增加成功!");
    }
}

删除图书 DelOPeration

import java.util.Scanner;

public class DelOPeration implements I0Operation{
    @Override
    public void work(BookTest bookTest) {
        int size = bookTest.getSize();
        System.out.println("请输入被删除书名");
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int t = 0;
        for (; t < size-1; t++) {
            if(s.equals(bookTest.getBooks(t).getName())){
                break;
            }
        }
        if(t==size){
            System.out.println("没有这本书");
            return;
        }
        else {
            for (int i = t; i < size - 1; i++) {
                bookTest.setBooks(bookTest.getBooks(size), size - 1);
            }
            bookTest.setSize(size-1);
        }
        System.out.println("删除成功!");
        }
}

显示图书 DisplayOPeration

public class DisplayOPeration implements I0Operation{

    @Override
    public void work(BookTest bookTest) {
        int size = bookTest.getSize();
        for (int i = 0; i < size; i++) {
            System.out.println(bookTest.getBooks(i));
        }
    }
}

借阅图书 BorOPeration

import java.util.Scanner;

public class BorOPeration implements I0Operation{
    @Override
    public void work(BookTest bookTest) {
        int size = bookTest.getSize();
        System.out.println("请输入要借的书名");
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int t = 0;
        for (; t < size-1; t++) {
            if(s.equals(bookTest.getBooks(t).getName())){
                break;
            }
        }
        if(t==size){
            System.out.println("没有这本书");
            return;
        }
        else{
            if(bookTest.getBooks(t).isState()==true){
                System.out.println("已借出");
                return;
            }
            bookTest.setstate(t,true);
        }
        System.out.println("借书成功");
    }
}

归还图书 ReturnOPeration

import java.util.Scanner;

public class ReturnOPeration implements I0Operation{

    @Override
    public void work(BookTest bookTest) {
            int size = bookTest.getSize();
            System.out.println("请输入要还的书名");
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            int t = 0;
            for (; t < size-1; t++) {
                if(s.equals(bookTest.getBooks(t).getName())){
                    break;
                }
            }
            if(t==size){
                System.out.println("没有这本书");
                return;
            }
            else{
                if(bookTest.getBooks(t).isState()==false){
                    System.out.println("没借还啥,非得找bug是吧");
                    return;
                }
                bookTest.setstate(t,false);
            }
            System.out.println("还书成功");
        }

    }

退出程序 ExitOPeration

public class ExitOPeration implements I0Operation{

    @Override
    public void work(BookTest bookTest) {
        System.out.println("退出程序!");
          System.exit(0);
    }
}

你可能感兴趣的:(java)