图书管理系统|<Java> 图书管理系统. 课程设计之经典(学不会给我发红包)

目录

Package
(书-包)book
(书-类)Book
(书架-类)BookList
(操作-包)operation
(操作接口)IOperation
(添加图书)AddOperation
(借阅图书)BorrowOperation
(删除图书)DelOperation
(显示图书)DisplayOperation
(退出系统)ExitOperation
【图书管理系统|<Java> 图书管理系统. 课程设计之经典(学不会给我发红包)】(查找图书)FindOperation
(归还图书)ReturnOperation
(用户-包)user
(用户父类)User
(管理员)AdminUser
(普通用户)NormalUser
(开始函数)Main
Package
(书-包)book (书-类)Book

package book; /** * Created with IntelliJ IDEA. * Description:书 * User: 86155 * Date: 2022-05-18 * Time: 10:12 */ public class Book { private String name; //书名 private String author; //作者 private int price; //价格 private String type; //类型 private boolean isBorrowed; //是否被借出//编译器生成方法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)?", 已借出":", 未借出") + '}'; } }

(书架-类)BookList
package book; /** * Created with IntelliJ IDEA. * Description:书架 * User: 86155 * Date: 2022-05-18 * Time: 10:12 */ public class BookList { private Book[] books = new Book[10]; //书架最多可以放10本书 private int usedSize; //记录书架上放了几本书//构造函数 public BookList(){ //书架上先放几本书 books[0] = new Book("三国演义","罗贯中",19,"小说"); books[1] = new Book("水浒传","施耐庵",29,"小说"); books[2] = new Book("西游记","吴承恩",23,"小说"); usedSize = 3; } //get set 公开接口 //给到下标可以从书架上得到书的信息 public Book getBook(int pos){//pos位置一定是合法的 return books[pos]; } //往书架上放书 public void setBooks(int pos,Book book){ books[pos] = book; }public int getUsedSize() { return usedSize; }public void setUsedSize(int usedSize) { this.usedSize = usedSize; } }

(操作-包)operation (操作接口)IOperation
package operation; import book.Book; import book.BookList; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 10:55 */ public interface IOperation { //通过一个规范的接口来访问操作我的书架 void work(BookList bookList); }

(添加图书)AddOperation
package operation; import book.Book; import book.BookList; import java.util.Scanner; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 10:57 */ public class AddOperation implements IOperation{ @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("请输入图书的价钱:"); int price = scanner.nextInt(); scanner.nextLine(); System.out.println("请输入图书的类型:"); String type = scanner.nextLine(); Book book = new Book(name,author,price,type); //把它放到书架上 int currentSize = bookList.getUsedSize(); bookList.setBooks(currentSize,book); bookList.setUsedSize(currentSize+1); System.out.println("新增书籍成功!"); } }

(借阅图书)BorrowOperation
package operation; import book.Book; import book.BookList; import java.util.Scanner; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 11:03 */ public class BorrowOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("借阅图书!"); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要借阅图书的名字:"); String name = scanner.nextLine(); //遍历书架 int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)){ book.setBorrowed(true); System.out.println("借阅成功!"); return; } } System.out.println("没有你要找的书"); } }

(删除图书)DelOperation
package operation; import book.Book; import book.BookList; import java.util.Scanner; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 10:59 */ public class DelOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("删除图书!"); System.out.println("请输入你要输出图书的名字:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); //遍历书架 int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)){ for (int j = i; j < currentSize; j++) { bookList.setBooks(j,bookList.getBook(j+1)); } bookList.setUsedSize(currentSize-1); System.out.println("删除成功!"); return; } } System.out.println("没有你要找的书"); } }

(显示图书)DisplayOperation
package operation; import book.Book; import book.BookList; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 11:00 */ public class DisplayOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("显示图书!"); //遍历书架 int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { System.out.println(bookList.getBook(i)); } } }

(退出系统)ExitOperation
package operation; import book.BookList; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 11:02 */ public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出系统!"); System.exit(0); } }

(查找图书)FindOperation
package operation; import book.Book; import book.BookList; import java.util.Scanner; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 10:58 */ public class FindOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("查找图书!"); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要查找图书的名字:"); String name = scanner.nextLine(); //遍历书架 int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)){ System.out.println("找到了这本书!"); System.out.println(book); return; } } System.out.println("没有你要找的书"); } }

(归还图书)ReturnOperation
package operation; import book.Book; import book.BookList; import java.util.Scanner; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 11:04 */ public class ReturnOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("归还图书!"); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要归还图书的名字:"); String name = scanner.nextLine(); //遍历书架 int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)){ book.setBorrowed(false); System.out.println("归还成功!"); return; } } System.out.println("没有你要找的书"); } }

(用户-包)user (用户父类)User
package user; import book.BookList; import operation.IOperation; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 11:08 */ //不管是普通人员还是管理人员,其父类都是User public abstract class User { protected String name; //用户名 protected IOperation[] iOperations; //接口数组 - 菜单选择的操作public User(String name){ this.name = name; } public abstract int menu(); //用来访问接口数组的哪个函数来操作书架 public void doOperation(int choice , BookList bookList){ this.iOperations[choice].work(bookList); //操作数选择了几,就调用这个函数的work方法 } }

(管理员)AdminUser
package user; import operation.*; import java.util.Scanner; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 11:12 */ public class AdminUser extends User{ //显示的帮助父类构造 public AdminUser(String name){ super(name); this.iOperations = new IOperation[]{ new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation() }; } //菜单 public int menu(){ 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("请输入你的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }

(普通用户)NormalUser
package user; import operation.*; import java.util.Scanner; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 11:11 */ public class NormalUser extends User{ public NormalUser(String name){ super(name); this.iOperations = new IOperation[]{ new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } //菜单 public int menu(){ System.out.println("欢迎"+this.name+"来到图书馆!"); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.归还图书"); System.out.println("0.退出系统"); System.out.println("请输入你的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }

(开始函数)Main
import book.BookList; import user.AdminUser; import user.NormalUser; import user.User; import javax.jws.soap.SOAPBinding; import java.util.Scanner; /** * Created with IntelliJ IDEA. * Description: * User: 86155 * Date: 2022-05-18 * Time: 11:32 */ public class Main { public static User login(){ System.out.println("请输入你的用户名:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); System.out.println("请输入你的身份:1:管理员0:普通用户"); int choice = scanner.nextInt(); if(choice == 1){ return new AdminUser(name); }else{ return new NormalUser(name); } } //开始整合 public static void main(String[] args) { //1.准备图书 BookList bookList = new BookList(); //2.登录 User user = login(); while(true){ //3.根据登录的身份打印菜单 多态 int choice = user.menu(); user.doOperation(choice,bookList); } } }


    推荐阅读