java使用面向对象实现图书管理系统

简介: java使用面向对象实现图书管理系统

꒰˃͈꒵˂͈꒱ write in front ꒰˃͈꒵˂͈꒱

ʕ̯•͡˔•̯᷅ʔ大家好,我是xiaoxie.希望你看完之后,有不足之处请多多谅解,让我们一起共同进步૮₍❀ᴗ͈ . ᴗ͈ აxiaoxieʕ̯•͡˔•̯᷅ʔ—CSDN博客

本文由xiaoxieʕ̯•͡˔•̯᷅ʔ 原创 CSDN 如需转载还请通知˶⍤⃝˶

个人主页xiaoxieʕ̯•͡˔•̯᷅ʔ—CSDN博客

系列专栏:xiaoxie的JAVA系列专栏——CSDN博客●'ᴗ'σσணღ*

我的目标:"团团等我💪( ◡̀_◡́ ҂)"

( ⸝⸝⸝›ᴥ‹⸝⸝⸝ )欢迎各位→点赞👍 + 收藏⭐️ + 留言📝+关注(互三必回)!

一.Book类

首先我们需要先创建一个Book类

public class Book {
    private String name;
    private String author;
    private double price;
    private String type;
    private boolean  isBorrowed;
   // 构造函数
    public Book(String name, String author, double 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 String getAuthor() {
        return author;
    }
 
    public void setAuthor(String author) {
        this.author = author;
    }
 
    public double getPrice() {
        return price;
    }
 
    public void setPrice(double price) {
        this.price = price;
    }
 
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public boolean isBorrowed() {
        return isBorrowed;
    }
 
    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }
  // 重写toString方法
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ( (isBorrowed == true) ? ",已借出" : ",未借出" )+
                //", isBorrowed=" + isBorrowed +
                '}';
    }
}

二.BookList类

public class BookList {
    public Book[] List;
    public int size;
    public BookList() {
       List = new Book[10];
       List[0] = new Book("西游记", "吴承恩", 10.25, "小说");
       List[1] = new Book("三国演义","罗贯中",20.50,"小说");
       List[2] = new Book("红楼梦","曹雪芹",50.9,"小说");
       List[3] = new Book("水浒传","施耐庵",15.5,"小说");
 
        size = 4;
    }
    public boolean isFull() {
        return size >= List.length;
    }
    public boolean isEmpty() {
        return size == 0;
    }
    public void newLength() {
        List = new Book[size+10];
    }
 
    public Book getList(int i) {
        return List[i];
    }
 
    public int getSize() {
        return size;
    }
 
    public void setList(Book book , int pos) {
        List[pos] = book;
    }
 
    public void setSize(int size) {
        this.size = size;
    }
}

三.User类

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

四.管理员类

public class Admin extends User{
    public Admin(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DeleteOperation(),
                new ShowOperation()
        };
    }
 
    public int menu() {
        System.out.println("********管理员菜单********");
        System.out.println("1.查找图书");
        System.out.println("2.新增图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        System.out.println("0.退出系统");
        System.out.println("************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

五.普通类

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
       this.iOperations = new IOperation[] {
               new ExitOperation(),
               new  FindOperation(),
               new BorrowedOperation(),
               new ReturnOperation()
       };
    }
    @Override
    public int menu() {
        System.out.println("********普通用户菜单********");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("***************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

六.实现操作的接口

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

七.各种操作类

import java.util.Scanner;
 
// 添加图书的操作
 
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        if(bookList.isFull()) {
            bookList.newLength();
        }
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要添加的书名");
        String name = scan.nextLine();
        System.out.println("请输入你要添加图书的作者");
        String author = scan.nextLine();
        System.out.println("请输入你要添加图书的价格");
        double price = scan.nextDouble();
        scan.nextLine();
        System.out.println("请输入你要添加图书的类型");
        String type = scan.nextLine();
        Book tmp = new Book(name,author,price,type);
        int count = bookList.getSize();
        for (int i = 0; i < count; i++) {
            if(tmp.getName().equals(bookList.getList(i).getName())) {
                System.out.println("请勿重复添加");
                System.exit(0);
            }
        }
        bookList.setList(tmp,count);
        bookList.setSize(count+1);
    }
}
import java.util.Scanner;
 
// 借出书籍
 
public class BorrowedOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        Scanner scan = new Scanner(System.in);
        int count1 = bookList.getSize();
        System.out.println("以下是图书馆的书单: ");
        for (int i = 0; i < count1; i++) {
            if (bookList.getList(i) != null) {
                System.out.println(bookList.getList(i));
            }
        }
        System.out.println("请输入你要借阅的图书名字:");
        String name = scan.nextLine();
        int count = bookList.getSize();
        int i = 0;
        for (; i < count; i++) {
            if (bookList.getList(i).getName().equals(name) ) {
                if(bookList.getList(i).isBorrowed()  ) {
                    System.out.println("书已被借走,请等归还后再来借阅");
                    return;
                }
                bookList.getList(i).setBorrowed(true);
                System.out.println("借阅成功,请于7天时间内归还");
                return;
            }
        }
 
        if(i == count) {
            System.out.println("没有找到这本书");
        }
    }
}
import java.util.Scanner;
 
 
 //删除书籍的操作
 
public class DeleteOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        if(bookList.isEmpty()) {
          throw  new NullException("书架为空");
        }
        Scanner san = new Scanner(System.in);
        System.out.println("请输入你要删除的图书名字");
        String name = san.nextLine();
        int count = bookList.getSize();
        int i = 0;
        for (; i < count; i++) {
            if(bookList.getList(i).getName().equals(name)) {
               break;
            }
        }
        if(i == count) {
            System.out.println("没有找到这本书");
 
        } else {
            while (i < count) {
                bookList.List[i++] = bookList.List[i+1];
            }
            System.out.println("删除成功");
            bookList.setSize(count-1);
        }
    }
}
// 退出操作
 
public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.exit(0);
    }
}
import java.util.Scanner;
 
// 通过图书的名字来查找图书
 
public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要查找的图书名字");
        String name = scan.nextLine();
        int count = bookList.getSize();
        int i = 0;
        for (; i < count; i++) {
            if(bookList.getList(i).getName().equals(name)) {
                System.out.println("图书信息如下:");
                System.out.println(bookList.getList(i));
                break;
            }
        }
        if(i == count) {
            System.out.println("没有找到这本书");
        }
    }
}
//归还操作
public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要归还的图书名字:");
        String name = scan.nextLine();
        int count = bookList.getSize();
        int i = 0;
        for (; i < count; i++) {
            if (bookList.getList(i).getName().equals(name) && bookList.getList(i).isBorrowed()) {
                bookList.getList(i).setBorrowed(false);
                System.out.println("归还成功,欢迎下次光临");
                return;
            }
        }
        if(i == count) {
            System.out.println("没有找到这本书");
        }
    }
}
//显示图书的操作
 
public class ShowOperation implements IOperation{
 
    @Override
    public void work(BookList bookList) {
        int count = bookList.getSize();
        System.out.println("图书信息如下: ");
        for (int i = 0; i < count; i++) {
            if (bookList.getList(i) != null) {
                System.out.println(bookList.getList(i));
            }
        }
    }
}
//异常
public class NullException extends RuntimeException {
    public NullException() {
    }
    public NullException(String message) {
        super(message);
    }
}

八.主函数

public class Main {
    public static User menu() {
        int choice = 0;
        String possWord = "123456";
        System.out.println("请输入你的身份:");
        System.out.println("1.管理员   2.普通用户 ");
        Scanner scan = new Scanner(System.in);
        choice = scan.nextInt();
        scan.nextLine();
        if (choice == 1) {
            System.out.println("请输入密码:");
            int count = 3;
            while (count > 0) {
                String MyPossWord = scan.nextLine();
                if (MyPossWord.equals(possWord)) {
                    count = -1;
                    break;
                } else {
                    --count;
                    System.out.println("密码错误,你还有" + count + "次机会");
                }
            }
            if (count != -1) {
                System.out.println("密码输入错误超过3次,请您24小时后在来");
                System.exit(0);
            }
            return new Admin("admin");
        }
        return new NormalUser("noraluser");
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = Main.menu();
        while (true) {
            int choice = user.menu();
            user.doOperation(choice,bookList);
        }
        }
}

九.说明

以上就是java使用面向对象的知识来实现图书管理系统的全部内容了,此代码仅仅只是对初学Java的读者有帮助,可以通过借鉴此代码,再根据自己所学的知识自己构建一个图书管理系统,这个 图书管理系统也是差不多涵盖了JavaSE所有内容,博主相信你自己下去编写一个图书管理系统,会对Java的掌握更上一步。


相关文章
|
12天前
|
存储 Java
Java——图书管理系统
该文档详细介绍了一个图书管理系统的设计与实现。系统包含普通用户和管理员两种角色,通过书架操作图书,如添加、查找、借阅、归还及删除图书等功能。文档展示了各个功能的具体代码实现,并使用继承和接口等方式优化了系统结构。通过多态技术实现了不同用户角色调用相应功能。整体设计清晰,逻辑严谨,便于理解和实现。
45 17
Java——图书管理系统
|
5天前
|
Java 编译器
封装,继承,多态【Java面向对象知识回顾①】
本文回顾了Java面向对象编程的三大特性:封装、继承和多态。封装通过将数据和方法结合在类中并隐藏实现细节来保护对象状态,继承允许新类扩展现有类的功能,而多态则允许对象在不同情况下表现出不同的行为,这些特性共同提高了代码的复用性、扩展性和灵活性。
封装,继承,多态【Java面向对象知识回顾①】
|
4天前
|
Java
java中面向过程和面向对象区别?
java中面向过程和面向对象区别?
13 4
|
5天前
|
Java
接口和抽象类【Java面向对象知识回顾②】
本文讨论了Java中抽象类和接口的概念与区别。抽象类是不能被实例化的类,可以包含抽象和非抽象方法,常用作其他类的基类。接口是一种纯抽象类型,只包含抽象方法和常量,不能被实例化,且实现接口的类必须实现接口中定义的所有方法。文章还比较了抽象类和接口在实现方式、方法类型、成员变量、构造方法和访问修饰符等方面的不同,并探讨了它们的使用场景。
接口和抽象类【Java面向对象知识回顾②】
|
21天前
|
安全 Java Go
面向对象程序设计语言:Java
Java语言语法和C语言和C++语言很接近,很容易学习和使用,Java丢弃了C++中很少使用的、很难理解的、令人迷惑的特性,Java语言不使用指针,而是引用,并提供了自动分配和回收内存空间,使得程序员不必为内存管理而担忧
34 2
|
2月前
|
Java 数据处理 开发者
【Java基础面试十二】、说一说你对面向对象的理解
这篇文章阐述了面向对象是一种以类和对象为基础,通过封装、继承和多态等概念来模拟现实世界中的事物及其相互关系的程序设计方法,它强调以事物为中心进行思考和系统构造,与结构化程序设计相比,更符合人类的自然思维方式。
【Java基础面试十二】、说一说你对面向对象的理解
|
2月前
|
Java
【Java基础面试十三】、面向对象的三大特征是什么?
这篇文章介绍了面向对象程序设计的三大基本特征:封装、继承和多态,其中封装隐藏对象实现细节,继承实现软件复用,多态允许子类对象表现出不同的行为特征。
【Java基础面试十三】、面向对象的三大特征是什么?
|
9天前
|
Java 开发者
Java 面向对象
Java 是一种面向对象的编程语言,通过对象与类的概念组织代码和数据。面向对象编程的核心包括类、对象、继承、多态、封装和抽象。类是对象的蓝图,定义了属性和行为;对象则是类的实例。继承允许子类继承父类的属性和方法,增强代码复用性;多态则支持通过相同接口调用不同类型对象的行为,包括方法重载和重写。封装通过公共方法隐藏对象细节,提高安全性;抽象则对对象特征进行提炼,通过抽象类和接口实现。理解这些概念有助于设计高效、可维护的 Java 应用程序。
|
16天前
|
Java 开发者
Java编程之旅:探索面向对象的力量
【9月更文挑战第16天】在编程的世界中,Java以其强大的面向对象编程特性而闻名。本文将带你走进Java的世界,一起探索类与对象的奥秘,学习如何通过封装、继承和多态性构建健壮的软件系统。无论你是初学者还是有经验的开发者,本文都旨在提供实用的代码示例,帮助你提升Java技能。准备好开始这段旅程了吗?让我们启程吧!
|
2月前
|
Java 开发者 C++
下一篇
无影云桌面