涉及知识:
1.类 2.对象 3.抽象类 4.接口 5.封装 6.继承 7.多态 8.顺序表
图书管理界面分为管理员界面和普通用户界面,选择对象不同具体可实现的功能也不相同. 而要实现这看似简单的操作需要对封装,继承,多态,接口相关知识点熟练掌握.(本篇文字的主要目的是熟练掌握java的封装,继承,多态等知识的灵活运用,不对业务上的处理过于深究.感兴趣的朋友可以自行拓展.)
首先, 第一步我们要创建好项目所需的包(package)来一方面保证类的唯一性, 另一方面使代码的可读性更强.
1.book包,该包主要储存两个类,Book和BookList. Book类中存放图书的
name(名称),author(作者),price(价格),type(类型)以及是否借出(isBorrowed), 这里体现了封装的思想.将书的一切属性设为private, 防止非管理人员对其更改,当我们创建好私有属性后, 其余代码均可有编译器自身来生成. 例如生成构造方法, 右击Gengerate.
点击Constructor(构造方法)
之后按ctrl+A全选,即可生成所有属性构造方法.
为了便于管理员后续添加更改书籍, 我们要引入公开的get和set方法让类外来访问它,同样由编译器来完成,选择Getter and Setter.即可生成所有方法.
这样一来仅仅几步就可生成60多行代码,体现了java语言的优势.
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;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + ''' +
", author='" + author + ''' +
", price=" + price +
", type='" + type + ''' +
", isBorrowed=" + isBorrowed +
'}';
}
}
创建好书的属性后我们需完成 2.book包底下的BookList类,该类的主要目的是把书籍的相关属性存于顺序表中,方便用于之后的增,删,查,改,等操作.
public class BookList {
private Book[] books=new Book[10];
private int usedsize;
public BookList(){
books[0]=new Book("三国演义","罗贯中",17,"小说");
books[1]=new Book("水浒传","施耐庵",17,"小说");
books[2]=new Book("java","bates",17,"study");
this.usedsize=3;
}
public int getUsedsize() {
return usedsize;
}
public void setUsedsize(int usedsize) {
this.usedsize = usedsize;
}
public Book getPos(int pos){
return this.books[pos];
}
public void setBooks(int pos,Book book){
this.books[pos]=book;
}
}
在顺序表中创建私有的属性存放书籍的属性和书籍的数目(usedsize),创建完成后同样调用Getter and Setter方法便于后续修改,但此时只生成与usedsize有关的.对于图书属性的操作我们采用其他方法.
通过getPos方法我们获取到pos位置的一本书,而setBooks方法我们向pos位置添加一本书.
完成这些操作之后书籍的增删查改等方法我们都通过面向对象的方式来进行调用.将这些所有的方法都单独的创建为一个类,每一个类都有其特定的方法.
然后再定义一个IOperation接口为后续的多态做准备,在Ioperation接口中我们定义一个抽象方法work,所有使用该接口的类都要重写这个方法,在AddOperation(新增图书)中,我们重写work方法,依照上述方法对每一个图书管理操作都复制粘贴.不过要将"新增图书"作出更改.
public interface IOperation {
void work(BookList bookList);
}
接下来我们实现管理员类和用户类,User为AdminUser和NormallUser的父类,用于抽取二者的共性,防止代码冗余.
User中定义命名方法,protected修饰表示可以在所有子类中访问.
public abstract class User {
protected String name;
之后我们知道管理员和用户除了有名字以外,还后各自独立的菜单模式.接下来我们定义他们各自的方法.
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
}
public void menu(){
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("============================");
}
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
}
public void menu(){
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("============================");
}
完成上述操作之后,需要一个Main方法来实现上述操作.例如输入姓名,输入身份,和登录(login)后显示哪种菜单.那么为什么login()方法的返回类型是User呢?因为我们要返回AdminUser还是NormallUser不能确定,所以只能返回到其父类方法.后续通过父类"向上转型"去调用子类的方法,正是多态的体现.
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 choice=scanner.nextInt();
if(choice==1){
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
接着我们在主方法中实现上述操作.user引用谁取决于login返回谁(这里体现了向上转型的思想) user要想调用menu,必须在User 类中定义menu方法,具体内容由子类来实现(这里体现了动态绑定的思想),而User中的menu方法既然不需实现,可以用abstract来修饰其为抽象方法.User也相应可以定义为抽象类.
public static void main(String[] args) {
User user=login();//向上转型
user.menu();//动态绑定
}
public abstract class User {
protected String name;
public User(String name){
this.name=name;
}
public abstract int menu();
}
定义完菜单之后,我们需要获取对菜单的操作值,这时改变菜单的返回类型为int.
public int menu(){
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("============================");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
用户菜单也是类似的操作这里不再赘述.
用choice来接收menu 的返回值..
public static void main(String[] args) {
User user=login();//向上转型
int choice=user.menu();//动态绑定
}
那么我们如何将这些操作组装在一起呢?首先我们在User中定义一个数组Ioperation[],接着在子类的构造方法中将这些操作全部存在数组中.那么我们为什么要用Ioperation接口来定义数组呢?因为接口中有work方法可以调用.
public abstract class User {
protected String name;
public User(String name){
this.name=name;
}
public abstract int menu();
protected IOperation[] iOperations;
}
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperations=new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
};
}
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.iOperations=new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
当我们把这些操作的对象存进数组之后,每个对象都有其具体的实现方法,此时我们定义一个doWork方法,通过数组的下标访问对应的work方法.此时我们之前定义的接口Ioperation就起了很大的作用.
public abstract class User {
protected String name;
public User(String name){
this.name=name;
}
public abstract int menu();
protected IOperation[] iOperations;
public void doWork(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();//向上转型
while (true) {
int choice = user.menu();
user.doWork(choice, bookList);
}
}
具体实现逻辑通过画图来看更加直观.首先在main函数中实例化BookList对象,便于后续调用其中方法.接着user接收到login返回的数字,调用对应的菜单.当菜单中出现一连串操作方法后,我们在User类中定义一个Ioperation数组用于存放这些操作方法的对象 ,然后在两个子类中将其对应的操作方法都存于数组中.最后在User类中定义doWork方法,该方法用于访问数组下标对应的操作方法,此时Ioperation接口中的work方法起到了关键作用.相对应在main函数中我们就可调用doWork方法
通过上述操作,我能对图书管理代码的各类操作与账户菜单的之间的联系都已完成,接下来就是具体操作内容的实现需要借助顺序表的知识完成增删查改等操作.
首先我们来完成Addoperation(新增图书)操作,这里我们默认新增图书在最后一个,对于业务上的处理不做深究.在Addoperation(新增图书)操作中我们输入图书的各种属性,为了使代码简便我们把 Scanner scanner = new Scanner(System.in);操作放于接口中,剩下只需输入图书属性即可.
public interface IOperation {
Scanner scanner=new Scanner(System.in);
void work(BookList bookList);
}
写好图书属性之后,我们要将这些属性传给Book类,然后调用getUsedsize方法得到顺序表的长度,最后调用setBook方法在在最后一位放置图书.(新增图书成功!!)
public class AddOperation implements IOperation {
public void work(BookList bookList){
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 size=bookList.getUsedsize();
bookList.setBooks(size,book);
bookList.setUsedsize(size+1);
System.out.println("新增图书成功");
}
}
DispalyOperation(打印图书),借助for循环遍历顺序表中的每一个元素.
public class DisplayOperation implements IOperation {
public void work(BookList bookList){
System.out.println("打印图书");
int size=bookList.getUsedsize();
for(int i=0;i
为了使打印内容更加好看,我们对Book类中的Tostring 方法稍作修改.
@Override
public String toString() {
return "Book{" +
"name='" + name + ''' +
", author='" + author + ''' +
", price=" + price +
", type='" + type + ''' +
((isBorrowed==true) ?" 已经被借出了 ":" 未被借出 ")+
'}';
}
}
FindOperation(查找图书).
public class FindOperation implements IOperation {
public void work(BookList bookList){
System.out.println("查找图书");
String name=scanner.nextLine();
int size=bookList.getUsedsize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
if(name.equals((book.getName()))){
System.out.println("找到了这本书,信息如下:");
System.out.println(book);
return;
}
}
System.out.println("没有找到这本书!");
}
}
ExitOperation(退出系统)
public class ExitOperation implements IOperation {
public void work(BookList bookList){
System.out.println("退出系统");
}
}
DelOperation(删除图书),首先根据书名找到图书的位置index,之后然后面的书覆盖前一位书即可,不要忘记删除后顺序表大小要-1.
public class DelOperation implements IOperation {
public void work(BookList bookList){
System.out.println("删除图书");
//1.根据书名找到书的位置 index
System.out.println("请输入你要删的书名");
String name=scanner.nextLine();
int index=0;
int currentsize= bookList.getUsedsize();//当前大小
int i = 0;
for (; i < currentsize; i++) {
Book book=bookList.getPos(i);
if(book.getName().equals(name)){//比较是否相等
index=i;
break;
}
}
if (i>=currentsize){
System.out.println("没有你要找的这本书");
return;
}
//2.进行删除
for (int j = index; j < currentsize-1; j++) {
Book book=bookList.getPos(j+1);//将j+1下标的图书属性由book引用,然后传到j下标.
bookList.setBooks(j,book);
}
bookList.setBooks(currentsize,null);
bookList.setUsedsize(currentsize-1);
System.out.println("删除成功");
}
}
BorrowOperation(借阅图书),实现方法与查找图书类似,不过要将setBorrowed改为true.正好与之前Tostring方法中的条件操作符相呼应.
ReturnOperation(归还图书),方法与归还图书基本一致.
public class BorrowOperation implements IOperation {
public void work(BookList bookList) {
System.out.println("新增图书");
System.out.println("请输入你要借阅图书的名字");
String name=scanner.nextLine();
int size=bookList.getUsedsize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
if(name.equals((book.getName()))){
book.setBorrowed(true);
System.out.println("借阅成功");
System.out.println(book);
return;
}
}
System.out.println("没有你要借阅的书!");
}
}
public class ReturnOperation implements IOperation {
public void work(BookList bookList){
System.out.println("归还图书");
System.out.println("请输入你要归还的图书");
String name=scanner.nextLine();
int size=bookList.getUsedsize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
if(name.equals((book.getName()))){
System.out.println("归还成功");
System.out.println(book);
return;
}
}
System.out.println("没有你要归还的书!");
}
}
以上就是图书管理代码的全部操作逻辑了,值得注意的是各种各样的类与对象可能会把大家搞晕,所以建议读代码从main函数开始,main函数就像是树干其余类和方法只是书的分支和叶子.可能在业务上无法满足更多的要求,但本篇文章的初衷是对封装,继承,多态,接口相关知识点熟练掌握.不足之处希望大家海涵并提出宝贵意见!



