企业网站建设基本流程图,短视频营销的优势和劣势,word文档怎么做网站跳转链接,免费购物系统文章目录 1.书类2.图书列表3.新增图书4.借阅图书5.删除图书6.显示图书7.退出系统8.查找图书9.接口10.归还图书11.管理员菜单12.普通用户菜单13.用户类14.主函数 1.书类
package book;public class Book {private String name;private String author;private int price;private… 文章目录 1.书类2.图书列表3.新增图书4.借阅图书5.删除图书6.显示图书7.退出系统8.查找图书9.接口10.归还图书11.管理员菜单12.普通用户菜单13.用户类14.主函数 1.书类
package 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, 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;}Overridepublic String toString() {return book{ name name \ , author author \ , price price , type type \ (isBorrowed true ? 已借出 : 未借出 )};}
}2.图书列表
package book;public class BookList {public Book[] bookList new Book[10];private int usedSize;//当前书架上书的数目public BookList() {bookList[0] new Book(三国演义,罗贯中,18,小说);bookList[1] new Book(水浒传,施耐庵,48,小说);bookList[2] new Book(西游记,吴承恩,28,小说);this.usedSize 3;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize usedSize;}//把书放到pos位置public void setBookList(int pos,Book book) {bookList[pos] book;}//获取pos位置的书public Book getBook(int pos) {return bookList[pos];}//这里本质上是可以写借阅书籍等操作的
}3.新增图书
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class AddOperation implements IOperation {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 currentSize bookList.getUsedSize();bookList.setBookList(currentSize,book);bookList.setUsedSize(currentSize 1);//放入一本书System.out.println(新增成功);}
}4.借阅图书
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(借阅图书);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(借阅成功借阅书籍信息如下);System.out.println(book);return;}}System.out.println(借阅失败没有这本书籍);}
}5.删除图书
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class DelOperation implements IOperation {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();int index -1;for (int i 0; i currentSize; i) {Book book bookList.getBook(i);if (book.getName().equals(name)) {index i;break;}}//如果index-1说明没有这本书if (index -1) {System.out.println(没有你要删除的图书);return;}//从这里开始删除for (int i 0; i currentSize-1; i) {Book book bookList.getBook(i 1);bookList.setBookList(i,book);}bookList.setUsedSize(currentSize - 1);}
}6.显示图书
package operation;import book.Book;
import book.BookList;public class DisplayOperation implements IOperation {public void work(BookList bookList) {System.out.println(显示图书);int currentSize bookList.getUsedSize();for (int i 0; i currentSize; i) {Book book bookList.getBook(i);System.out.println(book);}}
}7.退出系统
package operation;import book.BookList;public class ExitOperation implements IOperation{public void work(BookList bookList){//可以对书架进行手动清空System.out.println(退出系统);System.exit(0);}
}8.查找图书
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(查找图书);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)) {System.out.println(找到了这本书);System.out.println(book);return;}}System.out.println(没有这本书);}
}9.接口
package operation;import book.BookList;public interface IOperation {void work(BookList bookList);
}10.归还图书
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(归还图书);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(归还成功归还书籍信息如下);System.out.println(book);return;}}System.out.println(归还失败没有这本书籍);}
}11.管理员菜单
package user;import operation.*;import java.util.Scanner;public class AdiminUser extends User {public AdiminUser(String name) {super(name);this.iOperations new IOperation[] {new ExitOperation(),new FindOperation(),new AddOperation(),new DelOperation(),new DisplayOperation()};}public int menu() {System.out.println(管理员菜单);System.out.println(hello 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;}
}12.普通用户菜单
package user;import operation.*;import java.util.Scanner;public class NormaUser extends User {public NormaUser(String name) {super(name);this.iOperations new IOperation[] {new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation()};}public int menu() {System.out.println(普通用户菜单);System.out.println(hello 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;}
}13.用户类
package user;import book.BookList;
import operation.IOperation;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);}
}14.主函数
import book.BookList;
import user.AdiminUser;
import user.NormaUser;
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管理员 0普通用户);int choice scanner.nextInt();if (choice 1) {return new AdiminUser(name);}else{return new NormaUser(name);}}public static void main(String[] args) {BookList bookList new BookList();User user login();//User user AdiminUser(name);//User user NormaUser(name);while (true){int choice user.menu();user.doOperation(choice,bookList);}}
}