java idea中做一个简易的图书管理系统(控制台输入输出,无数据库)

2024-01-02 04:00:11

1:先创建增删改查四个接口

(1)QueryBook查询接口

package com.by.dao;

import com.by.entity.Book;

public interface QueryBook {
    /**
     * 查询所有图书信息
     * @param bs
     */
    void selectBook(Book[] bs);

    /**
     * 根据名字查询一个图书信息
     * @param bookName
     * @param bs
     */
    void selectBookByBookname(String bookName,Book[] bs);
}

(2)AddBook 增加接口

package com.by.dao;

import com.by.entity.Book;

public interface AddBook {
   /**
    * 在原图书数组末尾添加一个图书信息
    * @param book   被添加的图书对象
    * @param bs     图书数组
    * @return
    */
   Book [] insertBook(Book book,Book[] bs);
}

(3)RemoveBook删除接口

package com.by.dao;

import com.by.entity.Book;

public interface RemoveBook {
    /**
     * 删除指定图书对象
     * @param bookName  删除的图书名,图书对象
     * @param bs   返回的数组
     * @return
     */
    Book[] deleteBooksByName(String bookName, Book[] bs);
}

(4)UpdateBook修改接口

package com.by.dao;

import com.by.entity.Book;

public interface UpdateBook {
    /**
     * 根据图书名修改图书信息
     * @param bookName 图书名
     * @param bs   数组中内容改变
     */
    void setBookByName(String bookName,Book book,Book []bs);
}

2:创建用户类实现查询接口

package com.by.entity;

import com.by.dao.QueryBook;

import java.util.Scanner;

public class User implements QueryBook {
    private String userName;
    private String password;

    public User() {
    }

    public User(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    @Override
    public void selectBook(Book[] bs) {
        for (int i = 0; i <bs.length; i++) {
            System.out.println("图书名"+bs[i].getBookName()+" , "+"作者名"+bs[i].getBookAuthor()+" , "
                    +"价格"+bs[i].getBookPrice()+" , "+"库存"+bs[i].getBookInventory());
        }
    }

    @Override
    public void selectBookByBookname(String bookName, Book[] bs) {
        for (int i = 0; i <bs.length ; i++) {
            if (bookName.equals(bs[i].getBookName())){
                System.out.println("图书名"+bs[i].getBookName()+" , "+"作者名"+bs[i].getBookAuthor()+" , "
                        +"价格"+bs[i].getBookPrice()+" , "+"库存"+bs[i].getBookInventory());
            }
        }
    }

  /*  Scanner sc=new Scanner(System.in);
    public void login(){
        System.out.println("欢迎使用图书管理系统,请先登录");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name=sc.nextLine();
        if (name.equals(this.userName)){
            System.out.println("请输入密码:");
            String password=sc.nextLine();
            if(password.equals(this.password)){
                System.out.println("登录成功");

            }else{
                System.out.println("用户名或密码有误");
            }
        }else{
            System.out.println("请输入密码:");
            String password1=sc.nextLine();
            System.out.println("用户名或密码错误");
        }*//*else {
            System.out.println("用户名错误");
        }*//*
    }*/


   /* public void selectBook(){
        System.out.println("请输入操作指令");
        int num=sc.nextInt();
        switch (num){
            case 1:
            case 2:



        }
    }*/
}

3:创建普通用户继承用户,管理员账户继承用户类并实现增删改接口

(1)普通用户NormalUser类

package com.by.entity;

import com.by.dao.QueryBook;

import java.util.Scanner;

public class NormalUser extends User {

    public NormalUser() {
    }

    public NormalUser(String userName, String password) {
        super(userName, password);
    }

   /* @Override
    public void login() {
        System.out.println("欢迎使用图书管理系统,请先登录");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name=sc.nextLine();
        if (name.equals(this.getUserName())){
            System.out.println("请输入密码:");
            String password=sc.nextLine();
            if(password.equals(this.getPassword())){
                System.out.println("登录成功");


            }else{
                System.out.println("用户名或密码有误");
            }
        }else{
            System.out.println("请输入密码:");
            String password1=sc.nextLine();
            System.out.println("用户名或密码错误");
        }
    }*/
}

(2)管理员用户AdminUser类

package com.by.entity;

import com.by.dao.AddBook;
import com.by.dao.QueryBook;
import com.by.dao.RemoveBook;
import com.by.dao.UpdateBook;

import java.util.Scanner;

public class AdminUser extends User implements AddBook, UpdateBook, RemoveBook {


    public AdminUser() {
    }

    public AdminUser(String userName, String password) {

        super(userName, password);
    }

    Scanner sc = new Scanner(System.in);

    @Override
    public Book[] insertBook(Book book, Book[] bs) {
        Book[] bs1 = new Book[bs.length + 1];
        for (int i = 0; i < bs.length; i++) {
            bs1[i] = bs[i];
        }
        bs1[bs.length] = book;


        return bs1;
    }

    @Override
    public Book[] deleteBooksByName(String bookName, Book[] bs) {
        Book[] bs2 = new Book[bs.length - 1];

        for (int i = 0; i < bs.length; i++) {
            if (bs[i].getBookName().equals(bookName)) {
                for (int i1 = i; i1 < bs2.length; i1++) {
                    bs2[i] = bs[i + 1];
                }
            } else {
                bs2[i] = bs[i];
            }
        }

        return bs2;
    }

   /* @Override
    public void setBookByName(String bookName,Book[] bs) {
        for (int i=0;i< bs.length;i++){
            if (bookName.equals(bs[i].getBookName())){
                 bs[i]=book;
            }
        }
    }*/

    @Override
    public void setBookByName(String bookName, Book book, Book[] bs) {
        for (int i = 0; i < bs.length; i++) {
            if (bookName.equals(bs[i].getBookName())) {
                bs[i] = book;
            }
        }
        //    @Override
//    public void setBookByName(String bookName, Book[] bs) {
//
//    }


   /* @Override
    public void login() {
        System.out.println("欢迎使用图书管理系统,请先登录");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name=sc.nextLine();
        if (name.equals(this.getUserName())){
            System.out.println("请输入密码:");
            String password=sc.nextLine();
            if(password.equals(this.getPassword())){
                System.out.println("登录成功");
                System.out.println("请输入操作:1:添加图书");
                insertBook();

            }else{
                System.out.println("用户名或密码有误");
            }
        }else{
            System.out.println("请输入密码:");
            String password1=sc.nextLine();
            System.out.println("用户名或密码错误");
        }
    }

    @Override
    public void selectBook() {
        System.out.println("请输入操作指令");
        int num=sc.nextInt();
        switch (num) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
        }
    }*/
    }
}

4:创建图书类存放图书的属性?

package com.by.entity;

public class Book {
    private String bookName;
    private String bookAuthor;
    private double bookPrice;
    private int bookInventory;//库存
    public Book(){}
    public Book(String bookName,String bookAuthor,double bookPrice,int bookInventory){
        this.bookName=bookName;
        this.bookAuthor=bookAuthor;
        this.bookPrice=bookPrice;
        this.bookInventory=bookInventory;
    }

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

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public double getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(double bookPrice) {
        this.bookPrice = bookPrice;
    }

    public int getBookInventory() {
        return bookInventory;
    }

    public void setBookInventory(int bookInventory) {
        this.bookInventory = bookInventory;
    }
}

5:创建测试类测试

package com.by.test;

import com.by.entity.AdminUser;
import com.by.entity.NormalUser;
import com.by.entity.User;
import com.by.entity.Book;

import java.sql.SQLOutput;
import java.util.Scanner;

public class MainTest {
    public static void main(String[] args) {
        User[] users = {new AdminUser("张三", "123"), new NormalUser("李四", "321"),
                new NormalUser("王五", "456"), new NormalUser("haha", "123")};
        Book[] bs = {new Book("西游记", "吴承恩", 49.9, 999),
                new Book("三国演义", "罗贯中", 48.9, 888),
                new Book("水浒传", "施耐庵", 47.9, 777),
                new Book("红楼梦", "曹雪芹", 46.9, 666)};

        User user = login(users);
        fonction(user, bs);


    }

    public static User login(User[] users) {
        boolean result = false;
        System.out.println("欢迎使用图书管理系统,请先登录");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = sc.nextLine();
        System.out.println("请输入密码:");
        String password = sc.nextLine();
        for (int i = 0; i < users.length; i++) {
            if (name.equals(users[i].getUserName()) && password.equals(users[i].getPassword())) {
                System.out.println("登录成功");
                result = true;
                return users[i];
            }
        }
        System.out.println("用户名或密码错误,登录失败!");
        return null;

    }

    public static void fonction(User user, Book[] bs) {
        boolean a=true;
        while(a) {
            System.out.println("请选择你要进行的操作:1,查看所有图书信息  2。查看指定图书信息  " +
                    " 3,添加图书信息   4,修改图书信息    5,删除图书信息    6,退出");
            Scanner scanner = new Scanner(System.in);
            int sum = scanner.nextInt();
            switch (sum) {
                case 1:
                    user.selectBook(bs);
                    break;
                case 2:
                    System.out.println("请输入图书名:");
                    String name = scanner.next();
                    user.selectBookByBookname(name, bs);
                    break;
                case 3:
                    if (isAdmin(user)) {
                        AdminUser adminUser = (AdminUser) user;
                        System.out.println("请输入书名:");
                        String bookName = scanner.next();
                        System.out.println("作者");
                        String author = scanner.next();
                        System.out.println("请输入价格");
                        double price = scanner.nextDouble();
                        System.out.println("请输入库存");
                        int kucun = scanner.nextInt();
                        Book book = new Book(bookName, author, price, kucun);
                        Book[] bs1 = adminUser.insertBook(book, bs);
                        user.selectBook(bs1);
                    } else {
                        System.out.println("对不起,您太拉了,权限不够");
                    }
                    break;
                case 4:
                    if (isAdmin(user)) {
                        System.out.println("请输入你要修改的图书");
                        String byname = scanner.next();
                        AdminUser adminUser = (AdminUser) user;
                        System.out.println("请输入改成的图书名");
                        String gaiName=scanner.next();
                        System.out.println("请输入作者");
                        String author=scanner.next();
                        System.out.println("请输入价格");
                        double price=scanner.nextDouble();
                        System.out.println("请输入库存");
                        int kucun=scanner.nextInt();
                        Book book=new Book(gaiName,author,price,kucun);
                        adminUser.setBookByName(byname, book,bs);
                        user.selectBook(bs);
                    } else {
                        System.out.println("对不起,您太拉了,权限不够");
                    }
                    break;
                case 5:
                    if (isAdmin(user)) {
                        System.out.println("请输入要删除的图书");
                        String deleteName = scanner.next();
                        AdminUser adminUser = (AdminUser) user;
                        Book[] bs2=adminUser.deleteBooksByName(deleteName, bs);
                        user.selectBook(bs2);
                        break;
                    }
                case 6:
                    System.out.println("欢饮下次使用!");
                    a=false;
                    break;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }

    }

    public static boolean isAdmin(User user) {
        if (user instanceof AdminUser) {
            return true;
        } else {
            return false;
        }
    }


}

?

文章来源:https://blog.csdn.net/zhzjn/article/details/135330699
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。