目录
前言
Hi~ 你好!
欢迎点击我的博客 : )
这是我的学习总结,希望也能帮助到你
我的博客地址:hi~,我在这里~,欢迎关注哦,
三连不迷路,优质内容持续更新!
项目结构:
book包:
1.book类
package book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;//是否被借出,不用写进构造方法,因为默认为false没被借出
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 +
(isBorrowed == true ? " 已借出" : " 未借出") +
'}';
}
}
book类的javabeen /构造方法/重写toSpring方法
private boolean isBorrowed;//是否被借出,不用写进构造方法,因为默认为false没被借出
2.bookList类
package book;
public class BookList {
private static final int DEFAULT_SIZE = 10;//设置常量
private Book[] books = new Book[DEFAULT_SIZE];
private int usedSize;//记录当前books数组里有多少本书
public BookList() {
books[0] = new Book("水浒传", "施耐庵", 66, "小说");
books[1] = new Book("人生的智慧", "叔本华", 44, "哲学 ");
books[2] = new Book("局外人", "加缪", 30, "荒诞小说");
this.usedSize = 3;
}
public Book getBook(int pos){
return this.books[pos];
}
public void setBook(Book book) {
//books[pos] = book; //往books数组里放书
this.books[usedSize]=book;
}
public void setBook(int pos,Book book){
this.books[pos] =book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
通过构造方法将书存进数组
user包:
1.User抽象类
package user;
import book.BookList;
import opera.IOperation;
public abstract class User {//设置成抽象类
protected String name;
protected IOperation []iOperation;//定义一个接口数组
public User(String name) {
this.name = name;
}
public abstract int menu();//设置成抽象方法
public void doWork(int choice, BookList bookList){
this.iOperation[choice].work(bookList);//引用对象调用work方法
}
}
user父类
2.AdminUser类
package user;
import opera.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperation = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation(),
};
}
public int menu(){//接受的返回值类型为int,即为choice的值
System.out.println("===============");
System.out.println("hello "+name+ " 登录图书管理系统");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("===============");
System.out.println("请输入你的操作");
Scanner sc= new Scanner(System.in);
int choice = sc.nextInt();
return choice;//返回要输入的操作值
}
}
管理员
3.NormalUser类
package user;
import opera.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperation = new IOperation[]{//动态扩容数组,静态只能在定义的时候,这里的引用
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),//根据菜单的顺序,依次new对应的操作对象放进接口数组
};
}
@Override
public int menu() {
System.out.println("===============");
System.out.println("hello "+name+ " 登录图书管理系统");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("===============");
System.out.println("请输入你的操作");
Scanner sc= new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
}
普通用户
opera包
1.IOperation接口
package opera;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
通过Ioperation来实现不同的操作
2.AddOperation类
package opera;
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 sc = new Scanner(System.in);
System.out.println("输入书名");
String name = sc.nextLine();
System.out.println("输入作者");
String author = sc.nextLine();
System.out.println("输入价格");
int price = sc.nextInt();
System.out.println("输入类型");
String type = sc.nextLine();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
if (tmp.getName().equals(name)){
System.out.println("这本书已存在,不用重复存放");
return;
}
}
bookList.setBook(book);//把书放进books数组
bookList.setUsedSize(currentSize+1); //修改usedSize
}
}
添加图书,遍历数组,先判书是否存在于数组中,然后将新加的书放进books数组,修改存放的值
3.BorrowOperation类
package opera;
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.println("输入你要借阅的图书");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name) && !book.isBorrowed()) {
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("没有这本书");
}
}
借阅:遍历数组,先寻找到要借阅的书,并且书要处于未被借阅的状态
4.DelOperation类
package opera;
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.println("请输入你要删除的书名");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
int index = -1;
for (int i = 0; i < currentSize; i++) {//找到图书
Book tmp = bookList.getBook(i);
if (tmp.getName().equals(name)) {
index = i;
break;
}
}
for (int j = index; j < currentSize - 1; j++) {//移动数据
Book book = bookList.getBook(j + 1);
bookList.setBook(j, book);
}
bookList.setUsedSize(currentSize - 1);
bookList.setBook(currentSize-1,null);//删除的是对象,把最后一个置为null
}
}
删除图书:先遍历数组找到图书,记录索引下标,然后移动数组,把要删除的数后面的数向前一位移动,最后删除对象,将最后一置为null
5.ExitOperation类
package opera;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0);
}
}
System.exit(0),传递的参数为0,直接退出
6.FindOperation类
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("查找图书");
System.out.println("请输入要查找的书名");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
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("没有这本书");
}
}
查找图书,遍历数组,寻找数组中是否存在要找的书
7.ReturnOperation类
package opera;
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.println("请输入你要归还的图书");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name) && book.isBorrowed()) {
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("归还失败");
}
}
归还图书,同样遍历数组当输入的值存在于数组中,并且已被借出,返回
8.ShowOperation类
package opera;
import book.Book;
import book.BookList;
public class ShowOperation implements IOperation{
@Override
public 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);
}
}
}
遍历数组,打印拿到的book对象
Main
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login() {//接受的返回值类型为User
System.out.println("请输入你的姓名");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("请输入你的身份:1-->管理员/2--->普通用户");
int choice = sc.nextInt();//输入1/2进行判断,返回并实例化不同的对象,同时接受的返回值类型为User,发生向上转型
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();//调用父类的抽象方法menu,同时子类的menu方法重写,
// 并且向上转型,发生动态绑定,调用的是拿到的子类对象的menu方法
//同时,子类的menu方法的返回值是int,由choice进行接受,接受的choice值为菜单里要进行的操作值
user.doWork(choice,bookList);
}
//System.out.println("test");
}
}
login() 接受的返回值类型为User
int choice = sc.nextInt();
输入1/2进行判断,返回并实例化不同的对象,同时接受的返回值类型为User,发生向上转型
int choice = user.menu();//调用父类的抽象方法menu,同时子类的menu方法重写,并且向上转型,发生动态绑定,调用的是拿到的子类对象的menu方法
同时,子类的menu方法的返回值是int,由choice进行接受,接受的choice值为菜单里要进行的操作值
后记
看到这里,希望能帮到你~
您的点赞 ,收藏 ,关注 是我创作的最大动力!
同时也欢迎在评论区进行交流,共同进步~