文章目录
⭐构建框架
首先我们必须很清楚最后实现出的结果是什么样的
有管理员和普通用户两个选择。普通用户和管理员显示的菜单是不一样的。
先新建一个Main类,里面实现菜单,调用方法,程序的主逻辑。
然后新建一个book包。新建user包,里面新建User类(父类),AdminUser类(子类),NormalUser类(子类),AdminUser类和NormalUser类这两个类都要继承User这个父类。
用户要有姓名属性。
public class User {
protected String name;
public User(String name){
this.name=name;
}
}
public class AdminUser extends User{
public AdminUser(String name) {
super(name);//先帮助父类构造
}
public class NormalUser extends User{
public NormalUser(String name) {
super(name);//先帮助父类构造
}
构造一个book对象,有name,author,price,type,是否被借出这5个属性。
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;
}
这里不初始化isBorrowed,默认是false,未被借出
因为这些属性都是private修饰,所以调用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;
}
为了以后方便打印书,再重写一个toString()
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +", "+
((isBorrowed==true)?"该书已借出":"该书未借出")+
'}';
}
现在需要一个书架放很多书,创建一个数组作为书架,以后的显示书,删除书,新增书都是对这个数组做操作。
在book包里面新建BookList书架,假设默认能放10本书,还要知道当前书架有几本书。
public class BookList {
private Book[] books=new Book[10];
private int usedSize;//存储当前书的个数
前面显示图书默认显示3本书,那么就要在程序运行的时候往书架里存三本书。
可以在调用BookList的构造方法时,存3本书。
public BookList(){
books[0]=new Book("三国演义","罗贯中",89,"小说");
books[1]=new Book("西游记","吴承恩",78,"小说");
books[2]=new Book("红楼梦","曹雪芹",49,"小说");
this.usedSize=3;
}
接下来我们需要写查找,新增,删除,显示,借阅,归还,退出这些操作,这些是一种标准/规范,我们可以把它们写成接口。
在src底下新建operations包,包里新建AddOperation(新增图书)等等操作。
public class AddOperation {
public void work(BookList bookList){
System.out.println("新增图书!");
}
}
public class FindOperation {
public void work(BookList bookList){
System.out.println("查找图书!");
}
}
可以发现新增图书和查找图书里面的work方法都是构成重写的,所以可以在operations包里新建一个IOperation接口
public interface IOperation {
void work(BookList bookList);
}
public class AddOperation implements IOperation {
public class FindOperation implements IOperation{
让AddOperation,FindOperation实现IOperation接口。
这里接口的意义:为了之后实现向上转型,动态绑定,多态
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
}
}
显示,借阅,归还,退出这些操作和上面写的类似
🎈Main类
public class Main {
public static void main(String[] args) {
BookList bookList=new BookList();
User user=login();
准备3本书,用户登录
选择1或0就知道是管理员还是普通用户,然后导出不同的菜单
(多态)
public static User login(){
System.out.println("请输入你的姓名:");
Scanner scanner=new Scanner(System.in);
String userName=scanner.nextLine();
System.out.println("请输入你的身份:1-》管理员,0-》普通用户");
int choice=scanner.nextInt();
if(choice==1){
return new AdminUser(userName);
}else{
return new NormalUser(userName);
}
}
这里返回值实现了向上转型
User user=login();
AdminUser和NormalUser都重写menu()。user引用的对象不一样,user.menu()打印的菜单不一样。
//AdminUser的menu():
public class AdminUser extends User{
public void menu(){
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("**************************************");
}
//NormalUser的menu()
public class NormalUser extends User {
public void 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("**************************************");
}
user.menu();
直接这么写会报错,因为父类User没有menu()。
所以要在父类重写一个menu()。
public abstract class User {
public abstract void menu();
//不写方法体可以写成abstract,然后父类User就是抽象类
然后是输入你的操作,接收输入的数字。
所以menu()的返回值改成int。
public int menu(){
System.out.println("请输入你的操作:");
Scanner scanner=new Scanner(System.in);
int choice =scanner.nextInt();
return choice;
}
那么怎样通过输入的数字调用相对应的用户的接口?
//在User类里定义一个数组
public abstract class User {
protected IOperation[] iOperations;
//在 AdminUser类里初始化IOperation数组
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()
};
}
user引用的是哪个对象就初始化它的IOperation数组。
接下来需要写一个操作接口数组的方法,写在User类里
public abstract class User {
public void doOperation(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
}
public class Main {
public static void main(String[] args) {
BookList bookList=new BookList();
User user=login();
while(true) {//循环输入操作,直到0退出
int choice = user.menu();
user.doOperation(choice, bookList);
}
}
}
就main函数来完整分析一遍思路,第一步初始化书架,书架里预存3本书,然后登录,输入姓名,输入身份(管理员/普通用户),new一个用户对象,初始化相对应的接口数组,返回值是父类User,向上转型,调用menu(),多态。菜单再输入操作数字,调用user的doOperation(choice, bookList),找到对应用户的接口数组的choice下标,new一个操作,调用操作里的方法。
接下来具体实现查找,新增,删除,显示,借阅,归还,退出这些操作。
⭐对图书的操作
🎈查找图书
根据书名查找,找到之后打印书的所有属性。
遍历books数组比较书名,数组长度是usedSize(private)。
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
然后遍历过程如何通过i找到books数组对应的书,books数组(private)。数组是私有所以要调用getPos()
public class BookList {
public Book getPos(int pos){
return books[pos];
}
}
public class FindOperation implements IOperation{
public void work(BookList bookList){
System.out.println("查找图书!");
System.out.println("请输入你要查找的图书的名字:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize= bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){//比较书名
System.out.println("找到了这本书!");
System.out.println(book);
return ;
}
}
System.out.println("没有这本书!");
}
}
🎈新增图书
public class AddOperation implements IOperation {
public void work(BookList bookList){
System.out.println("新增图书!");
System.out.println("请输入你要新增的图书的名字:");
Scanner scanner=new Scanner(System.in);
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 book=new Book(name,author,price,type);
现在需要把这本书放到书架上。
再books数组usedSize下标处放一本书,然后usedSize++。
public class BookList {
public void setBooks(Book book,int pos){
books[pos]=book;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
public class AddOperation implements IOperation {
public void work(BookList bookList){
int currentSize= bookList.getUsedSize();
bookList.setBooks(book,currentSize);
bookList.setUsedSize(currentSize+1);
System.out.println("新增成功");
}
}
🎈显示图书
public class DisplayOperation 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.getPos(i);
System.out.println(book);
}
}
}
🎈退出系统
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
🎈删除图书
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
System.out.println("请输入你要删除的图书:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int index=-1;
int currentSize= bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
index=i;
break;
}
}
if(index==-1){
System.out.println("没有你要删除的书!");
return;
}
如果找到了要删除的书,删除的方法是后面覆盖前面,最后usedSize–。
for (int i = index; i <currentSize-1; i++) {
Book book=bookList.getPos(i+1);
bookList.setBooks(book,i);
}
bookList.setBooks(null,currentSize-1);
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功!");
每次删除都要置空,在Java中一个对象只有没人引用的情况下才会被回收。
🎈借阅图书
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅书籍!");
System.out.println("请输入你要借阅的图书的名字:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize= bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
if(book.isBorrowed()){
System.out.println("该书已经被借出!");
}else{
book.setBorrowed(true);
System.out.println("借阅图书成功!");
}
return;
}
}
System.out.println("没有你要借阅的书!");
}
}
🎈归还图书
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("请输入你要归还的图书的名字:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize= bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
book.setBorrowed(false);
System.out.println("归还图书成功!");
return;
}
}
System.out.println("没有你要归还的书!");
}
}
⭐完整代码
🎈book包
🔥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;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +", "+
((isBorrowed==true)?"该书已借出":"该书未借出")+
'}';
}
}
🔥BookList
package operations;
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 scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize= bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
book.setBorrowed(false);
System.out.println("归还图书成功!");
return;
}
}
System.out.println("没有你要归还的书!");
}
}
🎈operations包
🔥AddOperation
package operations;
import book.Book;
import book.BookList;
import com.sun.deploy.panel.IProperty;
import java.util.Scanner;
public class AddOperation implements IOperation {
public void work(BookList bookList){
System.out.println("新增图书!");
System.out.println("请输入你要新增的图书的名字:");
Scanner scanner=new Scanner(System.in);
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();
int currentSize= bookList.getUsedSize();
Book book=new Book(name,author,price,type);
bookList.setBooks(book,currentSize);
bookList.setUsedSize(currentSize+1);
System.out.println("新增成功");
}
}
🔥BorrowOperation
package operations;
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 scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize= bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
if(book.isBorrowed()){
System.out.println("该书已经被借出!");
}else{
book.setBorrowed(true);
System.out.println("借阅图书成功!");
}
return;
}
}
System.out.println("没有你要借阅的书!");
}
}
🔥DelOperation
package operations;
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 scanner=new Scanner(System.in);
String name=scanner.nextLine();
int index=-1;
int currentSize= bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
index=i;
break;
}
}
if(index==-1){
System.out.println("没有你要删除的书!");
return;
}
for (int i = index; i <currentSize-1; i++) {
Book book=bookList.getPos(i+1);
bookList.setBooks(book,i);
}
bookList.setBooks(null,currentSize-1);
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功!");
}
}
🔥DisplayOperation
package operations;
import book.Book;
import book.BookList;
public class DisplayOperation 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.getPos(i);
System.out.println(book);
}
}
}
🔥ExitOperation
package operations;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
🔥FindOperation
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
public void work(BookList bookList){
System.out.println("查找图书!");
System.out.println("请输入你要查找的图书的名字:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize= bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
System.out.println("找到了这本书!");
System.out.println(book);
return ;
}
}
System.out.println("没有这本书!");
}
}
🔥IOperation
package operations;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
🔥ReturnOperation
package operations;
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 scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize= bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
book.setBorrowed(false);
System.out.println("归还图书成功!");
return;
}
}
System.out.println("没有你要归还的书!");
}
}
🎈user包
🔥Adminuser
package user;
import operations.*;
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 DisplayOperation()
};
}
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("4.显示图书");
System.out.println("0.退出系统");
System.out.println("**************************************");
System.out.println("请输入你的操作:");
Scanner scanner=new Scanner(System.in);
int choice =scanner.nextInt();
return choice;
}
}
🔥Normaluser
package user;
import operations.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.iOperations=new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
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 scanner=new Scanner(System.in);
int choice =scanner.nextInt();
return choice;
}
}
🔥User
package user;
import book.BookList;
import operations.IOperation;
public abstract class User {
protected String name;
protected IOperation[] iOperations;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
}
🎈Main类
import book.BookList;
import user.AdminUser;
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 userName=scanner.nextLine();
System.out.println("请输入你的身份:1-》管理员,0-》普通用户");
int choice=scanner.nextInt();
if(choice==1){
return new AdminUser(userName);
}else{
return new NormalUser(userName);
}
}
public static void main(String[] args) {
BookList bookList=new BookList();
User user=login();
while(true) {
int choice = user.menu();
user.doOperation(choice, bookList);
}
}
}