Java设计模式之外观模式示例详解
366
2022-08-21
Java实现图书馆借阅系统
本文实例为大家分享了java实现图书馆借阅系统的具体代码,供大家参考,具体内容如下
Main.java
package com.src1.booksystem;
import com.src1.booksystem.booklist.BookList;
import com.src1.booksystem.users.AdminUser;
import com.src1.booksystem.users.NormalUser;
import com.src1.booksystem.users.User;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//1.准备书籍
BookList bookList = new BookList();
//2登录
User user = login();
while ( true ) {
int chioce = user.menu();
//3.根据选择调用方法
user.doOperation(chioce, bookList);
}
}
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = scanner.nextLine();
if ( "admin".equals(name) ) {
System.out.println("请输入管理员密码:");
String password = scanner.nextLine();
if ( "admin".equals(password) ) {
System.out.println("hello! 尊敬的管理员! 欢迎来到藏经阁");
return new AdminUser(name);
} else {
System.out.println("密码错误,进入用户端!");
System.out.println("hello! "+ name+" 欢迎来到藏经阁");
return new NormalUser(name);
}
}else {
System.out.println("hello! "+ name+" 欢迎来到藏经阁");
return new NormalUser(name);
}
}
}
package user
AdminUser.java
package com.src1.booksystem.users;
import com.src1.booksystem.operation.*;
import java.util.Scanner;
public class AdminUser extends User {
@Override
public int menu() {
Scanner scanner = new Shttp://canner(System.in);
System.out.println("1.查找图书");
System.out.println("2.新增书籍");
System.out.println("3.删除书籍");
System.out.println("4.书籍列表");
System.out.println("0.退出系统");
int chioce = scanner.nextInt();
return chioce;
}
public AdminUser(String name) {
super(name);
this.operations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
}
NormalUser.java
package com.src1.booksystem.users;
import com.src1.booksystem.operation.*;
import java.util.Scanner;
public class NormalUser extends User {
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("1.查找图书");
System.out.println("2.借阅书籍");
System.out.println("3.归还书籍");
System.out.println("4.书籍列表");
System.out.println("0.退出系统");
int chioce = scanner.nextInt();
return chioce;
}
public NormalUser(String name) {
super(name);
this.operations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new DisplayOperation()
};
}
}
User.java
package com.src1.booksystem.users;
import com.src1.booksystem.booklist.BookList;
import com.src1.booksystem.operation.IOperation;
abstract public class User {
public String name;
public IOperation[] operations;
/**
* 如果没有menu 就不能通过user访问
*/
public abstract int menu() ;
public void doOperation(int chioce, BookList bookList){
//数组当中元素的对象,通过.调用对应operation方法
operations[chioce].work(bookList);
}
public User(String name) {
this.name = name;
}
}
package book
book.java
package com.src1.booksystem.book;
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) {
this.name = name;
this.author = author;
this.price = price;
}
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;
}
puFIEjyshblic int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "[" +
"书名:" + name +
", 价格:" + price + ","+
(isBorrowed ? "已借出":"未借出")+"]";
}
}
package booklist
BookList.java
package com.src1.booksystem.booklist;
import com.src1.booksystem.book.Book;
public class BookList {
private Book[] books;
private int usedSize;
public BookList() {
this.books = new Book[10];
this.books[0] = new Book("西游记","吴承恩",13);
this.books[1] = new Book("水浒传","施耐庵",14);
this.books[2] = new Book("三国演义","罗贯中",15);
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 setBooks(int pos,Book book) {
this.books[pos] = book;
}
}
package operation
AddOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.book.Book;
import com.src1.booksystem.booklist.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名");
String name = scanner.nextLine();
String author = scanner.nextLine();
System.out.println("请输入价格");
int price = scanner.nextInt();
Book book = new Book(name, author, price);
int curSize = bookList.getUsedSize();
bookList.setBooks(curSize, book);
bookList.setUsedSize(curSize + 1);
System.out.println("新增成功");
}
}
BorrowOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.book.Book;
import com.src1.booksystem.booklist.BookList;
import java.util.Scanner;
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();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
if(book.isBorrowed()) {
System.out.println("此书已经借出了!");
return;
}
book.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
System.out.println("没系统未收录这本书!");
}
}
DelOperation
未完善
package com.src1.booksystem.operation;
import com.src1.booksystem.booklist.BookList;
import java.util.Scanner;
import com.src1.booksystem.book.Book;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入要删除的书名:");
Scanner scanner = new FIEjyshScanner(System.in);
String delbook = scanner.nextLine();
int size = bookList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(delbook)) {
if(i+1 bookList.setBooks(i, bookList.getBook(i + 1)); } bookList.setUsedSize(size-1); if(i+2 < size) { bookList.setBooks(i+1, bookList.getBook(i + 2)); } } } System.out.println("删除成功!"); } } 修改 public class DelOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("请输入要删除的书名:"); Scanner scanner = new Scanner(System.in); String delbook = scanner.nextLine(); int size = bookList.getUsedSize(); int pos = 0; int i = 0; for ( i = 0; i < size; i++) { Book book = bookList.getBook(i); if(book.getName().equals(delbook)) { pos = i; break; } } if(i == size){ System.out.println("系统中没有这本书"); return; } for(int j = pos; j < size-1; j++) { bookList.setBooks(j, bookList.getBook(j + 1)); } bookList.setUsedSize(size-1); System.out.println("删除成功!"); } } DisplayOperation import com.src1.booksystem.booklist.BookList; public class DisplayOperation implements IOperation{ @Override public void work(BookList bookList) { for (int i = 0; i < bookList.getUsedSize(); i++) { System.out.println(bookList.getBook(i)); } } } ExitOperation package com.src1.booksystem.operation; import com.src1.booksystem.booklist.BookList; public class ExitOperation implements IOperation { @Override public void work(BookList bookList) { System.exit(0); } } FindOperation package com.src1.booksystem.operation; import com.src1.booksystem.book.Book; import com.src1.booksystem.booklist.BookList; import java.util.Scanner; public class FindOperation implements IOperation{ @Override public void work(BookList bookList) { Scanner scanner = new Scanner(System.in); System.out.println("请输入要查找的书名"); String name = scanner.nextLine(); for( int i = 0; i < bookList.getUsedSize(); i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)) { System.out.println(book); return; } } } } ReturnOperation package com.src1.booksystem.operation; import com.src1.booksystem.book.Book; import com.src1.booksystem.booklist.BookList; import java.util.Scanner; public class ReturnOperation implements IOperation { @Override public void work(BookList bookList) { Scanner scanner = new Scanner(System.in); System.out.println("请输入你要归还的书名:"); String name = scanner.nextLine(); for (int i = 0; i < bookList.getUsedSize(); i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)) { //说明有这本书 book.setBorrowed(false); System.out.println("归还成功!"); return; } } System.out.println("系统未找到本书的相关信息!"); } } 接口 IOperation package com.src1.booksystem.operation; import com.src1.booksystem.booklist.BookList; public interface IOperation { void work(BookList bookList); }
bookList.setBooks(i, bookList.getBook(i + 1));
}
bookList.setUsedSize(size-1);
if(i+2 < size) {
bookList.setBooks(i+1, bookList.getBook(i + 2));
}
}
}
System.out.println("删除成功!");
}
}
修改
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入要删除的书名:");
Scanner scanner = new Scanner(System.in);
String delbook = scanner.nextLine();
int size = bookList.getUsedSize();
int pos = 0;
int i = 0;
for ( i = 0; i < size; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(delbook)) {
pos = i;
break;
}
}
if(i == size){
System.out.println("系统中没有这本书");
return;
}
for(int j = pos; j < size-1; j++) {
bookList.setBooks(j, bookList.getBook(j + 1));
}
bookList.setUsedSize(size-1);
System.out.println("删除成功!");
}
}
DisplayOperation
import com.src1.booksystem.booklist.BookList;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
for (int i = 0; i < bookList.getUsedSize(); i++) {
System.out.println(bookList.getBook(i));
}
}
}
ExitOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.booklist.BookList;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.exit(0);
}
}
FindOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.book.Book;
import com.src1.booksystem.booklist.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查找的书名");
String name = scanner.nextLine();
for( int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println(book);
return;
}
}
}
}
ReturnOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.book.Book;
import com.src1.booksystem.booklist.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要归还的书名:");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
//说明有这本书
book.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("系统未找到本书的相关信息!");
}
}
接口 IOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.booklist.BookList;
public interface IOperation {
void work(BookList bookList);
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~