BMS
- 2. 书架储存书【bookList】
- 初始化书架,类构造方法getBook,拿出pos处的书setBook,给pos下标放一本书getUesSize,setUesSize,获取实时书本数,用于修改
- 3. 设置两个角色【user】
- 构造方法,提供构造方法帮助父类构造构造方法,提供构造方法帮助父类构造
- 4. 每个角色对应功能列表不一样
- 5. 实现每一具体功能【operation】
- 功能标准化【IOperation】增添图书【AddOperation】借阅图书【BorrowOperation】删除图书【DelOperation】显示图书【DisplayOperation】归还图书【ReturnOperation】查找图书【FindOperation】退出系统【ExitOperation】
项目实现
创几个包:用来组织类(分类)文章源自懂站帝-http://www.sfdkj.com/23071.html

文章源自懂站帝-http://www.sfdkj.com/23071.html
1. 每本书数据【book】
public class Book { }
设置字段(书的属性)
public class Book { private String name; //书名 private String author; //作者 private int price; //价格 private String type; //类型 private boolean isBorrowed;//是否被借出 默认false
提供构造
实例化对象,设置几个属性,因为字段是private修饰的就给每属性get set方法文章源自懂站帝-http://www.sfdkj.com/23071.html
在加个构造和toString方法文章源自懂站帝-http://www.sfdkj.com/23071.html
//构造 public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; }
get set方法,便于操作数据
//get set 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; }
重写toString方法,便于打印显示,(借出处略加修改)
//toString @Override public String toString() { return "Book{" "name='" name ''' ", author='" author ''' ", price=" price ", type='" type ''' ((isBorrowed==true)?"已经借出":"未借出") '}'; }
2. 书架储存书【bookList】
BookList类
public class BookList{ }
设置书架大小
//对多放十本 private Book[] books = new Book[3]; private int usedSize ; //实时记录 目前有几本书
初始化书架,类构造方法
public BookList() { books[0] = new Book("西游记","吴承恩",10,"小说"); books[1] = new Book("三国演义","施耐庵",11,"小说"); books[2] = new Book("红楼梦","曹雪芹",12,"小说"); usedSize = 3; }
getBook,拿出pos处的书
//拿出pos处的书 public Book getBook(int pos){ return books[pos]; }
setBook,给pos下标放一本书
// 给pos下标放一本书 public void setBook(int pos, Book book){ books[pos] = book; }
getUesSize,setUesSize,获取实时书本数,用于修改
//获取实时书本数,用于修改 public int setUesSize(int size){ return (usedSize = size); } public int getUesSize(){ return this.usedSize; }
3. 设置两个角色【user】
管理员【AdmainUser】
public class AdmainUser extends User{ }
构造方法,提供构造方法帮助父类构造
//提供构造方法帮助父类构造 public AdmainUser(String name) { super(name); //在new管理人员这个对象时,就把这些功能一并写入 this.iOperations = new IOperation[]{ new ExitOperation(),//0 new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation() };
//设置一个开始菜单 public int menu (){ System.out.println("hello" this.name "欢迎使用BMS!"); System.out.println("1.查找图书"); System.out.println("2.增添图书"); System.out.println("3.删除图书"); System.out.println("4.显示图书"); System.out.println("0.退出BNS"); Scanner scanner = new Scanner(System.in); return scanner.nextInt(); }
普通用户【NormalUser】
public class NormalUser extends User{ }
菜单【menu】
//设置一个开始菜单 public int menu (){ System.out.println("hello" this.name "欢迎使用BMS!"); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.归还图书"); System.out.println("0.退出BNS"); Scanner scanner = new Scanner(System.in); return scanner.nextInt(); }
构造方法,提供构造方法帮助父类构造
//提供构造方法帮助父类构造 public NormalUser(String name) { super(name); //在new一般人员这个对象时,就把这些功能一并写入 this.iOperations = new IOperation[]{ new ExitOperation(),//0 new FindOperation(), new BorrowOperation(), new ReturnOperation() }; }
4. 每个角色对应功能列表不一样
身份不同,拥有的功能不一样文章源自懂站帝-http://www.sfdkj.com/23071.html
管理员文章源自懂站帝-http://www.sfdkj.com/23071.html | 一般用户文章源自懂站帝-http://www.sfdkj.com/23071.html |
1.查找图书文章源自懂站帝-http://www.sfdkj.com/23071.html | 1.查找图书文章源自懂站帝-http://www.sfdkj.com/23071.html |
2.增添图书文章源自懂站帝-http://www.sfdkj.com/23071.html | 2.借阅图书文章源自懂站帝-http://www.sfdkj.com/23071.html |
3.删除图书文章源自懂站帝-http://www.sfdkj.com/23071.html | 3.归还图书文章源自懂站帝-http://www.sfdkj.com/23071.html |
4.显示图书文章源自懂站帝-http://www.sfdkj.com/23071.html | |
0.退出BNS文章源自懂站帝-http://www.sfdkj.com/23071.html | 0.退出BNS文章源自懂站帝-http://www.sfdkj.com/23071.html |
5. 实现每一具体功能【operation】
接口【interface】
功能标准化【IOperation】
public interface IOperation { //实现功能 public abstract void work(BookList bookList);}
增添图书【AddOperation】
- implements实现接口
- 录入书本信息
- 用getUesSize(),在书架的size位置插入这本书
Tips:
要先输入字符串String,再输入整数int!文章源自懂站帝-http://www.sfdkj.com/23071.html
先输入整数再输字符串的话回车会被读进去文章源自懂站帝-http://www.sfdkj.com/23071.html
package operation;import book.Book;import book.BookList;import java.util.Scanner;public class AddOperation implements IOperation{ @Override 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 newbook = new Book(name,author,price,type); bookList.setBook(bookList.getUesSize(),newbook); bookList.setUesSize(bookList.getUesSize() 1); //更新数量 System.out.println("新增书记成功!"); }}
借阅图书【BorrowOperation】
package operation;import book.Book;import book.BookList;import java.util.Scanner;public class BorrowOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("借出图书!"); System.out.print("请输入借阅图书的名字:"); int currentSize = bookList.getUesSize(); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); for (int i = 0 ; i < currentSize; i ) { Book book = bookList.getBook(i); if (book.getName().equals(name)) { System.out.println("找到了!"); book.setBorrowed(true); //改状态 System.out.println("借阅成功!"); return; } } System.out.println("没有这本书!"); System.out.println(); }}
删除图书【DelOperation】
package operation;import book.Book;import book.BookList;import java.util.Scanner;/** *覆盖 */public class DelOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("删除图书!"); System.out.print("请输入图书的名字:"); int currentSize = bookList.getUesSize(); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int index = -1;//存下标 int i = 0; for ( ; i < currentSize; i ) { Book book = bookList.getBook(i); if (book.getName().equals(name)) { System.out.println("找到了!"); index = i; System.out.println(book); System.out.println(); } } if (i >= currentSize) { System.out.println("找不到此书"); return; } //直接覆盖 for (int j = index; j < currentSize-1; i ) { bookList.setBook(j,bookList.getBook(j 1)); } bookList.setUesSize(bookList.getUesSize()-1); //更新数量 bookList.setBook(currentSize-1,null); //置为空 相当于free System.out.println("删除成功!"); System.out.println(); }}
显示图书【DisplayOperation】
package operation;import book.BookList;public class DisplayOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("显示图书!"); int currentSize = bookList.getUesSize(); for (int i = 0; i < currentSize; i ) { System.out.println(bookList.getBook(i)); } System.out.println(); }}
归还图书【ReturnOperation】
package operation;import book.Book;import book.BookList;import java.util.Scanner;public class ReturnOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("归还图书!"); System.out.print("请输入归还图书的名字:"); int currentSize = bookList.getUesSize(); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); for (int i = 0 ; i < currentSize; i ) { Book book = bookList.getBook(i); if (book.getName().equals(name)) { System.out.println("找到了!"); book.setBorrowed(true); //改状态 System.out.println("归还成功!"); return; } } System.out.println("没有这本书!"); System.out.println(); }}
查找图书【FindOperation】
package operation;import book.Book;import book.BookList;import java.util.Scanner;public class FindOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.print("请输入图书的名字:"); int currentSize = bookList.getUesSize(); Scanner scanner = new Scanner(System.in); 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); System.out.println(); return; } } System.out.println("没有这本书!"); System.out.println(); }}
退出系统【ExitOperation】
package operation;import book.BookList;public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出系统!"); System.exit(0); // 状态码 零代表正常结束,负数异常结束 }}
6. 主函数 【Main】
整理所有功能文章源自懂站帝-http://www.sfdkj.com/23071.html
import book.Book;import book.BookList;import user.AdmainUser;import user.NormalUser;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-->管理员,2-->普通用户"); int choise = scanner.nextInt(); if(choise == 1){ return new AdmainUser(name); }else { return new NormalUser(name); } } public static void main(String[] args) { //整合! BookList bookList = new BookList();//准备图书 //登录, User user = login(); while(true) { int choise = user.menu();//父类的引用想访问子类的方法,此方法必须为抽象方法,动态绑定 user.doOperator(choise, bookList); } }}
率7. 总结
我亦无他,惟手熟尔!,多动手实操!在实践中进步!文章源自懂站帝-http://www.sfdkj.com/23071.html