作者主页:paper jie 的博客
本文作者:大家好,我是paper jie,感谢你阅读本文,欢迎一建三连哦。
本文录入于《JAVA》专栏,本专栏是针对于大学生,编程小白精心打造的。笔者用重金(时间和精力)打造,将javaSE基础知识一网打尽,希望可以帮到读者们哦。
其他专栏:《算法详解》《C语言》《javaSE》等
内容分享:本期将会使用javase来制作一个简易版本的图书管理系统
目录
我们需要通过使用javase来实现一个简易版本的图书管理系统,它需要具备以下几个功能:
1 首先需要有两个模式,一个是管理员模式,一个是普通用户模式
2 需要具有 新增图书,删除图书,查找图书,显示图书,借阅图书,归还图书,退出系统这些功能
3.需要将上面的功能分成两部分,新增,删除,显示,查找,退出为管理员的权限,而查找,借阅,归还,退出为普通用户的权限
1 我们首先需要将我们的用户分为两个部分,一个为管理员,一个为普通用户,在使用的时候根据不同的需求展现不同的界面和功能,这里我们可以通过继承,多态,向上转型给他们一个父类来实现
2 我们需要实现多个类来实现删除图书,查找图书,显示图书,借阅图书,归还图书,退出系统这些功能
3 我们通过创建一个接口,让功能类来实现这个接口,再用一个类型为创建的接口类型的数组来存放功能类的new出的对象,这样子就可以进行统一管理了
4 我们还需要创建书和书架类,将书的属性封装,书架类存放的就是书,会用一个书架类型的数组来存放
为了让程序去复杂化,易修改这里将用三个包和一个主类来搭建这个框架会有book包,里面存放书类和书架类。operation包存放各个功能类和接口。user包中放着父类User类和普通用户类,管理员类,最后还有一个主类Main
book类起到的作用就是将book类封装起来,后面容易放入书架中。 这里的构造方法,getandset方法以及toString方法可以通过编译器生成,速度很快的。在toString方法中我们需要改动一下编译器生成的,我们可以用三目操作符来确定是true还是false来打印是已借出还是未接出
- package book;
-
-
- public class Book {
- private String name;
- private String author;
- private int price;
- private String type;
- //默认flose 未被借出
- private boolean isBorrowed;
- //get 和 set方法设置和返回成员变量
- public Book(String name, String author, int 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 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 isBorrowed() {
- return isBorrowed;
- }
-
- public void setBorrowed(boolean borrowed) {
- isBorrowed = borrowed;
- }
- //打印
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", price=" + price +
- ", type='" + type + '\'' +
- ", " + ((isBorrowed == true) ? "已经借出" : "未接出") +
- '}';
- }
- }
这里也可以使用编译器生成需要的代码,这里要注意的是books数组,它是用来存放book书本的。在构造方法中要对它进行初始化,可以存放几本,已进放进去的书的内容。
- package book;
-
-
- public class Booklist {
- //书架
- private Book[] books;
- //记录使用书的位置
- private int usedsize;
- private static final int DEFAULT_CAPACITY = 10;
- public Booklist() {
- //对书架初始化
- this.books = new Book[DEFAULT_CAPACITY];
- //放书
- this.books[0] = new Book("三国演义", "罗贯中", 10, "小说");
- this.books[1] = new Book("西游记", "吴承恩", 20, "小说");
- this.books[2] = new Book("红楼梦", "曹雪芹", 30, "小说");
- this.usedsize = 3;
- }
-
- public int getUsedsize() {
- return usedsize;
- }
-
- public void setUsedsize(int usedsize) {
- this.usedsize = usedsize;
- }
-
- public Book getBook(int pos) {
- return books[pos];
- }
-
- public void setBook(int pos, Book book) {
- books[pos] = book;
- }
-
- public Book[] getBooks() {
- return books;
- }
- }
这个包里放的是一个父类和两个子类,企图通过继承和多态来实现管理员和普通用户的切换
这里要注意的是IOoperation这个数组,它会在User类的子类中进行初始化,来存放需要功能类的对象。而dooperation方法则是通过IOoperations数组里的成员的work方法来实现需要的操作
- package user;
-
- import book.Booklist;
- import operation.IOoperation;
-
-
- public abstract class User {
- protected String name;
- protected IOoperation[] iOoperations;
-
- public User(String name) {
- this.name = name;
- }
- public abstract int menu();
-
- public void dooperation(int choise, Booklist booklist) {
- iOoperations[choise].work(booklist);
- }
- }
它和NormalUser都是User的子类,这里面的构造方法就对父类的成员进行了IOoperation初始化,且实现的菜单界面,menu方法的返回值就是你选择需要使用的功能
- package user;
-
- import operation.*;
-
- import java.util.Scanner;
-
-
- public class AdminUser extends User{
-
- public AdminUser(String name) {
- super(name);
- this.iOoperations = new IOoperation[]{new Exitoperation(),new Addopertaion(),
- new Deloperation(), new Showoperation(),new Findoperation()};
- }
-
- 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;
- }
- }
实现思路和AdminUser一样
- package user;
-
- import operation.*;
-
- import java.util.Scanner;
-
-
- public class NormalUser extends User {
-
- public NormalUser(String name) {
- super(name);
- this.iOoperations = new IOoperation[]{new Exitoperation(),new Findoperation(),
- new Borrowoperation(), new Returnoperation()};
- }
-
- 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;
- }
- }
这个包里需要实现需要的功能类和一个将他们都串起来的接口
所有的功能类都要实现这个接口,从而重写work方法,来统一使用work这个名字
- package operation;
-
- import book.Booklist;
-
-
- public interface IOoperation {
- public void work(Booklist booklist);
- }
新增图书功能:
- package operation;
-
- import book.Book;
- import book.Booklist;
-
- import java.util.Scanner;
-
- /**
- * Created with IntelliJ IDEA.
- * Description:
- * User: sun杰
- * Date: 2023-08-25
- * Time: 10:43
- */
- public class Addopertaion implements IOoperation{
- @Override
- public void work(Booklist booklist) {
- System.out.println("新增图书:");
- Scanner scanner = new Scanner(System.in);
-
- System.out.println("请输入名字:");
- String name = scanner.nextLine();
-
- System.out.println("请输入作者:");
- String author = scanner.nextLine();
-
- System.out.println("请输入类型:");
- String type = scanner.nextLine();
-
- System.out.println("请输入价格:");
- int price = scanner.nextInt();
-
- Book book = new Book(name, author, price, type);
- int pos = booklist.getUsedsize();
- for (int i = 0; i < pos; i++) {
- Book book1 = booklist.getBook(i);
- if(book1.getName().equals(name)) {
- System.out.println("已有这本书,不再存放");
- return;
- }
- }
- if(pos == booklist.getBooks().length) {
- System.out.println("书架满了");
- }else {
- booklist.setBook(pos, book);
- booklist.setUsedsize(pos + 1);
- }
- }
- }
删除图书功能:
- import book.Booklist;
-
- import java.util.Scanner;
-
-
- public class Deloperation implements IOoperation{
- @Override
- public void work(Booklist booklist) {
- System.out.println("删除图书:");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- int size = booklist.getUsedsize();
- for (int i = 0; i < size; i++) {
- Book book = booklist.getBook(i);
- if(book.getName().equals(name)) {
- int pos = i;
- int j = 0;
- for ( j = pos; j < size - 1; j++) {
- Book book1 = booklist.getBook(j+1);
- booklist.setBook(j, book1);
- }
- booklist.setBook(j, null);
- booklist.setUsedsize(size - 1);
- System.out.println("删除成功");
- return;
- }
- }
- System.out.println("不存在这本书");
-
- }
- }
借阅图书功能:
- package operation;
-
- import book.Book;
- import book.Booklist;
-
- import java.util.Scanner;
-
- public class Borrowoperation implements IOoperation {
- @Override
- public void work(Booklist booklist) {
- System.out.println("借阅图书:");
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入你要借阅的图书:");
- String name = scanner.nextLine();
- int size = booklist.getUsedsize();
- for (int i = 0; i < size; i++) {
- Book book = booklist.getBook(i);
- if(book.getName().equals(name)) {
- book.setBorrowed(true);
- System.out.println(book);
- System.out.println("借阅成功");
- return;
- }
- }
- System.out.println("该书不存在");
- }
- }
退出系统功能:
- package operation;
-
- import book.Booklist;
-
-
- public class Exitoperation implements IOoperation{
- @Override
- public void work(Booklist booklist) {
- System.out.println("退出系统");
- System.exit(0);
- }
- }
查找图书功能:
- package operation;
-
- import book.Book;
- import book.Booklist;
- import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
-
- import javax.imageio.metadata.IIOMetadataController;
- import java.util.Scanner;
-
-
- public class Findoperation implements IOoperation{
- @Override
- public void work(Booklist booklist) {
- System.out.println("查找图书:");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- int size = booklist.getUsedsize();
- for (int i = 0; i < size; i++) {
- Book book = booklist.getBook(i);
- if(book.getName().equals(name)) {
- System.out.println("找到了,信息如下:");
- System.out.println(book);
- return;
- }
-
- }
- System.out.println("上述不存在");
- }
- }
归还图书功能:
- package operation;
-
- import book.Book;
- import book.Booklist;
-
- import java.util.Scanner;
-
-
- public class Returnoperation implements IOoperation{
- @Override
- public void work(Booklist booklist) {
- System.out.println("归还图书:");
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入你要归还的图书:");
- String name = scanner.nextLine();
- int size = booklist.getUsedsize();
- for (int i = 0; i < size; i++) {
- Book book = booklist.getBook(i);
- if(book.getName().equals(name)) {
- book.setBorrowed(false);
- System.out.println(book);
- System.out.println("归还成功");
- return;
- }
- }
- System.out.println("该书不存在");
- }
- }
显示图书功能:
- package operation;
-
- import book.Book;
- import book.Booklist;
-
-
- public class Showoperation implements IOoperation {
- @Override
- public void work(Booklist booklist) {
- System.out.println("显示图书:");
- int size = booklist.getUsedsize();
- for (int i = 0; i < size; i++) {
- Book book = booklist.getBook(i);
- System.out.println(book);
- }
- }
- }
通过Main类来组织程序执行:
- import book.Book;
- import book.Booklist;
- import user.AdminUser;
- import user.NormalUser;
- import user.User;
-
- import java.util.Scanner;
-
-
- public class Main {
- public static User login() {
- System.out.println("请输入你的姓名:");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- System.out.println("请输入你的身份:1.管理员 2.普通用户");
- int choice = scanner.nextInt();
- if(choice == 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();
- //choice来决定调用哪个方法类
- user.dooperation(choice, booklist);
- }
-
- }
- }