⚡️前言⚡️
本篇文章是对目前Java专栏已有内容的一个总结练习,希望各位小主们在学习完面向对象的知识后,可以阅览本篇文章后,自己也动手实现一个这样的demo来加深总结应用已经学到知识并进行巩固。
博客主页: 【如风暖阳】
精品Java专栏【Javase】、【Java数据结构】
欢迎点赞 收藏 ⭐留言评论 私信必回哟本文由 【如风暖阳】 原创,首发于 CSDN
博主将持续更新学习记录收获,友友们有任何问题可以在评论区留言
博客中涉及源码及博主日常练习代码均已上传码云(gitee)
Java最主要的特征便是面向对象,这个小练习就是让我们加强对面向对象的理解,所以系统的实现肯定是通过分多个包和类来共同实现系统功能的。
该系统的通过四部分来实现:
1.book包下为图书信息的存储
2.user包下为用户的信息及操作逻辑
3.Main文件下为整个系统的逻辑实现
4.operation包下为系统功能的具体实现
首先是登陆页面,输入姓名后选择身份,然后跳转到不同的菜单(管理员和普通用户两份菜单),然后在用户进一步选择功能以后去实现对应的功能。
该系统是通过顺序表来实现的,在顺序表的数组中存储的是每一本书的信息,然后通过顺序表进行增删查改功能的实现(顺序表了解可以博主之前的文章【Java数据结构之顺序表】
书需要包含书名、作者、价格、类型、是否被借出等信息
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() {//以下getter、setter实现通过idea中的快捷键alt+insert来实现
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
//重写toString()方法是为了打印书的具体信息,如果不重写不会打印出书的具体内容
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed==true)?"已被借出":"未被借出") +
'}';
}
}
package book;
public class BookList {
private Book [] books=new Book[10];
//顺序表数组,暂时先将长度设小,随着接下来的学习,该部分还会继续改进
private int useSize;
//记录目前书的本数
public BookList() {
//在构造方法中先存储三本书的信息
books[0] = new Book("三国演义","罗贯中",17,"小说");
books[1] = new Book("西游记","吴承恩",47,"小说");
books[2] = new Book("水浒传","施耐庵",37,"小说");
this.useSize=3;
}
public int getUseSize() {
return useSize;
}
public void setUseSize(int useSize) {
this.useSize = useSize;
}
//以下两个方法为手敲(非快捷键)
public Book getPos(int pos) {
return this.books[pos];
}//得到角标为pos位置的书
public void setBook(int pos,Book book) {
this.books[pos] = book;
}//将角标为pos位置的书改为函数传参传过来的book.
}
因为使用者有管理者和普通用户两个对象,但两者多有相同属性且有细致区别的方法,所以将相同代码抽取出来创建抽象类User,再建立AdminUser和NormalUser分别继承User。
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
String name;//使用者姓名
IOperation[] iOperations;
//创建使用者操作数组,数组内用于存储多项功能
public User(String name) {
this.name = name;
}
public abstract int menu();
//两个对象的菜单不同,所以创建抽象方法
public void doWork(int choice, BookList bookList) {
iOperations[choice].work(bookList);
}//这个方法是在用户进行选择后,根据对应操作数组的选择进行不同操作
}
package user;
import operation.*;
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 " + 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值返回,调用实现对应功能
}
}
package user;
import operation.*;
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 " + this.name +" 欢迎来到这里!");
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);
int choice = scanner.nextInt();
return choice;
}
}
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 name = scanner.nextLine();
System.out.println("请输入你的身份:1-》管理员,0-》普通用户");
int choice = scanner.nextInt();
if(choice == 1) {
//根据choice的不同值,实例化不同对象并将其返回
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();//发生了动态绑定 -》 多态
//根据你的choice 调用合适的操作
user.doWork(choice, bookList);
}
}
}
因为系统有多项功能,所以在这个包下,将每一个功能都独立创建为一个类来具体清晰实现,为了这些类中的代码复用,又抽象出来一个接口(多个类共有的功能)
package operation;
import book.BookList;
import java.util.Scanner;
public interface IOperation {
Scanner scanner = new Scanner(System.in);
void work(BookList bookList);
}
新增图书需要new图书并将信息初始化,然后将其放在顺序表最后位置
package operation;
import book.Book;
import book.BookList;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
System.out.println("请输入书名:");
String name = scanner.next();
System.out.println("请输入作者:");
String author= scanner.next();
System.out.println("请输入类型:");
String type=scanner.next();
System.out.println("请输入价格:");
int price=scanner.nextInt();
Book book=new Book(name, author,price,type);
//实例化新增图书
int size= bookList.getUseSize();
//导包后调用bookList.getUseSize();函数确定新增图书存放位置
bookList.setBook(size,book);
//新增图书
bookList.setUseSize(size+1);
//有效书目加一
System.out.println("新增图书成功!");
}
}
输入借阅图书书名后遍历数组查找,找到后将图书借阅情况更改
package operation;
import book.Book;
import book.BookList;
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
System.out.println("请输入要借阅图书的名字");
String name=scanner.nextLine();
int size=bookList.getUseSize();
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("没有你要借阅的书");
}
}
package operation;
import book.Book;
import book.BookList;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
System.out.println("请输入要删除的图书");
String name=scanner.next();
int size= bookList.getUseSize();
//设置循环先进行书目查找
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
//找到后数组前移覆盖此书
if (name.equals(book.getName())) {
for (int j = i; j < size-1; j++) {
Book bookNext=bookList.getPos(j+1);
bookList.setBook(j,bookNext);
}
bookList.setBook(size,null);
bookList.setUseSize(size-1);
//删除书目后有效书目-1;
System.out.println("删除完成");
return;
}
}
System.out.println("该书不存在");
}
}
把已有书目信息全部打印
package operation;
import book.Book;
import book.BookList;
import java.util.Arrays;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("浏览图书!");
int size= bookList.getUseSize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
System.out.println(book);
}
}
}
退出系统
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
package operation;
import book.Book;
import book.BookList;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请输入要查找的图书:");
String name=scanner.next();
int size= bookList.getUseSize();
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("没找到");
}
}
package operation;
import book.Book;
import book.BookList;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("请输入要归还图书的名字");
String name=scanner.nextLine();
int size=bookList.getUseSize();
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("没有你要归还的书");
}
}
⚡️最后的话⚡️
总结不易,希望uu们不要吝啬你们的哟(^U^)ノ~YO!!如有问题,欢迎评论区批评指正