图书管理系统
1. 搭建框架
当我们拿到一个项目时不能直接就进行代码实现那样很容易造成逻辑混乱,所以要先对项目进行分析,整理出一个大概逻辑,然后根据大纲一步步的进行实现。
1.1 用户登录
我们的用户有两种:管理员和普通用户,由于这两个类有一些共同的特性,所以我们可以把这部分特性抽离出来写一个User类然后让这两个类分别继承User类。
1.2 书和书架
1.2.1 书
因为书有自己的特性所以我们可以把书封装成一个类,为了封装的完整性我们需要提供对应的Getter和Setter方法并且重写toString方法,这些编译器都可以帮助我们一键生成。当中由于我们对于一本书中的类容进行了编写,由于为都为私有,我们设置了它的get和set方法,同时我们也重写了toString方法,这样一本书就编写好了。
package book;
import java.util.Scanner;
public class book {
private String name;
private String auther;
private int price;
private String type;
private boolean isborrowed;//是否借出
public book(String name, String auther, int price, String type) {
this.name = name;
this.auther = auther;
this.price = price;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuther() {
return auther;
}
public void setAuther(String auther) {
this.auther = auther;
}
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 isIsborrowed() {
return isborrowed;
}
public void setIsborrowed(boolean isborrowed) {
this.isborrowed = isborrowed;
}
@Override
public String toString() {
return "book{" +
"name='" + name + '\'' +
", author='" + auther + '\'' +
", price=" + price +
", type='" + type + '\'' +
(isborrowed ? " 已借出" : " 未借出") +
'}';
}
}
1.2.2 书架
再创建一个容器把书放在里面然后在里边创建一个Book类型的数组来存放这些数据。
由于我们没有连接数据库所以就需要手动给书架初始化一些书。还需要一个count来记录书架上书的数量。
package book;
import java.awt.print.Book;
public class booklist {//书架,可以用数组代替
private book[] booklist = new book[10];
private int usedSize;//记录当前书架的书的数量
public booklist(){
booklist[0] = new book("三国演义","罗贯中",19,"小说");
booklist[1] = new book("西游记","吴承恩",29,"小说");
booklist[2] = new book("水浒传","施耐庵",8,"小说");
this.usedSize = 3;
}
public book[] getbooks() {
return books;
}
public void setBooklist(book[] books) {
this.books = books;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
1.3 操作
因为用户和管理员的权限不一样但是又有相同的部分,所以我们可以把这些功能单独写成一个类在用一个接口去约束它们。
2. 业务实现
import book.booklist;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
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) {
booklist booklist = new booklist();
User user = login();
int choice = user.menu();//分辨调用了哪个菜单
user.doOperation(choice,booklist);
}
}
3. 代码汇总
user类:
package user;
import book.booklist;
import operation.IOperation;
public abstract class User {
protected String name;
public IOperation[] iOperations;//没有分配空间
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice,booklist booklist){
this.iOperations[choice].work(booklist);
}
}
NormalUser类:
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("*******************************");
System.out.println("heiio" + this.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;
}
}
AdminUser类:
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 ShowOperation()
};
}
public int menu() {
System.out.println("管理员菜单!");
System.out.println("*******************************");
System.out.println("heiio" + 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("*******************************");
System.out.println("请输入你的操作:");
Scanner Scanner = new Scanner(System.in);
int choice = Scanner.nextInt();
return choice;
}
}
AddOperation类(书的添加):
package operation;
import book.booklist;
import java.util.Scanner;
public class AddOperation implements IOperation {
@Override
public void work(booklist booklist) {
System.out.println("新增图书!");
}
public void work(Bookrack bookrack) {
System.out.println("请输入要添加的书名,作者,价格,类型");
Scanner sc = new Scanner(System.in);
String name = sc.next();
String author = sc.next();
int price = sc.nextInt();
String type = sc.next();
Book book = new Book(name, author, price, type);
int count = bookrack.getCount();
bookrack.getBooks()[count] = book;
bookrack.setCount(count + 1);
}
}
BorrowOperation类(书的借阅):
package operation;
import book.book;
import book.booklist;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@Override
public void work(booklist booklist) {
System.out.println("借阅图书!");
}
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int count = bookrack.getCount();
for(int i = 0; i < count; i++) {
book book = bookrack.getbooks()[i];
if (name.equals(book.getName())) {
if (book.isBorrowed()) {
System.out.println("这本书被借走了");
}else {
System.out.println("请取书");
book.setBorrowed(true);
}
return;
}
}
System.out.println("没找到这本书");
}
}
DelOperation类(书的删除):
package operation;
import book.booklist;
import java.awt.print.Book;
import java.util.Scanner;
public class DelOperation<Bookrack> implements IOperation {
@Override
public void work(booklist booklist) {
System.out.println("删除图书!");//业务逻辑
}
public void work(Bookrack bookrack) {
System.out.println("请输入要删除的书名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int count = bookrack.getCount();
for (int i = 0; i < count; i++) {
Book book = bookrack.getBooks()[i];
if (name.equals(book.getName())) {
for (int j = i; j < count - 1; j++) {
bookrack.getBooks()[j] = bookrack.getBooks()[j+1];
}
bookrack.getBooks()[count - 1] = null;
bookrack.setCount(count - 1);
return;
}
}
System.out.println("没找到这本书");
}
}
ExitOperation类(退出系统):
package operation;
import book.booklist;
public class ExitOperation implements IOperation {
@Override
public void work(booklist booklist) {
System.out.println("退出系统!");
}
}
FindOperation类(图书查找):
package operation;
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 count = bookrack.getCount();
for (int i = 0; i < count; i++) {
Book book = bookrack.getBooks()[i];
if (name.equals(book.getName())) {
System.out.println(book);
return;
}
}
System.out.println("没找到这本书");
}
}
接口:
package operation;
import book.booklist;
public interface IOperation {
void work(booklist booklist);
}
ReturnOperation类(归还图书):
package operation;
import book.booklist;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(booklist booklist) {
System.out.println("归还图书!");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int count = bookrack.getCount();
for (int i = 0; i < count; i++) {
book book = booklist.getbooks()[i];
if (name.equals(book.getname())) {
if (book.isBorrowed()) {
System.out.println("还书成功");
book.setBorrowed(false);
}else {
System.out.println("该书未被借出,请检查输入是否正确");
}
return;
}
}
System.out.println("没找到这本书");
}
}
ShowOperation类(图书展示):
package operation;
import book.booklist;
public class ShowOperation implements IOperation {
@Override
public void work(booklist booklist) {
System.out.println("打印所有图书!");
}
int count = booklist.getCount();
for (int i = 0; i < count; i++) {
book book = booklist.getbooks()[i];
System.out.println(book);
}
}