接口实现 图书管理器(模拟)

18 篇文章 0 订阅
12 篇文章 0 订阅

直接放代码

+package User;
+
+import operation.*;
+
+import java.util.Scanner;
+
+//管理用户
+public class AdminUser extends User {
+    public AdminUser(String name) {
+        super(name);
+//实现接口数组IOPeration
+        this.ioPerations=new IOPeration[] {
+                new ExitOperation(),
+                new FindOperation(),
+                new AddOperation(),
+                new DeleteOperation(),
+                new DisplayOperation()
+
+
+        };
+
+    }
+
+    public int menu() {
+        System.out.println("\n");
+        System.out.println("您好管理员[" + this.name + "]欢迎来到图书管理系统");
+        System.out.println("1.《查找书籍》");
+        System.out.println("2.《新增书籍》");
+        System.out.println("3.《删除书籍》");
+        System.out.println("4.《显示书籍》");
+        System.out.println("0.《退出系统》");
+        System.out.println("\n");
+        System.out.print("选择:");
+        Scanner sca=new Scanner(System.in);
+        int choice=sca.nextInt();
+        System.out.println("\n");
+        return choice;
+    }
+}
diff --git a/src/User/NormaIUser.java b/src/User/NormaIUser.java
new file mode 100644
index 0000000..3c578b2
--- /dev/null
+++ b/src/User/NormaIUser.java
@@ -0,0 +1,40 @@
+package User;
+
+import operation.*;
+
+import java.util.Scanner;
+
+//普通用户
+public class NormaIUser extends User{
+
+    public NormaIUser(String name) {
+        super(name);
+        //实现接口数组IOPeration
+        this.ioPerations=new IOPeration[] {
+                new ExitOperation(),
+                new FindOperation(),
+                new BorrowOperation(),
+                new ReturnOperation()
+
+
+        };
+    }
+
+    public int menu(){
+        System.out.println("\n");
+        System.out.println("尊敬的用户["+this.name+"]欢迎来到图书借阅系统");
+        System.out.println("1.《查找书籍》");
+        System.out.println("2.《借阅书籍》");
+        System.out.println("3.《归还书籍》");
+        System.out.println("0.《退出系统》");
+        System.out.println("\n");
+        System.out.print("选择:");
+        Scanner sca=new Scanner(System.in);
+        int choice=sca.nextInt();
+
+        System.out.println("\n");
+        return choice;
+
+    }
+}
+
diff --git a/src/User/User.java b/src/User/User.java
new file mode 100644
index 0000000..2d933b8
--- /dev/null
+++ b/src/User/User.java
@@ -0,0 +1,29 @@
+package User;
+
+import book.BookList;
+
+import operation.IOPeration;
+
+public abstract class User {
+
+    protected String name;
+
+
+/对子类的动态绑定name方法
+
+
+    public IOPeration[] ioPerations;
+
+
+    public User(String name ){
+        this.name=name;
+
+    }
+
+    public abstract int menu();
+
+public void doOperation(int choice,BookList bookList){
+    ioPerations[choice].work(bookList);
+}
+
+}
diff --git a/src/User/UserExit.java b/src/User/UserExit.java
new file mode 100644
index 0000000..6370574
--- /dev/null
+++ b/src/User/UserExit.java
@@ -0,0 +1,25 @@
+package User;
+
+import operation.*;
+
+import java.util.Scanner;
+
+//普通用户
+public class UserExit extends User{
+
+    public UserExit(String name) {
+        super(name);
+        //实现接口数组IOPeration
+        this.ioPerations=new IOPeration[] {
+                new ExitOperation()
+        };
+    }
+
+    public int menu(){
+
+        System.out.println("[输入有误]");
+        System.out.println(" ");
+        int choice=0;
+        return choice;
+    }
+}
\ No newline at end of file
diff --git a/src/book/Book.java b/src/book/Book.java
new file mode 100644
index 0000000..20eba0d
--- /dev/null
+++ b/src/book/Book.java
@@ -0,0 +1,79 @@
+package book;
+
+public class Book {
+  private String name;//书名
+
+  private String author;//作者
+
+  private int price;//价格
+
+private String type;//类型
+
+private boolean isBOrr;//判断是否被借阅
+
+    ///   //book的构造方法
+    public Book(String name, String author, int price, String type) {
+        this.name = name;
+        this.author = author;
+        this.price = price;
+        this.type = type;
+    }
+    ///  //提供get和set
+    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(int price) {
+        this.price = price;
+    }
+///
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+///
+
+    public boolean isBOrr() {
+        return isBOrr;
+    }
+
+    public void setBOrr(boolean BOrr) {
+        isBOrr = BOrr;
+    }
+
+    ///  //提供toString打印Book
+    @Override
+    public String toString() {
+        return "| 库存:("+
+                "名称《" + name + "》"+
+                " 作者[" + author + "]"+
+                " 价格[" + price +"]"+
+                " 类别[" + type + "]"+
+                ((isBOrr==true)?"  已借出":"  未借出") +
+              // " 状态'=" + isBOrr +
+                ") |";
+    }
+
+}
diff --git a/src/book/BookList.java b/src/book/BookList.java
new file mode 100644
index 0000000..c497095
--- /dev/null
+++ b/src/book/BookList.java
@@ -0,0 +1,34 @@
+package book;
+
+public class BookList {
+    /
+private Book[] books=new Book[10];//构思一个书架数组可放十本书籍
+
+private int usedSize;
+/ //数组中放了几本书
+    public BookList(){
+        books[0]=new Book("斗罗大陆","唐家三少",58,"小说");
+
+        books[1]=new Book("斗破苍穹","天蚕土豆",58,"小说");
+
+        books[2]=new Book("盗墓笔记","南派三叔",58,"小说");
+        this.usedSize=3;
+    }
+    /
+    public int getUsedSize() { //给usedSize提供get获取数组当中元素的个数
+        return usedSize;
+    }
+
+    public void setUsedSize(int usedSize) {//给usedSize提供set设置数组当中元素的个数
+        this.usedSize = usedSize;
+    }
+
+/
+    public Book getPos(int pos){ //获取books书的下标
+        return books [pos];
+    }
+
+    public void setBooks(int pos,Book book){ //给books的[pos]下标放置书
+        books[pos]=book;
+    }
+}
diff --git a/src/main.java b/src/main.java
new file mode 100644
index 0000000..1faecc4
--- /dev/null
+++ b/src/main.java
@@ -0,0 +1,48 @@
+import User.AdminUser;
+import User.NormaIUser;
+import User.UserExit;
+import User.User;
+import book.BookList;
+
+
+
+
+import java.util.Scanner;
+
+public class main {
+
+    public static User login() {
+        System.out.print("请输入您的姓名 :");
+        Scanner scan = new Scanner(System.in);
+        String name = scan.nextLine();
+        System.out.println("\n");
+        System.out.println("请输入你的身份:[(1)管理员] 或 [(2)用户]");
+        int choice = scan.nextInt();
+        if (choice>=0&&choice<=3) {
+            if (choice == 1) {
+                return new AdminUser(name);
+            } else if (choice==2){
+                return new NormaIUser(name);
+
+            }
+
+        }
+
+       return new UserExit(name);
+
+    }
+
+    public static void main(String[] args) { //User引用了那个对象
+
+        BookList bookList=new BookList();
+  User user=  login();
+
+  while (true) {
+      int choice = user.menu();
+      user.doOperation(choice,bookList);
+
+
+  }
+    }
+
+}
diff --git a/src/operation/AddOperation.java b/src/operation/AddOperation.java
new file mode 100644
index 0000000..208a539
--- /dev/null
+++ b/src/operation/AddOperation.java
@@ -0,0 +1,37 @@
+package operation;
+
+
+import book.Book;
+import book.BookList;
+
+import java.util.Scanner;
+
+public class AddOperation implements IOPeration {//定义一个Add添加图书
+
+
+    @Override
+    public void work(BookList bookList){
+        System.out.println("[新增书籍]");
+
+        System.out.print("请输入书籍名称:");
+        Scanner scanner=new Scanner(System.in);
+        String name=scanner.nextLine();
+        System.out.print("请输入书籍作者:");
+        String author=scanner.nextLine();
+        System.out.print("请输入书籍类型:");
+        String type=scanner.nextLine();
+        System.out.print("请输入书籍价格:");
+        int price=scanner.nextInt();
+
+
+        Book book=new Book(name,author,price,type);
+
+        int crSize=bookList.getUsedSize();
+        bookList.setBooks(crSize,book);
+
+        bookList.setUsedSize(crSize+1);//调用BookList让数组+1
+
+        System.out.println("\n[添加成功!]");
+    }
+
+}
diff --git a/src/operation/BorrowOperation.java b/src/operation/BorrowOperation.java
new file mode 100644
index 0000000..1339559
--- /dev/null
+++ b/src/operation/BorrowOperation.java
@@ -0,0 +1,29 @@
+package operation;
+
+import book.Book;
+import book.BookList;
+
+import java.util.Scanner;
+
+public class BorrowOperation implements IOPeration {
+    public void work(BookList bookList){
+        System.out.println("[借阅图书]");
+
+        System.out.print("请输入书籍名称:");
+        Scanner scanner=new Scanner(System.in);
+        String name=scanner.nextLine();
+        int crSize=bookList.getUsedSize();
+        for (int i=0;i<crSize;i++) {
+            Book book = bookList.getPos(i);
+            if (book.getName().equals(name)){
+
+                book.setBOrr(true);
+                System.out.println("\n\n");
+                System.out.println("[借阅成功]");
+                return;
+            }
+        }
+        System.out.println("\n\n");
+        System.out.println("[借阅失败]:没有这本书籍!");
+    }
+}
diff --git a/src/operation/DeleteOperation.java b/src/operation/DeleteOperation.java
new file mode 100644
index 0000000..80df862
--- /dev/null
+++ b/src/operation/DeleteOperation.java
@@ -0,0 +1,40 @@
+package operation;
+
+import book.Book;
+import book.BookList;
+
+import java.util.Scanner;
+
+public class DeleteOperation implements IOPeration{
+    public void work(BookList bookList){
+        System.out.println("[删除图书]");
+        System.out.print("请输入书籍名称:");
+        Scanner scanner=new Scanner(System.in);
+        String name=scanner.nextLine();
+        int idx=0;
+        int crSize=bookList.getUsedSize();
+        int i=0;
+        for (i=0;i<crSize;i++){
+            Book book=bookList.getPos(i);
+            if (book.getName().equals(name)){
+
+                idx=i;
+                break;
+            }
+        }
+        if (i==crSize){
+            System.out.println("\n\n");
+        System.out.println("[删除失败]:没有这本书籍!");
+        return ;
+    }
+        for (int j=idx;j<crSize-1;j++){
+           Book book= bookList.getPos(j+1);
+           bookList.setBooks(j,book);
+        }
+        bookList.setBooks(crSize-1,null);
+
+        bookList.setUsedSize(crSize-1);
+        System.out.println("\n\n");
+        System.out.println("[删除成功]");
+    }
+}
diff --git a/src/operation/DisplayOperation.java b/src/operation/DisplayOperation.java
new file mode 100644
index 0000000..d9bc46c
--- /dev/null
+++ b/src/operation/DisplayOperation.java
@@ -0,0 +1,21 @@
+package operation;
+
+import book.Book;
+import book.BookList;
+
+public class DisplayOperation implements IOPeration {
+    public void work(BookList bookList){
+        System.out.println("[显示成功]");
+        System.out.println(" --------------------------------------------------------------");
+        int crSize=bookList.getUsedSize();
+        for (int i=0;i<crSize;i++){
+            Book book=bookList.getPos(i);
+
+            System.out.println(book);
+
+        }
+        System.out.println(" --------------------------------------------------------------");
+        System.out.println("\n");
+    }
+
+}
diff --git a/src/operation/ExitOperation.java b/src/operation/ExitOperation.java
new file mode 100644
index 0000000..a18bfcf
--- /dev/null
+++ b/src/operation/ExitOperation.java
@@ -0,0 +1,15 @@
+package operation;
+
+import book.Book;
+import book.BookList;
+
+public class ExitOperation implements IOPeration {
+    public void work(BookList bookList){
+        System.out.println("[退出系统]");
+        for (int i=0;i<bookList.getUsedSize();i++){
+           bookList.setBooks(i,null);
+
+        }
+        System.exit(0);
+    }
+}
diff --git a/src/operation/FindOperation.java b/src/operation/FindOperation.java
new file mode 100644
index 0000000..afe97f9
--- /dev/null
+++ b/src/operation/FindOperation.java
@@ -0,0 +1,29 @@
+package operation;
+
+import book.Book;
+import book.BookList;
+
+import java.util.Scanner;
+
+public class FindOperation implements IOPeration {
+    public void work(BookList bookList){
+        System.out.println("[查找图书]");
+
+        System.out.print("请输入书籍名称:");
+        Scanner scanner=new Scanner(System.in);
+        String name=scanner.nextLine();
+        int crSize=bookList.getUsedSize();
+        for (int i=0;i<crSize;i++){
+            Book book=bookList.getPos(i);
+if (book.getName().equals(name)){
+    System.out.println("\n\n");
+    System.out.println("[查找成功]:"+book);
+    return;
+}
+
+
+        }
+        System.out.println("\n\n");
+        System.out.println("[查找失败]:没有这本书籍!");
+    }
+}
diff --git a/src/operation/IOPeration.java b/src/operation/IOPeration.java
new file mode 100644
index 0000000..af6f8c2
--- /dev/null
+++ b/src/operation/IOPeration.java
@@ -0,0 +1,9 @@
+package operation;
+
+import book.BookList;
+//实现接口IO链接所有方法
+public interface IOPeration {
+
+    void work(BookList bookList);
+
+}
diff --git a/src/operation/ReturnOperation.java b/src/operation/ReturnOperation.java
new file mode 100644
index 0000000..188fad3
--- /dev/null
+++ b/src/operation/ReturnOperation.java
@@ -0,0 +1,28 @@
+package operation;
+
+import book.Book;
+import book.BookList;
+
+import java.util.Scanner;
+
+public class ReturnOperation implements IOPeration {
+    public void work(BookList bookList){
+        System.out.println("[归还图书]");
+        System.out.print("请输入书籍名称:");
+        Scanner scanner=new Scanner(System.in);
+        String name=scanner.nextLine();
+        int crSize=bookList.getUsedSize();
+        for (int i=0;i<crSize;i++) {
+            Book book = bookList.getPos(i);
+            if (book.getName().equals(name)){
+
+                book.setBOrr(false);
+                System.out.println("\n\n");
+                System.out.println("[归还成功]");
+                return;
+            }
+        }
+        System.out.println("\n\n");
+        System.out.println("[归还失败]:没有这本书籍!");
+    }
+}

 部分实现效果

 

 

gitee: https://gitee.com/lin-xiantao/elementary-exercises-with-code.git //链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值