文章目录
前言
实现一个简单的图书管理系统。有管理员和普通用户两个身份。
管理员:1.查找图书,2.新增图书,3.删除图书,4.显示图书,0.退出系统
普通用户:1.查找图书,2.借阅图书,3.归还图书,0.退出系统
想要构建多个属性,按shift。
一、系统框架
1.角色:普通用户和管理员
定义user包,User角色
package user;
public class User {
private String name;
public User(String name) {
this.name = name;
}
}
普通用户
package user;
public class NomalUser extends User{
public NomalUser(String name) {
super(name);
}
}
管理员
package user;
public class AdminUser extends User{
public AdminUser(String name){
super(name);
}
}
2.行为:书和书架
设置一个book包。有Book和BookList类。
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)?"已借出":"未借出")+
// ", isBorrowed=" + isBorrowed +
'}';
}
}
书架可以用数组来代替。
package book;
public class BookList {
private Book[] books=new Book[10];
//假设能放10本书
private int useSize;//记录书架上有几本书
//在构造方法中初始化三本书
public BookList() {
books[0] = new Book("三国演义", "罗贯中", 23, "小说");
books[1] = new Book("历史", "小华", 43, "报刊");
books[2] = new Book("数学", "莉莉", 29, "课本");
this.useSize=3;
}
public Book getBook(int pos){
return books[pos];
}
public int getUseSize() {
return useSize;
}
public void setUseSize(int useSize) {
this.useSize = useSize;
}
public void setBooks(int pos,Book book) {
books[pos]=book;
}
}
3.接口标准
定义operation包,在里面定义IOperation
所有的操作都是针对bookList的,因此规范IOperation接口
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
以下是各种操作的类
新增图书:
package operation;
import book.BookList;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("新增图书");
}
}
查找图书
package operation;
import book.BookList;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书");
}
}
删除图书
package operation;
import book.BookList;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
}
}
显示图书
package operation;
import book.BookList;
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示图书");
}
}
借阅图书
package operation;
import book.BookList;
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书");
}
}
归还图书
package operation;
import book.BookList;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书");
}
}
退出系统
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
}
}
以上三部分都还是一块一块的,接下来要整合。
二、整合
在src下定义一个入口Main。实现在Main下运行时程序能够运行起来。
Main
import book.BookList;
import user.AdminUser;
import user.NomalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name=sc.nextLine();
System.out.println("请输入你的身份:1--管理员,0--普通用户");
int choice=sc.nextInt();
if(choice==1){
return new AdminUser(name);
}else{
return new NomalUser(name);
}
}
public static void main(String[] args) {
BookList bookList=new BookList();
//最终指向哪个用户
User user=login();
while (true) {
int choice = user.menu();//根据所进入菜单的choice来进行选则功能。
user.doOperation(choice, bookList);
}
}
}
用户的抽象类
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);
}
}
普通用户菜单
package user;
import operation.*;
import java.util.Scanner;
public class NomalUser extends User{
public NomalUser(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("hello"+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 sc=new Scanner(System.in);
int choice=sc.nextInt();
return choice;
}
}
管理员菜单:
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("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("*******************************");
System.out.println("请输入你的操作");
Scanner sc=new Scanner(System.in);
int choice=sc.nextInt();
return choice;
}
}
三、功能实现
查找图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书");
Scanner sc=new Scanner(System.in);
System.out.println("请输入你要查找的图书姓名:");
String name=sc.nextLine();
int currentSize=bookList.getUseSize();
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("没有你要查找的这本书!");
}
}
实现其功能要往BookList中写获得getUseSize的方法和能够利用下标返回中此数组中下标的值。
package book;
public class BookList {
private Book[] booklist=new Book[10];
//假设能放10本书
private int useSize;//记录书架上有几本书
//在构造方法中初始化三本书
public BookList() {
booklist[0] = new Book("三国演义", "罗贯中", 23, "小说");
booklist[1] = new Book("历史", "小华", 43, "报刊");
booklist[2] = new Book("数学", "莉莉", 29, "课本");
this.useSize=3;
}
public Book getBook(int pos){
return booklist[pos];
}
public int getUseSize() {
return useSize;
}
public void setUseSize(int useSize) {
this.useSize = useSize;
}
}
显示图书
与前面的查找功能类似,只是省去了判断是否在这一步,只留下遍历和显示。
package operation;
import book.Book;
import book.BookList;
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示图书");
int currentSize=bookList.getUseSize();
for(int i=0;i<currentSize;i++){
Book book=bookList.getBook(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);
}
}
新增图书
把这本书放到数组最后可以存放的位置。
注意:一定要nextLine在nextInt前,否则不能够输入下面的字符串类型
package operation;
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("请输入图书类型");
String type= sc.nextLine();
System.out.println("请输入图书价格");
int price=sc.nextInt();
Book book=new Book(name,author,price,type) ;
int currentSize=bookList.getUseSize();
bookList.setBooks(currentSize,book);
bookList.setUseSize(currentSize+1);
System.out.println("新增图书成功!");
}
}
删除图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
Scanner sc=new Scanner(System.in);
System.out.println("请输入你要删除的图书姓名:");
String name=sc.nextLine();
int currentSize=bookList.getUseSize();
int delIndex=0;
int i=0;
for(;i<currentSize;i++){
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
delIndex=i;
}
}
if(i==currentSize){
System.out.println("没有你删除的这本书");
}
for(int j=delIndex;j<currentSize;j++){
Book book=bookList.getBook(j+1);
bookList.setBooks(j,book);
}
bookList.setBooks(currentSize-1,null);
bookList.setUseSize(currentSize-1);
System.out.println("删除图书成功");
}
}
借阅图书
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);
System.out.println("请输入你要借阅图书姓名:");
String name=sc.nextLine();
int currentSize=bookList.getUseSize();
for(int i=0;i<currentSize;i++){
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
if(book.isBorrowed())
{
System.out.println("该书已经被借出了");
}else{
book.setBorrowed(true);
}
return;
}
}
System.out.println("没有你要借阅的图书!");
}
}
归还图书
package operation;
import book.Book;
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);
System.out.println("请输入你要归还图书姓名:");
String name=sc.nextLine();
int currentSize=bookList.getUseSize();
int i=0;
for(;i<currentSize;i++){
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
if(book.isBorrowed())
{
book.setBorrowed(false);
System.out.println("归还成功!");
}else{
System.out.println("该书已经被归还了!");
}
return;
}
}
if(i==currentSize)
System.out.println("没有你要归还的图书!");
}
}