做公司网站要收费吗,平昌县住房和城乡建设局网站,嘉鱼网站建设前十,建网站需要哪些文件夹一.抽象出对象:
1.要有书架#xff0c;图书#xff0c;用户#xff08;包括普通用户#xff0c;管理员用户#xff09;。根据这些我们可以建立几个包#xff0c;来把繁杂的代码分开#xff0c;再通过一个类来把这些#xff0c;对象整合起来实现系统。说到整合#xf…一.抽象出对象:
1.要有书架图书用户包括普通用户管理员用户。根据这些我们可以建立几个包来把繁杂的代码分开再通过一个类来把这些对象整合起来实现系统。说到整合肯定缺不了相关接口我们再定义一个放接口和扩展这个接口的方法。
如图 二.构思
1.先在书架类上初始化好默认书籍其他构造方法如:getBook,setBook(在具体的下标放书和返回书)具体在写实现接口的方法时来增加。
public class BookList {//组合的方式初始化书架private Book[] books new Book[10];private int usedSize;//实际放的书的个数//初始化书架放书public BookList() {this.books[0] new Book(三国演义, 罗贯中, 12, 小说);this.books[1] new Book(红楼梦, 曹雪芹, 13, 小说);this.books[2] new Book(西游记, 吴承恩, 14, 小说);this.usedSize 3;}//返回一本pos要找的书下标的书public Book getBook(int pos) {return books[pos];}//插入一本书的方法(相当于要初始化好书架原来已有的书)public void setBook(int pos, Book books) {this.books[pos] books;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize usedSize;}public Book[] getBooks() {return books;}public void setBooks(Book[] books) {this.books books;}
} 2.在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) ? 已借出 : 未借出) /* isBorrowed isBorrowed*/};}
} 3.在User类中定义好name和相关构造方法以及接口命名的数组为后面到达调用扩展了接口的类里的方法做铺垫。
public abstract class User {protected String name;//定义接口命名类型的数组后续配合// 【return new AdminUser(name);】就可以看出,再加上接口调用的方法就知道操作了哪一个方法protected IOperation[] iOperations;//要根据子类来初始化父类成员变量public User(String name) {this.name name;}public abstract int menu();//这里封装一个方法提供给Main调用。public void DoIOperation(int choice, BookList bookList) {//这里iOperations数组里有我们要的对象通过数组里的对象调用接口里的方法iOperations[choice].work(bookList);}
} 4.管理员类中AdminUser和普通用户类中NormalUser继承了user类初始化好系统菜单相关构造方法。这个构造方法很关键用接口作为数组相当于实例化了扩展了接口的类的方法达到调用系统具体方法的作用
public class NormalUser extends User{public NormalUser(String name) {super(name);//通过【return new AdminUser(name);】,再加上实现接口的方法就知道操作了哪一个方法//登录界面选择了哪个角色NormalUser或者AdminUserthis就是哪个的引用this.iOperations new IOperation[] {//这些对象都实现了iOperations接口所以不会报错//下面相当于实例化了扩展了接口的类的方法达到调用系统具体方法的作用new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation(),};}public int menu() {System.out.println(欢迎 this.name 使用图书系统);System.out.println(********普通用户菜单********);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);System.out.println(请输入你的操作:);int choice scanner.nextInt();return choice;}}5.在Main类中写好登录界面及整合一下如何实例化对象来操作系统。 public static void main(String[] args) {//实例化书架BookList bookList new BookList();//通过返回值向上转型确定用户//这里的user是返回的AdminUser或者NormalUserUser user login();while (true) {//然后通过返回信息调用恰当的菜单int choice user.menu();//发生了动态绑定/*** 根据choice,返回值看看调用了哪个方法** 1.哪个对象* 答User user login();** 2.哪个方法-》进一步还要确定当前对象包含了这些方法*答在构造方法【return new AdminUser(name)】运行时会初始化好对应的操作对象。** 注意后面通过父类对象调用方法int choice user.menu();通过choice判断* 调用了哪个方法 接下来就要对父类进行操作*/user.DoIOperation(choice, bookList);}}}6.初始化好接口和菜单里操作系统的work方法实现了这个接口的类就是每个操作系统的方法
public interface IOperation {//这个接口有操作书架的方法在其他类实现就可以操作性的区分不同用户的方法public void work(BookList bookList);
}7.接下来就是实现了接口的每一个类每个操作系统的方法
以下是管理员菜单方法
(1).查找图书:
public class FindOperation implements IOperation {Overridepublic void work(BookList bookList) {System.out.println(查找图书);int currentSize bookList.getUsedSize();Scanner scanner new Scanner(System.in);System.out.println(请输入你要查找的图书);String name scanner.nextLine();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(没有你要找的书...);}
}(2).新增图书
public class AddOperation implements IOperation{Overridepublic void work(BookList bookList) {//1.判断书架数组是否满了int currentSize bookList.getUsedSize();if (currentSize bookList.getBooks().length) {System.out.println(该书架满了不能放了);return;}//2.构建对象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();System.out.println(请输入新增的类型);String type scanner.next();Book newBook new Book(name, author, price, type);//3.判断书架是否已经存在这本书for (int i 0; i currentSize; i) {//遍历书架已初始化的书Book book bookList.getBook(i);if (book.getName().equals(name)) {System.out.println(书已经存在了不用再添加了);return;}}//插入图书bookList.setBook(currentSize, newBook);bookList.setUsedSize(currentSize1);}
}(3).删除图书:
public class DelOperation implements IOperation{Overridepublic void work(BookList bookList) {System.out.println(删除图书);int currentSize bookList.getUsedSize();Scanner scanner new Scanner(System.in);System.out.println(请输入你要删除的图书);String name scanner.nextLine();int pos 0;int i 0;for (; i currentSize; i) {//遍历书架已初始化的书Book book bookList.getBook(i);if (book.getName().equals(name)) {//找到要删除的位置pos i;break;}}if (i currentSize) {System.out.println(没有找到你要删除的图书);}//开始删除for (int j pos; j currentSize-1; j) {//思路:bookList[j] bookList[j1];//先找到j1,那个位置然后覆盖Book book bookList.getBook(j1);bookList.setBook(j, book);}//更新下标bookList.setUsedSize(currentSize-1);System.out.println(删除成功);}
}(4).显示图书
public class ShowOperation implements IOperation{Overridepublic 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);}}} (5).退出系统
public class ExitOperation implements IOperation{Overridepublic void work(BookList bookList) {System.out.println(退出系统);System.exit(0);}
}
以下是普通用户菜单方法
(1).退出系统和查找图书是普通人员和管理员的共同方法 (2归还图书
public class ReturnOperation implements IOperation{Overridepublic void work(BookList bookList) {System.out.println(归还图书);int currentSize bookList.getUsedSize();Scanner scanner new Scanner(System.in);System.out.println(请输入你要归还的图书);String name scanner.nextLine();for (int i 0; i currentSize; i) {//遍历书架已初始化的书Book book bookList.getBook(i);if (book.getName().equals(name)) {//看看isBorrowed返回,ture(已借出),还是false(未借出)if (book.isBorrowed()) {book.setBorrowed(false);return;}}}System.out.println(错误没有你要归还的图书);}
}
(3借阅图书
public class BorrowOperation implements IOperation{Overridepublic void work(BookList bookList) {System.out.println(借阅图书);int currentSize bookList.getUsedSize();Scanner scanner new Scanner(System.in);System.out.println(请输入你要借阅的图书);String name scanner.nextLine();for (int i 0; i currentSize; i) {//遍历书架已初始化的书Book book bookList.getBook(i);if (book.getName().equals(name)) {//看看isBorrowed返回,ture(已借出),还是false(未借出)if (book.isBorrowed()) {System.out.println(该书已经被借出);return;}book.setBorrowed(true);//置为借出System.out.println(借阅成功);return;}}System.out.println(没有找到你要借阅的那本书);}
}