Java面向对象 -- 图书管理系统

题目:利用抽象类、接口等实现简单的图书管理系统。
整体布局
在这里插入图片描述
Test 类

package user.action;

import java.util.Scanner;

public class Test {

    public void choose() {
        while (true) {
            System.out.println("------------图论科技图书管理系统------------\n");
            System.out.print("请登录:1:普通用户   2:管理员登录\n\n");
            Scanner in = new Scanner(System.in);

            int choose = in.nextInt();
            Scanner scan = new Scanner(System.in);
            User user = null;
            AdminPerson adminPerson = null;
            switch (choose) {
                case 1:
                    System.out.print("请输入:姓名 \n");
                    String userName = scan.next();
                    System.out.print("请输入:性别 \n");
                    String userSex = scan.next();
                    System.out.print("请输入:年龄 \n");
                    int userAge = scan.nextInt();
                    user = new User(userName, userSex, userAge);
                    System.out.println("当前用户:" + userName + " " + userSex + " " + userAge);
                    user.start();
                    break;
                case 2:
                    System.out.print("请输入:姓名 \n");
                    String adminName = scan.next();
                    System.out.print("请输入:性别 \n");
                    String adminSex = scan.next();
                    System.out.print("请输入:年龄 \n");
                    int adminAge = scan.nextInt();
                    System.out.println("当前管理员:" + adminName + " " + adminSex + " " + adminAge);
                    adminPerson = new AdminPerson(adminName, adminSex, adminAge);
                    adminPerson.start();
                    break;
            }
        }
    }
    public static void main(String[] args){
        Test test = new Test();
        test.choose();
    }

}

Person 抽象类

package user.action;

import java.util.Scanner;

abstract class Person {
    protected static String name;
    protected static String sex;
    protected static int age;
    Scanner input = new Scanner(System.in);

    public static String getName() {
        return name;
    }

    public static String getSex() {
        return sex;
    }

    public static int getAge() {
        return age;
    }

    public static void setName(String name) {
        Person.name = name;
    }

    public static void setSex(String sex) {
        Person.sex = sex;
    }

    public static void setAge(int age) {
        Person.age = age;
    }

    /**
     * 查找图书信息
     * @param book
     */
    public void selectBook(String[][] book) {
        String m;
        System.out.println("请输入您要查找的图书名字:");
        m = input.next();
        if (Book.count == 0) {
            System.out.println("不好意思,无此书籍!");
        } else {
            boolean flag = true;
            while(flag){
            for (int i = 0; i < book.length; i++) {
                if (book[i][1].equals(m)) {
                    System.out.println("图书编号:" + book[i][0]);
                    System.out.println("图书名字:" + book[i][1]);
                    System.out.println("图书作者:" + book[i][2]);
                    System.out.println("图书数量:" + book[i][3]);
                    System.out.println("~~~~~~图书信息查找完毕~~~~~~");
                    flag = false;
                    break;
                }else{
                    System.out.println("不好意思,无此书籍!");
                    flag = false;
                    break;
                }
              }
            }
        }
    }
}

User 类

package user.action;

import java.util.Scanner;

public class User extends Person {
    //用户姓名
    private static String userName;
    //用户性别
    private static String userSex;
    //用户年龄
    private static int userAge;
    //无参构造函数
    Book book = new Book();
    public User(){

    }
    //有参构造函数
    public User(String userName,String userSex,int userAge){

    }

    //借阅书籍
    public void borrowBook(String[][] book1){
        System.out.println("请输入您要借阅的图书名字:");
        String m;
        int n = -1;
        m = input.next();
            for (int i = 0; i < book1.length; i++) {
                if (book1[i][1].equals(m)) {
                    n = i;
                    break;
                }
            }
            int p = Integer.parseInt(Book.book1[n][3]);
            if (p == 0) {
                System.out.println("不好意思,已经被借光了~");
            } else {
                Book.book1[n][3] = (p - 1) + "";
                System.out.println("借阅成功!");
            }
    }

    /**
     * 归还图书
     * @param book1
     */
    public void returnBook(String[][] book1){
        int BookId1,BookNum1;
        String BookName1,Author1;

        System.out.println("~~~~~~开始归还图书~~~~~~");
        System.out.println("请输入归还图书名字:");
        BookName1 = input.next();
        for (int i = 0; i < Book.count; i++) {
            if (book1[i][1].equals(BookName1)) {
                    int p = Integer.parseInt(book1[i][3]);
                    book1[i][3] = (p+1) + "";
                    break;
                }
            }
        System.out.println("~~~~~~归还成功~~~~~~");
    }

    /**
     * 用户操作函数
     */
    public void start(){
        Scanner input = new Scanner(System.in);

        boolean flag = true;
        int number;
        while(flag) {
            System.out.println("---------------------------------------------------");
            System.out.print("请选择:1:查询书籍  2:借阅书籍  3:归还书籍  4:退出\n\n");
            number = input.nextInt();
            switch (number) {
                case 1:
                    selectBook(Book.book1);
                    break;
                case 2:
                    borrowBook(Book.book1);
                    break;
                case 3:
                    returnBook(Book.book1);
                    break;
                case 4:
                    System.out.println("~~~~~~您已退出系统~~~~~~");
                    flag = false;
                    break;
                    default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }
}


Operate 接口

package user.impl;

public interface Operate {
    void arrangeBooks(String[][] book);
    void printBooksList(String[][] book);
    void alter_s(String[][] book1);
}

AdminPerson 类

package user.action;

import user.impl.Operate;

import java.util.Scanner;

public class AdminPerson extends Person implements Operate,Comparable<Book> {
    //用户姓名
    private static String adminName;
    //用户性别
    private static String adminSex;
    //用户年龄
    private static int adminAge;

    Scanner input = new Scanner(System.in);

    Book book = new Book();

    //无参构造函数
    public AdminPerson(){

    }
    //有参构造函数
    public AdminPerson(String adminName,String adminSex,int adminAge){

    }

    /**
     * 按图书编号整理书籍
     * @param book1
     */
    @Override
    public void arrangeBooks(String[][] book1) {
        String[] temp;
        for(int i = 0;i < Book.count-1;i++){
            for(int j=0;j < Book.count-1-i;j++) {
                if (book1[j][0].compareTo(book1[j + 1][0]) > 0) {
                    temp = book1[j];
                    book1[j] = book1[j + 1];
                    book1[j + 1] = temp;
                }
            }
        }
        System.out.println("整理完毕!");
    }

    /**
     * 打印图书列表
     * @param book
     */
    @Override
    public void printBooksList(String[][] book) {
        if(Book.count == 0){
            System.out.println("仓库书籍为空!");
        }else {
            System.out.println("所有图书信息如下:");
            for (int i = 0; i < Book.count; i++) {
                System.out.print("图书编号:" + book[i][0] + "\t\t");
                System.out.print("图书名字:" + book[i][1] + "\t\t");
                System.out.print("图书作者:" + book[i][2] + "\t\t");
                System.out.print("图书数量:" + book[i][3] + "\n");
            }
        }
    }

    /**
     * 修改图书
     * @param book1
     * @return
     */
    @Override
    public void alter_s(String[][] book1) {
        String m;
        int o;
        int n = -1;
        System.out.println("请输入您要修改的图书名字:");
        m = input.next();
        for(int  i = 0;i < Book.count;i++){
            if(book1[i][1].equals(m)){
                n = i;
                break;
            }
        }
        System.out.println("请选择您要修改的内容:1. 图书编号  2. 图书名字  3. 图书作者  4.图书数量");
        o = input.nextInt();
        System.out.println("请输入您要修改的内容:");
        book1[n][o-1] = input.next()+"";
        System.out.println("~~~~~~图书信息已修改~~~~~~");
    }

    /**
     * 管理员操作函数
     */
    public void start(){
        boolean flag = true;
        int number;
        while(flag){
            System.out.println("---------------------------------------------------------------------------");
            System.out.print("请选择:1:增加书籍  2:查询书籍  3:修改书籍  4:删除书籍  5:打印书籍列表 6:整理书籍  7: 退出\n\n");
            number = input.nextInt();
            switch (number) {
                case 1:
                    book.add(book);
                    break;
                case 2:
                    selectBook(Book.book1);
                    break;
                case 3:
                    alter_s(Book.book1);
                    break;
                case 4:
                    book.delete();
                    break;
                case 5:
                    printBooksList(Book.book1);
                    break;
                case 6:
                    arrangeBooks(Book.book1);
                    break;
                case 7:
                    System.out.println("~~~~~~您已退出系统~~~~~~");
                    flag = false;
                    break;
                default :
                    System.out.println("输入错误");
                    break;
            }
        }
    }

    @Override
    public int compareTo(Book o) {
        return book.getBookId()-o.getBookId();
    }
}

Book 类

package user.action;


import java.util.Scanner;

public class Book {
    Scanner input = new Scanner(System.in);
    protected int BookId;//书的ID
    protected String BookName;//书的名字
    protected String Author;//书的作者
    protected int BookNum;//书的数量
    protected static int count;

    static String[][] book1 = new String[5][4];//存放图书信息的数组

    public Book(){


    }

    public Book(int BookId,String BookName,String Author,int BookNum){
        this.BookId = BookId;
        this.BookName = BookName;
        this.Author = Author;
        this.BookNum = BookNum;
    }

    public Scanner getInput() {
        return input;
    }

    public void setInput(Scanner input) {
        this.input = input;
    }

    public int getBookId() {
        return BookId;
    }

    public void setBookId(int bookId) { this.BookId = bookId; }

    public String getBookName() {
        return BookName;
    }

    public void setBookName(String bookName) {
        this.BookName = bookName;
    }

    public String getAuthor() {
        return Author;
    }

    public void setAuthor(String author) {
        this.Author = author;
    }

    public int getBookNum() {
        return BookNum;
    }

    public void setBookNum(int bookNum) {
        this.BookNum = bookNum;
    }

    /**
     * 增加图书以及图书信息函数
     * @param book
     */
    public void add(Book book){
        int BookId1,BookNum1;
        String BookName1,Author1;
        System.out.println("~~~~~~开始添加图书信息~~~~~~");
        System.out.println("图书ID:");
        BookId1 = input.nextInt();
        book.setBookId(BookId1);
        System.out.println("图书名字:");
        BookName1 = input.next();
        book.setBookName(BookName1);
        System.out.println("图书作者:");
        Author1 = input.next();
        book.setAuthor(Author1);
        System.out.println("图书数量:");
        BookNum1 = input.nextInt();
        book.setBookNum(BookNum1);
        System.out.println("~~~~~~添加成功~~~~~~");

        book1[count][0] = book.BookId+"";
        book1[count][1] = book.BookName;
        book1[count][2] = book.Author;
        book1[count][3] = book.BookNum+"";
        System.out.println("图书编号:"+book1[count][0]);
        System.out.println("图书名字:"+book1[count][1]);
        System.out.println("图书作者:"+book1[count][2]);
        System.out.println("图书数量:"+book1[count][3]);

        count++;
    }

    /**
     * 用于删除图书信息
     */
    public void delete(){
        System.out.println("输入您要删除的图书名字:");
        String m;
        m = input.next();
        for(int i = 0; i < count;i++) {
            if(book1[i][1].equals(m)) {
                book1[i] = null;
                book1[i] = book1[i + 1];
                break;
            }
        }
        System.out.println("~~~~~~图书信息已被删除~~~~~~");
        count--;
    }
}

  • 管理员登录
    在这里插入图片描述
    以下为管理员操作

  • 管理员添加书籍
    在这里插入图片描述
  • 查询书籍
    在这里插入图片描述
  • 打印书籍列表
    在这里插入图片描述
  • 按编号整理书籍
    在这里插入图片描述
  • 修改图书
    在这里插入图片描述
    修改后
    在这里插入图片描述
  • 删除图书
    在这里插入图片描述
  • 退出
    在这里插入图片描述

以下为用户操作


  • 用户登录
    在这里插入图片描述

  • 查询书籍
    在这里插入图片描述

  • 借阅书籍
    在这里插入图片描述

  • 归还书籍
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xyxy66/article/details/84036733
0条评论
添加一条新回复