*运用到类与类之间的关系
*掌握抽象类、接口、继承、封装等应用
1、创建图书的类
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean flg;
public Book(String name,String author,int price,String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
创建一个书类(里面定义书的具体参数书名
、作者、价格、种类)
构造函数 一本书的具体信息
@Override
public String toString() {
return "book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((flg==true)?", 已借出":", 未借出" )+
'}';
}
2、创建BookList,当做书架,里面默认有三本书,和书架的内存。同时写Get和Set方法(private)
public class BookList {
private Book[] books=new Book[10];
private int usedSize;//私有提供getset方法
public BookList(){//默认有三本书
books[0]=new Book("三国演义","罗贯中",56,"小说");
books[1]=new Book("西游记","吴承恩",76,"小说");
books[2]=new Book("水浒传","施耐庵",96,"小说");
this.usedSize=3;
}
3、创建一个IOpeartion,用来实现增删查改的功能,由于没有具体实现,所以设为空类
public interface IOpeartion{
public void work(BookList bookList);
}
4、增删查改退出的实现
1)查找一本书
public class FindOperation implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("请输入要查找的书名");
Scanner scanner=new Scanner(System.in);
String bookname=scanner.nextLine();
for (int i = 0; i <bookList.getUsedSize() ; i++) {
Book book=bookList.getBooks(i);
if(book.getName().equals(bookname)){
System.out.println("找到这本书");
System.out.println(book);
return;
}
}
System.out.println("没有找到这本书!");
}
}
2)增加一本书
public void work(BookList bookList) {
Scanner scanner=new Scanner(System.in);
System.out.println("新增图书");
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 pos=bookList.getUsedSize();
bookList.setBooks(pos,book);
int curSize=bookList.getUsedSize();
bookList.setUsedSize(curSize+1);
}
}
3)删除一本书
public class DelOperation implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
System.out.println("请输入要删除的书名");
Scanner scanner = new Scanner(System.in);
String Delname = scanner.nextLine();
int pos = -1;
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBooks(i);
if (book.getName().equals(Delname)) {
int curSize = bookList.getUsedSize() - 1;
Book book1 = bookList.getBooks(i + 1);
bookList.setBooks(i, book1);
int cur = bookList.getUsedSize();
bookList.setUsedSize(cur - 1);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有这本书");
}
4)打印图书
public class DisplayOperation implements IOpeartion{
@Override
public void work(BookList bookList) {
System.out.println("打印图书");
for (int i = 0; i <bookList.getUsedSize() ; i++) {
System.out.println(bookList.getBooks(i));//bookList是类名,应该用它的get方法打印
}
}
}
5)借阅图书
public class BrrowOperation implements IOpeartion{
@Override
public void work(BookList bookList) {
System.out.println("请输入要借阅的书名");
Scanner scanner=new Scanner(System.in);
String brrowname=scanner.nextLine();
for (int i = 0; i <bookList.getUsedSize() ; i++) {
Book book=bookList.getBooks(i);
if(book.getName().equals(brrowname)){
book.setFlg(false);
System.out.println("借阅成功");
return;
}
}
System.out.println("没有找到这本书!");
}
5)归还图书
public class ReturnOperation implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("归还图书");
System.out.println("请输入要归还的书名");
Scanner scanner = new Scanner(System.in);
String returnName = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBooks(i);
if (book.getName().equals(returnName)) {
book.setFlg(true);
System.out.println("归还成功");
return;
}
}
}
}
6)退出系统
public class ExitOperation implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(1);//1代表强制退出
}
}
5、创建使用者的类(管理员和借阅人)
//准备一个接口数组,存储每个对象
public abstract class User {//使用者
public String name;
public IOpeartion[] iOpeartions;
public User(String name){
this.name=name;
}
public abstract int menu();//使用者不同,所以没有任何实现,定义为抽象类
public void doOperation(int choice, BookList bookList){
this.iOpeartions[choice].work(bookList);
}
}
public AdminUser(String name) {//提供构造方法,只要new一个NormalUser对象,构造方法就会执行,准备操作对象
super(name);
this.iOpeartions = new IOpeartion[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
@Override
public int menu() {
Scanner scanner=new Scanner(System.in);
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("===============================");//两者菜单不同
int choice=scanner.nextInt();
return choice;
}
}
public class NormalUser extends User{
public NormalUser(String name){//提供构造方法,只要new一个NormalUser对象,构造方法就会执行,准备操作对象
super(name);
this.iOpeartions=new IOpeartion[] {
new ExitOperation(),
new FindOperation(),
new BrrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
Scanner scanner=new Scanner(System.in);
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("===============================");
int choice=scanner.nextInt();
return choice;
}
}
6、主函数的调用
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 AdminUser(name);//向上转型
} else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();//默认三本书
User user = login();
while (true) {
int choice = user.menu();//调用菜单
user.doOperation(choice, bookList);//由下标实现具体的功能
}
}
}