编辑
前言
大家好,我还是那个不会打拳的程序猿。在我们掌握大量的知识后如果不实现出一些小程序实属可惜,因此今天我分享给大家一个图书管理系统小程序。此练习涉及到数据类型、数组、方法、封装、继承、多态、接口、抽象类等知识,效果展示以及源码放在下方了,快去看看吧~
目录
编辑
1.效果及源码展示
效果展示:
编辑
编辑
编辑
编辑
编辑编辑
编辑
编辑
以上代码,只是管理员身份进行操作。当然你也可以进行测试用户操作,以下为源码展示:
编辑
(1)Book.java文件
//Book.java文件下 package Book; //书的属性 public class Book { //书名 private String bookName; //作者 private String author; //价钱 private int price; //类型 private String type; //是否被借出 private boolean isLend; public Book(String bookName, String author, int price, String type) { this.bookName = bookName; this.author = author; this.price = price; this.type = type; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } 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 void setLend(boolean lend) { isLend = lend; } @Override public String toString() { return "Book{" + "bookName='" + bookName + '\'' + ",author='" + author + '\'' + ",price="+ price + ",type='" + type + '\'' + ",isLend=" +(isLend == true ? "已被借出":"未被借出")+ '}'; } }
(2)BookList.java文件下
//BookList.java文件下 package Book; //书架 public class BookList { //书架容纳数量 protected static final int DEFAULE_SIZE = 10; //书架数组 private Book[] books = new Book[DEFAULE_SIZE]; protected int useSize; public BookList() { books[0] = new Book("三国演义","罗贯中",25,"小说"); books[1] = new Book("朝花夕拾","鲁迅",26,"小说"); books[2] = new Book("阿Q正传","鲁迅",24,"小说"); this.useSize = 3; } public Book getBook (int i) { return this.books[i]; } public void setBooks(Book book) { this.books[useSize] = book; } public void setBooks(int pos,Book book) { this.books[pos] = book; } public int getUseSize() { return useSize; } public void setUseSize(int useSize) { this.useSize = useSize; } }
(3)AddBook.java文件下
//AddBook.java文件下 package BookOperation; import Book.Book; import Book.BookList; import java.util.Scanner; public class AddBook implements IOperation{ @Override public void work(BookList bookList) { Scanner scanner = new Scanner(System.in); System.out.print("请输入书名:>"); String name = scanner.nextLine(); System.out.print("请输入作者:>"); String author = scanner.nextLine(); System.out.print("请输入价格:>"); int price = scanner.nextInt(); scanner.nextLine(); System.out.print("请输入类型:>"); String type = scanner.nextLine(); System.out.println("存入成功"); Book books = new Book(name,author,price,type); int size = bookList.getUseSize(); bookList.setBooks(books); bookList.setUseSize(size+1); } }
(4)BorrowBook.java文件下
//BorrowBook.java文件下 package BookOperation; import Book.Book; import Book.BookList; import java.util.Scanner; public class BorrowBook implements IOperation{ @Override public void work(BookList bookList) { System.out.println("请输入你要借阅的书籍"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int size = bookList.getUseSize(); for (int i = 0; i <size ; i++) { Book book = bookList.getBook(i); if (name.equals(book.getBookName())) { book.setLend(true); return; } } System.out.println("没有此书"); } }
(5)DelBook.java文件下
//DelBook.java文件下 package BookOperation; import Book.Book; import Book.BookList; import java.util.Scanner; public class DelBook implements IOperation{ @Override public void work(BookList bookList) { System.out.print("请输入你要删除的书名:>"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int size = bookList.getUseSize(); int index = -1; for (int i = 0; i < size; i++) { Book book = bookList.getBook(i); if (name.equals(book.getBookName())) { index = 1; break; } } for (int i = index; i < size -1; i++) { Book tmp = bookList.getBook(i+1); bookList.setBooks(index,tmp); } bookList.setUseSize(size-1); bookList.setBooks(size-1,null); System.out.println("删除成功"); } }
(6)ExitBook.java文件下
//ExitBook.java文件下 package BookOperation; import Book.BookList; public class ExitBms implements IOperation{ @Override public void work(BookList bookList) { System.out.println("你已退出系统"); System.exit(0); } }
(7)FindBook.java文件下
//FindBook.java文件下 package BookOperation; import Book.Book; import Book.BookList; import java.util.Scanner; public class FindBook implements IOperation{ @Override public void work(BookList bookList) { int size = bookList.getUseSize(); System.out.print("请输入书名:>"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); for (int i = 0; i < size; i++) { Book book = bookList.getBook(i); if (name.equals(book.getBookName())) { System.out.println(book); return; } } System.out.println("没有此书"); } }
(8)IOperation.java文件下
//IOperation.java文件下 package BookOperation; import Book.BookList; public interface IOperation { void work(BookList bookList); }
(9)ShowBook.java文件下
//ShowBook.java文件下 package BookOperation; import Book.Book; import Book.BookList; public class ShowBook implements IOperation{ @Override public void work(BookList bookList) { int size = bookList.getUseSize(); for (int i = 0; i < size; i++) { Book book = bookList.getBook(i); System.out.println(book); } } }
(10)Admini.java文件下
//Admini.java文件下 package User; import Book.BookList; import BookOperation.*; import java.util.Scanner; public class Admini extends Control{ public Admini(String name) { super(name); this.iOperations = new IOperation[] { new ExitBms(), new AddBook(), new DelBook(), new FindBook(), new ShowBook(), }; } public int menu() { System.out.println("======================"); System.out.println("欢迎["+name+"]来到图书管理系统"); 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.print("请输入你的操作:>"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
(11)Control.java文件下
//Control.java文件下 package User; import Book.BookList; import BookOperation.IOperation; public abstract class Control { protected String name; protected IOperation[] iOperations; public Control(String name) { this.name = name; } public abstract int menu(); public void work(int pos, BookList bookList) { iOperations[pos].work(bookList); } }
(12)User.java文件下
//User.java文件下 package User; import Book.Book; import Book.BookList; import BookOperation.*; import java.util.Scanner; public class User extends Control { public User(String name) { super(name); this.iOperations = new IOperation[]{ new ExitBms(), new FindBook(), new BorrowBook(), new ShowBook(), }; } public int menu() { System.out.println("======================"); System.out.println("欢迎[" + name + "]来到图书管理系统!"); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.展示图书"); System.out.println("0.退出系统"); System.out.println("======================="); System.out.print("请输入你的操作:>"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
(13)Main.java文件下
//Main.java文件下 import Book.BookList; import User.Admini; import User.Control; import User.User; import java.util.Scanner; //主操作 public class Main { public static Control login() { System.out.print("请输入你的姓名:>"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); System.out.print("请输入你的选择[1.管理员 0.用户]:>"); int choice = scanner.nextInt(); if (choice == 1) { return new Admini(name); }else { return new User(name); } } public static void main(String[] args) { BookList bookList = new BookList(); Control control = login(); while(true) { int ret = control.menu(); control.work(ret,bookList); } } }
2.设计思想
图书管理系统的功能大致为:增加图书,删除图书,修改图书,查找图书,借阅图书,显示图书,退出系统。这几方面,当然这只是最基本的几个功能,你也可以增加其他功能如排序等等。
在图书管理系统中,我们分为两个操作者一个为管理员一个为用户。管理员可以增加图书、删除图书、修改图书、显示图书、退出系统。而用户能查找图书、借阅图书、显示图书、退出系统。
当然,本次的图书管理系统只是一个小小的程序没有连接数据库以及前端页面。实现的效果因此也只是黑框框的展示,但内容很丰富。适合初学者,以及练习使用。
本期博客到这里就结束了,大家可以复制粘贴到编译器上试试。
编辑
下期预告:String