框架搭建
book包
将书相关的放到book包中,创建一个Book类用来设置书的属性,包括书名、作者、价格、类型、是否被借出等。
以上属性均被private所修饰
利用编译器生成构造方法(不需要构造isBorrowed,因为其初始值为false,表示为未借出),
生成get、set方法(其中isBorrowed方法生不生成无所谓,以后用不到)
重写toString方法,用来打印书 属性
代码如下:
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=" + isBorrowed +'}';}
}
创建一个BookList类,作为书架
使用数组存放书
public class BookList {private Book[] books = new Book[10];private int useSize;//有效的数据个数【实际存放书的数量】
}
初始容量设为10,不够的话,后面可以扩容数组
定义一个变量useSize,用来存放实际存放书的数量
再写一个构造方法,当图书管理系统运行后,里面默认存放了三本书
public BookList() {books[0] = new Book("三国演义","罗贯中",10,"小说");books[1] = new Book("西游记","吴承恩",20,"小说");books[2] = new Book("红楼梦","曹雪芹",30,"小说");this.useSize = 3;}
user包
创建一个user包,存放用户信息
创建一个User类,用来提取用户的共性属性,如name
因为name一定会被继承,所以不建议设置为private,为了代码有一定的限制,也不能用public,用default修饰只能在同一个包访问,也不合适,因此用protected来修饰最为合适
再添加一个构造方法,当子类继承父类时,在子类中帮助父类成员进行初始化
因为User类不能表示某一种具体的用户类型,所以将User设置为抽象类(abstract),更为合适
tips:抽象类中不一定有抽象方法,但有抽象方法的类一定是抽象类
package user;public abstract class User {protected String name;public User(String name) {this.name = name;}
}
创建一个AdminUser类作为管理员,继承User
当子类继承于父类时,父类中的构造方法一定要被调用,父类中的抽象方法一定要被重写
package user;public class AdminUser extends User {public AdminUser(String name) {super(name);}
}
同理,生成一个NormalUser,普通用户类
package user;public class NormalUser extends User {public NormalUser(String name) {super(name);}
}
管理员菜单和普通用户菜单,分别写在AdminUser和NormalUser
public void menu() {System.out.println("******管理员菜单******");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);System.out.println("请输入你的操作:");int choice = scanner.nextInt();}public void menu() {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();}
Main函数
创建主函数Main,按照输入值判断用户类型
import user.AdminUser;
import user.NormalUser;
import user.User;import java.util.Scanner;public class Main {public static void login() {Scanner scanner = new Scanner(System.in);System.out.println("请输入你的姓名:");String name = scanner.nextLine();System.out.println("请输入你的身份,1:管理员 2:普通用户");int choice = scanner.nextInt();if(choice == 1){return new AdminUser(name);}else{return new NormalUser(name);}}public static void main(String[] args) {}
}
发现返回类型不匹配,如下:
利用向上转型,通过User作为统一的返回值进行接收,即可解决
在main函数中用User类型变量接收,去调用menu()却报错了:
这是因为父类中没有menu()方法,不能调用子类中的方法
在父类中加一个抽象方法 menu() ,使其发生动态绑定,即可解决
ioperations包
我们的操作都是在书架上,代码也应写在BookList中,但这里发现管理员用户和普通用户的某些操作是不一样的(如:同是操作 2 ,一个是新增,一个是借阅),为了方便区分不同用户类型的操作,我们再创建一个 ioperations 包,在此包内放所有操作
为了区分不同用户类型的操作,我们创建一个接口IOPeration,在此接口中,设置方法work()
package ioperations;import book.BookList;public interface IOPeration {void work(BookList bookList);
}
然后在其他操作中实现该接口,重写此方法
package ioperations;import book.BookList;public class AddOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("新增图书。。。");}
}package ioperations;import book.BookList;public class BorrowedOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("借阅图书。。。");}
}package ioperations;import book.BookList;public class DelOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("删除图书。。。");}
}package ioperations;import book.BookList;public class ExitOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("退出系统。。。");}
}package ioperations;import book.BookList;public class FindOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("查找图书。。。");}
}package ioperations;import book.BookList;public class ReturnOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("归还图书。。。");}
}package ioperations;import book.BookList;public class ShowOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("显示图书。。。");}
}
功能判断选择
下面将这些操作组织起来,首先要接收返回值,来确认调用哪一个操作
第一步:当用户选择管理员或普通用户之后,这里演示为管理员,将choice值返回用来判断调用哪一种操作
第二步:将返回值类型设置为int类型
第三步:同时将User父类中的同名方法的返回值类型也改为int(此处是子类重写父类menu方法)
在main函数中接收返回值,并判断需调用 哪个对象 的 哪个方法
上图中,1 操作已经确定 哪个对象,原理如下:
接下来要确定调用 该对象的 哪个方法
第一步,确定当前对象已经包含了这些方法
User类,通过user调用某个方法,传入choice,才能知道调用谁,通过user调,定义的东西就该放到user中,
定义一个接口数组:
NormalUser中,构造方法,通过this访问 ioPerations
给该数组直接初始化,数组元素为普通用户中各种操作的类
相当于在子类里面确定了大小,同时初始化了该数组
只要这行代码执行完,一定会知道数组中有哪些操作
同样,在 AdminUser 中,初始化该数组,如下:
结论:在调用子类对象的时候,构造方法会初始化好对应的操作对象
具体调用:
main函数中目前四行代码的具体实现分析:
具体功能实现
下面实现具体功能
退出系统:
设置状态码为0即可
书架类BookList其实就是一个数组
我们初始化书架有三本书,通过访问其下标即可显示图书
通过BookList类中的useSize作为数组有效访问长度,因为useSize修饰符为private
所以需创建get、set方法
来遍历BookList类中的books数组,该数组也是private所修饰,也需get、set方法
编译器自动生成如下:
getBooks不符合我们的需求,我们想要获取某一个下标的数组,这里返回的是成员变量boosk
改为以下:返回pos下标的书,这里不考虑pos不合法的情况
setBooks也不符合需求,我们要往某一个下标去放一本书,改为如下:往pos下标放一本书
查找图书
根据书名查找
运行查找“三国演义”
修改重写的toString方法,使用三目运算符改为借出、未借出
新增图书
将新增图书放到数组(顺序表)的最后即可,即放到下标为 useSize 的位置
第一步:判满
第二步:输入各种成员变量,构建对象
第三步:放到数组最后一个位置
第四步:useSize++
public class AddOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("新增图书。。。");//1、判满int currentSize = bookList.getUseSize();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();scanner.nextLine();System.out.println("请输入书的类型:");String type = scanner.nextLine();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("有这本书,不能插入!");System.out.println(book);return;}}//4、插入新增书bookList.setBook(currentSize,newBook);bookList.setUseSize(currentSize+1);System.out.println("新增图书成功!");}
}
具体实现分析
1、book数组由private修饰,无法被外部访问,所以写一个get方法
定义变量currentSize接收当前数组实际占用空间
获取数组长度与实际占用空间相比较
2、构建书对象
3、遍历数组,比较有无同名书
4、新增图书
将书添加到数组中currentSize下标处,currentSize+1
普通用户专属借阅图书:
归还图书:
删除图书
本章示例代码
book包
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) ? " 已借出" : " 未借出") +//", isBorrowed=" + isBorrowed +'}';}
}package book;public class BookList {private Book[] books = new Book[10];private int useSize;//有效的数据个数【实际存放书的数量】public BookList() {books[0] = new Book("三国演义","罗贯中",10,"小说");books[1] = new Book("西游记","吴承恩",20,"小说");books[2] = new Book("红楼梦","曹雪芹",30,"小说");this.useSize = 3;}public int getUseSize() {return useSize;}public void setUseSize(int useSize) {this.useSize = useSize;}public Book getBook(int pos) {return books[pos];}public void setBook(int pos, Book book) {this.books[pos] = book;}public Book[] getBooks() {return books;}
}
ioperations包
package ioperations;import book.BookList;import java.util.Scanner;import book.Book;
public class AddOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("新增图书。。。");//1、判满int currentSize = bookList.getUseSize();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();scanner.nextLine();System.out.println("请输入书的类型:");String type = scanner.nextLine();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("有这本书,不能插入!");System.out.println(book);return;}}//4、插入新增书bookList.setBook(currentSize,newBook);bookList.setUseSize(currentSize+1);System.out.println("新增图书成功!");}
}package ioperations;import book.Book;
import book.BookList;import java.util.Scanner;public class BorrowedOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("借阅图书。。。");Scanner scanner = new Scanner(System.in);System.out.print("请输入要借阅的书名:");String name = scanner.nextLine();int currentSize = bookList.getUseSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(name)){if(book.isBorrowed()) {System.out.println("这本书已借出!");return;}book.setBorrowed(true);System.out.println("借阅成功!");return;}}System.out.println("没找到你要借阅的书。。。");}
}package ioperations;import book.Book;
import book.BookList;import java.util.Scanner;public class DelOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("删除图书。。。");Scanner scanner = new Scanner(System.in);System.out.print("请输入你要删除的书名:");String name = scanner.nextLine();int currentSize = bookList.getUseSize();int pos = -1;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("没有你要删除的书!");return;}for(int j = pos;j < currentSize-1;j++){//错误示例://bookList[j] = bookList[j+1];Book book = bookList.getBook(j+1);bookList.setBook(j,book);}bookList.setBook(currentSize-1,null);bookList.setUseSize(currentSize-1);System.out.println("删除成功!");}
}package ioperations;import book.BookList;public class ExitOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("退出系统。。。");System.exit(0);}
}package ioperations;import book.Book;
import book.BookList;import java.util.Scanner;public class FindOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("查找图书。。。");Scanner scanner = new Scanner(System.in);System.out.print("请输入要查找的书名:");String name = scanner.nextLine();int currentSize = bookList.getUseSize();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("没找到你要查找的书。。。");}
}package ioperations;import book.BookList;public interface IOPeration {void work(BookList bookList);
}package ioperations;import book.Book;
import book.BookList;import java.util.Scanner;public class ReturnOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("归还图书。。。");Scanner scanner = new Scanner(System.in);System.out.print("请输入要归还的书名:");String name = scanner.nextLine();int currentSize = bookList.getUseSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(name)){if(book.isBorrowed()) {book.setBorrowed(false);System.out.println("归还成功!");return;}}}System.out.println("没有你要归还的图书!");}
}package ioperations;import book.Book;
import book.BookList;public class ShowOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("显示图书。。。");int currentSize = bookList.getUseSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);System.out.println(book);//错误示例://Book book = bookList[i];}}
}
user包
package user;import ioperations.*;import java.util.Scanner;public class AdminUser extends User {public AdminUser(String name) {super(name);this.ioPerations = new IOPeration[]{new ExitOperation(),new FindOperation(),new AddOperation(),new DelOperation(),new ShowOperation()};}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("4. 显示图书");System.out.println("0. 退出系统");System.out.println("********************");Scanner scanner = new Scanner(System.in);System.out.println("请输入你的操作:");int choice = scanner.nextInt();return choice;}
}package user;import ioperations.*;import java.util.Scanner;public class NormalUser extends User {public NormalUser(String name) {super(name);this.ioPerations = new IOPeration[]{new ExitOperation(),new FindOperation(),new BorrowedOperation(),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;}
}package user;import book.BookList;
import ioperations.IOPeration;public abstract class User {protected String name;//此时这个数组没有初始化protected IOPeration[] ioPerations;public User(String name) {this.name = name;}public abstract int menu();public void doIoperation(int choice, BookList bookList){ioPerations[choice].work(bookList);}
}
Main函数
import book.BookList;
import ioperations.IOPeration;
import user.AdminUser;
import user.NormalUser;
import user.User;import java.util.Scanner;public class Main {public static User login() {Scanner scanner = new Scanner(System.in);System.out.println("请输入你的姓名:");String name = scanner.nextLine();System.out.println("请输入你的身份,1:管理员 2:普通用户");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.doIoperation(choice, bookList);}}
}
扩展功能
将书籍数据存储到文件中
将书籍数据存储到MySQL中
加入网页、框架,实现web端
功能上按书名、作者名、价格等排序