赞
踩
public class Book {
private String name;
private String author;
private Double price;
private String type;
private boolean state;
private String Borrower;
}
构造方法
和getter()
和setter()
以及重写toString
public String getBorrower() {
return Borrower;
}
public void setBorrower(String borrower) {
Borrower = borrower;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", " + (state ? "已被借出" : "未被借出") +
'}';
}
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(Double price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public Book(String name, String author, Double price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
public class BookList {
private Book[] books = new Book[5];
private int number = 3;
private int capacity = 5;
{
books[0] = new Book("小王子1", "王子1", 99.9, "学习");
books[1] = new Book("小王子2", "王子2", 99.9, "学习");
books[2] = new Book("小王子3", "王子3", 99.9, "学习");
}
}
toString()
方法 @Override
public String toString() {
String str = "";
for (int i = 0; i < number; i++) {
str += "\n" + books[i];
}
return str + '\n';
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public void setBooks(Book[] books) {
this.books = books;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public Book[] getBooks() {
return books;
}
public Book getBooks(int pos) {
return books[pos];
}
public void setBooks(Book book, int pos) {
this.books[pos] = book;
}
public class Main {
public static User judge(int role, String name) {
return role == 1 ? new Admin(name) : new Normal(name);
}
public static void main(String[] args) {
BookList bookList = new BookList();
while(true) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入你的名字:>");
String name = scanner.nextLine();
System.out.println("1. 管理员\n2. 普通用户");
System.out.print("请输入你的身份:>");
int role = scanner.nextInt();
User user = judge(role, name);
user.menu(bookList);
}
}
}
public abstract class User {
protected String name;
public abstract void menu(BookList bookList);
public User(String name) {
this.name = name;
}
}
public class Admin extends User {
//继承User
}
public class Normal extends User {
//继承User
}
User user = judge(role, name);
user.menu(bookList);
public static User judge(int role, String name) {
return role == 1 ? new Admin(name) : new Normal(name);
}
这两段可能比较难理解
其实就是根据 输入role
判断judge()
返回什么类型的实例化
menu()
方法menu()
方法public interface IOpera {
public void work(BookList bookList, String person);
//待重写方法
//这里的person指的是使用之人
}
'\n'
public class Add implements IOpera{
@Override
public void work(BookList bookList, String s) { //这里的s并没有实际意义,只是为了满足重写要求
//输入书的信息
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要添加的书名:>");
String name = scanner.nextLine();
System.out.print("请输入这本书的作者:>");
String author = scanner.nextLine();
System.out.print("请输入这本书的类别:>");
String type = scanner.nextLine();
System.out.print("请输入这本书的价格:>");
Double price = scanner.nextDouble();
//拿到书
Book book = new Book(name, author, price, type);
//放入书架
bookList.setBooks(book, bookList.getNumber());
bookList.setNumber(bookList.getNumber() + 1);
//扩容机制
if(bookList.getCapacity() == bookList.getNumber()) {
bookList.setCapacity(bookList.getCapacity() + 5);
Book[] books = Arrays.copyOf(bookList.getBooks(), bookList.getCapacity());
bookList.setBooks(books);
}
System.out.println("添加成功!");
}
}
Search类
public class Del implements IOpera{
@Override
public void work(BookList bookList, String person) {
System.out.print("请输入你要删除的书:>");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int index = Search.search(bookList, name);
if(index != -1) {
if(bookList.getBooks(index).isState()) {
System.out.println("此书被借出,无法删除");
System.out.println("请找" + bookList.getBooks(index).getBorrower() + "要回此书");
return;
}
System.out.println("删除成功");
bookList.setBooks(null, index);
for (int i = index + 1; i < bookList.getNumber(); i++) {
bookList.setBooks(bookList.getBooks(i), i - 1);
bookList.setBooks(null, i);
}
bookList.setNumber(bookList.getNumber() - 1);
}else {
System.out.println("找不到你要删除的书");
}
}
}
public class Search implements IOpera{
public static int search(BookList bookList, String name) {
for (int i = 0; i < bookList.getNumber(); i++) {
if(bookList.getBooks()[i].getName().equals(name)) {
return i;
}
}
return -1;
}
@Override
public void work(BookList bookList, String person) {
System.out.print("请输入你想查询的书:>");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int index = search(bookList, name);
if(index != -1) {
System.out.println("找到了");
System.out.println(bookList.getBooks()[index]);
}else {
System.out.println("未能找到");
}
}
}
bookList
即可public class Show implements IOpera{
@Override
public void work(BookList bookList, String person) {
System.out.println(bookList);
System.out.println("展示成功");
}
}
public class Borrow implements IOpera{
@Override
public void work(BookList bookList, String person) {
System.out.print("请输入你要借的书:>");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int index = Search.search(bookList, name);
if(index != -1) {
if(bookList.getBooks(index).isState()) {
System.out.println(name + "已被借走");
}else {
System.out.println("借阅成功");
bookList.getBooks(index).setState(true);
bookList.getBooks(index).setBorrower(person);
}
}else {
System.out.println("你要的书这里找不到");
}
}
}
public class Return implements IOpera{
@Override
public void work(BookList bookList, String person) {
System.out.print("请输入你要归还的书:>");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int index = Search.search(bookList, name);
if(index != -1 && bookList.getBooks(index).isState()) {
System.out.println("归还成功");
bookList.getBooks(index).setState(false);
}else {
System.out.println("此书并不是你在此处借的");
}
}
}
exit(0)
退出的想法,因为我想继续有人进入系统public class Exit implements IOpera{
@Override
public void work(BookList bookList, String person) {
System.out.println("退出成功");
//System.exit(0) 为正常退出执行!就是直接不执行了,而不是跳到末尾,也不是直接此方法结束,是整个程序结束
}
}
知识背景:数组向上转型技巧 --> 各元素不同的向上转型
public class Admin extends User{
public Admin(String name) {
super(name);//传参不是给自己,而是给父类构造
}
//不同下标的元素,实现了不同的向上转型,重写了不同的方法
private IOpera[] iOperas = {
new Exit(),
new Add(),
new Del(),
new Search(),
new Show()
};
}
@Override
public void menu(BookList bookList) {
int choice = 0;
do{
System.out.println("********************************");
System.out.println("hello " + 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);
choice = scanner.nextInt();
iOperas[choice].work(bookList, this.name);
//调用被不同向上转型的不同引用的work()方法
}while(!(iOperas[choice] instanceof Exit));
//只要改下标对应元素是Exit实例化的,就退出。
}
public class Normal extends User{
public Normal(String name) {
super(name);
}
private IOpera[] iOperas = {
new Exit(),
new Search(),
new Borrow(),
new Return()
};
@Override
public void menu(BookList bookList) {
int choice = 0;
do {
System.out.println("********************************");
System.out.println("hello " + 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);
choice = scanner.nextInt();
iOperas[choice].work(bookList, this.name);
}while(!(iOperas[choice] instanceof Exit));
}
}
文章到此结束!谢谢观看
可以叫我 小马,我可能写的不好或者有错误,但是一起加油鸭声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/512273
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。