在做一个项目的时候,先要搭建项目的架构,首先分析,图书管理系统,需要有书,人,操作等对象,我们将其分为
在User中编写构造方法以及用户菜单
public abstract class User {
public String name;
//定义构造方法
public User(String name) {
this.name = name;
}
public abstract void menu();
}
对应的在AdminUser和NomalUser中继承User并编写构造方法,搭建不用用户菜单框架
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
}
@Override
public void menu() {
System.out.println("普通用户的菜单");
}
}
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
}
@Override
public void menu() {
System.out.println("管理员的菜单");
}
}
提供属性,构造方法,get和set方法,以及打印的toString方法
package 图书管理.book;
/**
* Created with IntelliJ IDEA.
* Description:
* User: starry
* Date: 2021 -01 -20
* Time: 18:08
*/
public class Book {
/**
*图书的所有属性
*/
private String name; //图书名称
private String author; //作者
private int price; //价格
private String type; //类型
private boolean status; //借出状态:默认false
/**
* 创建构造方法
* @param name
* @param author
* @param price
* @param type
*/
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
/**
* 既然封装了起来,编写get和set方法之后来调用Book中的属性
* @return
*/
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 isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
/**
* 提供打印所有属性的toString方法
* @return
*/
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", status=" + status +
'}';
}
}
把书存入数组中,相当于放置在书架上
public class BookList {
private Book[] books; //图书对象,存放在顺序表中
private int usedSize;
public BookList(){
this.books = new Book[10];
books[0] = new Book("三国演义","罗贯中",55,"小说");
books[1] = new Book("西游记","吴承恩",74,"小说");
books[2] = new Book("水浒传","施耐庵",49,"小说");
this.usedSize = 3;
}
//尾插法
public void setBooks(int pos,Book book) {
this.books[pos] = book;
}
public Book getBook(int pos) {
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
先不着急实现每个操作类的具体工作,先搭建框架
public interface IOperation {
void work(BookList bookList);
}
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("添加书籍");
}
}
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅书籍");
}
}
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除书籍");
}
}
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("展示书籍");
}
}
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
}
}
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找书籍");
}
}
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还书籍");
}
}
public class Main {
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名");
String name = scanner.nextLine();
System.out.println("请输入你的身份:1-》管理员 0-》普通用户");
int choice = scanner.nextInt();
if(choice == 1) {
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
//1.登录
User user = login(); //向上转型
user.menu(); //动态绑定
}
}
现在我们已经实现了大概的程序框架,接下来就需要把每一部分完善了
public abstract class User {
public String name;
//定义构造方法
public User(String name) {
this.name = name;
}
public abstract int menu();
}
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
}
/**
* 返回 你要执行的对应的操作
* @return
*/
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("管理员的菜单");
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("==============================");
int choice = scanner.nextInt();
return choice;
}
}
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
}
/**
* 返回 你要执行的对应的操作
* @return
*/
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("普通用户的菜单");
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("==============================");
int choice = scanner.nextInt();
return choice;
}
}
public abstract class User {
public String name; //用户名
public IOperation[] operations; //对应用户的操作数组
//定义构造方法
public User(String name) {
this.name = name;
}
//用户菜单
public abstract int menu();
//调用对用操作的work方法
public void doOperation(int choice, BookList bookList) {
this.operations[choice].work(bookList);
}
}
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.operations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
}; //调用不同功能
}
/**
* 返回 你要执行的对应的操作
* @return
*/
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("管理员的菜单");
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("==============================");
int choice = scanner.nextInt();
return choice;
}
}
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.operations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
}; //调用不同功能
}
/**
* 返回 你要执行的对应的操作
* @return
*/
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("普通用户的菜单");
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("==============================");
int choice = scanner.nextInt();
return choice;
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
//1.登录
User user = login(); //向上转型
while (true) {
//拿choice去调用对应类中的work方法
int choice = user.menu(); //动态绑定
user.doOperation(choice,bookList);
}
}
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("添加书籍");
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();
System.out.println("请输入图书的类型");
String type = scanner.next();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
//放在书架有的书后面
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
}
}
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("输入要查找的书名");
String name = scanner.nextLine();
for(int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("有这本书!");
System.out.println(book);
return;
}
}
System.out.println("没有这本书!");
}
}
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("展示书籍");
for(int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要借阅的书名");
String name = scanner.nextLine();
for(int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
//说明有这本书
//逻辑:把这本书的借阅状态修改为true
book.setStatus(true);
System.out.println("借阅成功!");
System.out.println(book);
return;
}
}
System.out.println("借阅失败,没有你要借阅的图书");
}
}
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(1);
}
}
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的书名");
String name = scanner.nextLine();
int i;
for(i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
break;
}
}
if(i >= bookList.getUsedSize()) {
System.out.println("没有你要删除的这本书!");
return;
}
int pos = i;
for(int j = pos; j < bookList.getUsedSize()-1; j++) {
Book book = bookList.getBook(j+1);
bookList.setBooks(j,book);
}
int currentSize = bookList.getUsedSize();
bookList.setUsedSize(currentSize-1);
System.out.println("删除书籍完毕!");
}
}
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要归还的书名");
String name = scanner.nextLine();
for(int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
//说明有这本书
//逻辑:把这本书的借阅状态修改为false
book.setStatus(false);
System.out.println("归还成功!");
System.out.println(book);
return;
}
}
System.out.println("归还失败,没有你要归还的图书");
}
}