本系列是通过学习AlexTan这位博主的文章搭建一个图书管理系统,顺便再学习一下java这门语言。大二水了一学期java课,完全是菜鸡,所以注释很多自己的理解。
本篇是解决代码过于臃肿问题,把项目构建成MVC模式。
一、项目目录
二、创建Operator.java类,将MainClass的增删改查方法放到此类中。代码如下:
package control;
import model.Book;
import model.BookList;
public class Operator {
//添加图书
public boolean addBook(String bookname, String author, float price)
{
Book book = new Book(bookname,author,price);
if (BookList.booklist.add(book))
{
return true;
}
else
return false;
}
//删除图书
public boolean deleteBook(int id)
{
if(BookList.booklist.remove(id) != null)
{
return true;
}
else
return false;
}
//修改图书
public boolean changeBoo(int id,String bookname, String author, float price)
{
Book book = (Book) BookList.booklist.get(id);
if(book.setBook(bookname,author,price))
return true;
else
return false;
}
//查找图书
public void findBoo(int id)
{
Book book = (Book)BookList.booklist.get(id);
System.out.println("查找成功,您查找到的书为第"+(id+1)+"本书!"+"书名为:"+book.getBookname()+" 作者:"+book.getAuthor()+" 单价:"+book.getPrice()+"元/本");
}
//将顺序转化为对应的id
public int orderFind(int number)
{
//System.out.println(number);
if(number <= BookList.booklist.size())
{
int id = number - 1;
return id;
}
else
return -1;
}
//将书名转化为对应的id
public int nameFind(String name)
{
int id = -1;
for(int i = 0; i < BookList.booklist.size(); i++)
{
Book book = (Book)BookList.booklist.get(i);
if(book.getBookname().equals(name))
{
id = i;
break;
}
else if(i<BookList.booklist.size())
continue;
else
{
System.out.println("未查找到您想要的书名");
break;
}
}
return id;
}
//打印所有图书
public void printAllBook()
{
for (int i = 0; i < BookList.booklist.size(); i++)
{
Book book = (Book)BookList.booklist.get(i);
System.out.println("第"+(i+1)+"本书名:"+book.getBookname()+" 作者:"+book.getAuthor()+" 单价:"+book.getPrice()+"元/本");
}
}
}
三、修改Book.jave类
package model;
//创建一book类
public class Book {
private String bookname; //三个成员变量,定义图书有三种属性
private String author; //用private修饰,指定该变量只能自己类的方法访问,其他任何类(包括子类)的方法都不能访问
private float price;
//构造方法
public Book(String bookname, String author, float price)
{
this.bookname = bookname;
this.author = author;
this.price = price;
}
//获取各个字段的值
public String getBookname() {
return bookname;
}
public String getAuthor() {
return author;
}
public float getPrice() {
return price;
}
//重置各个字段的值
public boolean setBook(String bookname, String author, float price) {
try {
this.bookname = bookname;
this.author = author;
this.price = price;
return true;
}catch(Exception e) {
e.printStackTrace();
return false;
}
}
}
四、BookList.java类
package model;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class BookList{
public static ArrayList booklist = new ArrayList();
}
这个类原本作者是要将load()方法放进来,但是没有成功,所以就没什么东西。
五、修改io.java
package tool;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import model.Book;
import model.BookList;
import ui.MainClass;
public class IO {
public void load()//读取文件
{
try {
String filename = "D:/eclipse/Library_manager/book.txt"; //这个根据个人设置
File file = new File(filename); //初始化一个文件类型的实例
//FileReader类是从InputStreamReader类继承而来,按字符读取流中数据
BufferedReader reader = new BufferedReader(new FileReader(file));
String temp;
while((temp = reader.readLine()) != null)
{
String bookname = temp.split(",")[0]; //读取文本文件里的内容,使用split()函数按“,”分块
String author = temp.split(",")[1];
String pricestr = temp.split(",")[2];
float price = Float.parseFloat(pricestr); //parseFloat()解析一个字符串并返回一个float类型的值,还有parseInt()等
Book book = new Book(bookname,author,price);
BookList.booklist.add(book); //用ArrayList类加载图书
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void save()//写入文件
{
String fileName = "D:/eclipse/Library_manager/book.txt";
String allbook="";
for(int i = 0; i < BookList.booklist.size(); i++)
{
Book book = (Book)BookList.booklist.get(i);
String temp = book.getBookname() + "," + book.getAuthor() + "," + book.getPrice() + "\r\n";
//一本书占一行,以String的格式存入文本文件
allbook += temp;
}
try {
File file1 = new File(fileName);
//创建文件
FileWriter fileWriter = new FileWriter(file1);
//向文件中写入内容
fileWriter.write(allbook);
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
六、修改MainClass.java
package ui;
import java.util.ArrayList;
import java.util.Scanner;
import model.Book;
import tool.IO;
import control.Operator;
import model.BookList;
public class MainClass {
/*
public static final int SIZE = 10; //最大可存储图书的数量
//public指定该变量为公共,static指定变量被所有对象共享,即所有实例都可以使用该变量
//final为最终修饰符,指定此变量的值不能变
Book[] booklist = new Book[SIZE]; //初始化Book类型的实例
int count = 0; //当前图书数量
public ArrayList booklist = new ArrayList();
public int count=0;
*/
public MainClass()
{
Scanner scan = new Scanner(System.in); //Java的控制台输入由System.in完成,获取用户输入
IO io = new IO();
io.load();
printMenu(); //调用打印菜单的方法
while(true)
{
//读取用户输入
int choice = scan.nextInt(); //获取一个int型输入
if(choice == 5)
{
io.save();
System.out.println("成功退出系统,欢迎再次光临!");
break;
}
switch(choice)//switch形式
{
case 1: addBook(); break;
case 2: deleteBoo(); break;
case 3: changeBoo(); break;
case 4: findBoo(); break;
default: System.out.println("输入非法"); printMenu(); continue;//这个continue 是continue的while,
}
}
}
void printMenu()
{
//打印菜单
System.out.println("欢迎...");
System.out.println("增加图书...1");
System.out.println("删除图书...2");
System.out.println("修改图书...3");
System.out.println("查询图书...4");
System.out.println("退出系统...5");
}
void addBook()//增加图书
{
System.out.println("当前共有:"+BookList.booklist.size()+"本书!");
Scanner scan = new Scanner(System.in);
System.out.println("请输入图书名:");
String bookname = scan.next();
System.out.println("请输入作者:");
String author = scan.next();
System.out.println("请输入单价:");
float price = scan.nextFloat();
Operator operator = new Operator();
boolean isSuccess = operator.addBook(bookname, author, price);
if(isSuccess)
{
System.out.println("增加成功!");
}
else
{
System.out.println("增加失败!");
}
operator.printAllBook();
}
void deleteBoo()//删除图书
{
Scanner scan = new Scanner(System.in);
while(true)
{
System.out.println("请输入按哪种方法删除图书:1、序号/2、书名/3、返回主菜单");
int choose = scan.nextInt();
if(choose == 1)
{
System.out.println("请输入要删除第几本书:");
int id = scan.nextInt();
Operator operator = new Operator();
id = operator.orderFind(id); //调用Operator类中的方法,将要删除的第几本书转换为此书对应id
//System.out.println(id);
if(id > -1)
{
/*
for(int i = id; i < count - 1 ; i++)
booklist[i]=booklist[i+1];
*/
boolean isSuccess = operator.deleteBook(id); //调用删除方法,根据提供的id删除对应的图书
if(isSuccess)
System.out.println("删除成功!");
else
System.out.println("删除失败!");
operator.printAllBook();
}
else
{
System.out.println("输入错误!");
}
}
else if(choose == 2)
{
System.out.println("请输入您要删除的书名:");
String name = scan.next();
Operator operator = new Operator();
int id = operator.nameFind(name);
if(id > -1)
{
/*
for(int j = id; j<count-1; j++)
{
booklist[j]=booklist[j+1];
}
*/
boolean isSuccess = operator.deleteBook(id);
if(isSuccess)
System.out.println("删除成功!");
else
System.out.println("删除失败!");
operator.printAllBook();
}
else
{
System.out.println("未查找到您想要的书名");
}
}
else if(choose == 3)
{
printMenu();
break;
}
else
{
System.out.println("输入非法!");
}
}
}
void changeBoo() //修改图书,提供两种修改方式
{
Scanner scan = new Scanner(System.in);
while(true)
{
System.out.println("请输入按哪种方法修改图书:1、序号/2、书名/3、返回主菜单");
int choose = scan.nextInt();
if(choose == 1)
{
System.out.println("请输入要修改第几本书:");
int number = scan.nextInt();
Operator operator = new Operator();
int id = operator.orderFind(number); //将要修改的第几本书转换为此书对应的id,通过id来查找到这本书,然后进行修改
if(id > -1)
{
Book book = (Book)BookList.booklist.get(id); //通过id获得这本书
//System.out.println("原书名为:"+booklist[id].getBookname()+" 请输入你要修改为什么书名:");
System.out.println("原书名为:"+book.getBookname()+" 请输入你要修改为什么书名:");
String str = scan.next();
System.out.println("请输入作者:");
String author = scan.next();
System.out.println("请输入单价:");
float price = scan.nextFloat();
//booklist[id].setBook(str,author,price);
boolean isSuccess = operator.changeBoo(id,str,author,price); //调用changeBoo()方法进行修改
if(isSuccess)
System.out.println("修改成功!");
else
System.out.println("修改失败!");
operator.printAllBook();
}
else
{
System.out.println("输入错误!");
}
}
else if(choose == 2)
{
System.out.println("请输入您要修改的书名:");
String name = scan.next();
Operator operator = new Operator();
int id = operator.nameFind(name);
if(id > -1)
{
Book book = (Book)BookList.booklist.get(id);
//System.out.println("原书名为:"+booklist[id].getBookname()+" 请输入你要修改为什么书名:");
System.out.println("原书名为:"+book.getBookname()+" 请输入你要修改为什么书名:");
String str = scan.next();
System.out.println("请输入作者:");
String author = scan.next();
System.out.println("请输入单价:");
float price = scan.nextFloat();
//booklist[id].setBook(str,author,price);
boolean isSuccess = operator.changeBoo(id,str,author,price);
if(isSuccess)
System.out.println("修改成功!");
else
System.out.println("修改失败!");
operator.printAllBook();
}
}
else if(choose == 3)
{
printMenu();
break;
}
else
{
System.out.println("输入非法!");
}
}
}
void findBoo()
{
Scanner scan = new Scanner(System.in);
Operator operator = new Operator();
while(true)
{
System.out.println("请输入按哪种方法查找图书:1、序号/2、书名/3、返回主菜单");
int choose = scan.nextInt();
if(choose == 1)
{
System.out.println("请输入要查找第几本书:");
int number = scan.nextInt();
int id = operator.orderFind(number);
if(id > -1)
{
operator.findBoo(id); //通过id查找对应的图书
}
else
{
System.out.println("输入错误!");
}
}
else if(choose == 2)
{
System.out.println("请输入您要查找的书名:");
String name = scan.next();
int id = operator.nameFind(name);
if(id > -1)
{
operator.findBoo(id);
}
}
else if(choose == 3)
{
printMenu();
break;
}
else
{
System.out.println("输入非法!");
}
}
}
public static void main(String[] args) {
new MainClass();
}
}
MVC模式更改完成。
整个项目的逻辑是:
ui即视图(V):MainClass.java既作为入口函数,又作为面向用户的视图,提供了各种操作选择
control即控制器(C):Operator.java提供了增加图书、删除图书、修改图书、查找图书、打印所有图书、将书名,书号转换为对应的id等方法,供MainClass.java来调用
model即模型(M):Book.java建立一个图书模型,用来处理数据逻辑部分,模型部分负责在数据库中存取数据,目前项目中还没用到数据库存储,只是先存在文本文件里,然而现实中的系统单靠文本文件来存储数据肯定是不够的。