作者:AlexTan
E-mail: [email protected]
上一篇博客我们提到代码太臃肿,那么这篇博客将来解决这个问题。这篇博客将把上篇博客的代码改成MVC编程模式,MVC即models,views,controller,详细介绍请继续往下看。
MVC编程模式介绍:
那么根据MVC编程模式,我们把上次的代码分成一下几个模块:
注:ui即view。
我们把增删改查的函数放入control里,把输出输入放在MainClass里,把Book,以及BookList放在model下,这样程序看起来就更系统化了,下面是具体代码:
Operator.java:
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()+"元/本");
}
public int orderFind(int number)
{
//System.out.println(number);
if(number <= BookList.booklist.size())
{
int id = number - 1;
return id;
}
else
return -1;
}
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
Book.java和以前的一样,没发生任何变化。
BookList.java:
注释部分是把load加进构造函数里,但是这样运行没成功,所以还是用的tool:
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 extends ArrayList{
public static BookList booklist = new BookList();
public BookList()
{
try {
String filename = "D:\\Users\\alext\\workspace\\first\\book.txt";
File file = new File(filename);
BufferedReader reader = new BufferedReader(new FileReader(file));
String temp;
while((temp = reader.readLine()) != null)
{
String bookname = temp.split(",")[0];
String author = temp.split(",")[1];
String pricestr = temp.split(",")[2];
float price = Float.parseFloat(pricestr);
Book book = new Book(bookname,author,price);
this.booklist.add(book);
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*/
public class BookList{
public static ArrayList booklist = new ArrayList();
}
这个文件有一点小的变化,主要是用的booklist不一样了:
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:\\Users\\alext\\workspace\\first\\book.txt";
File file = new File(filename);
BufferedReader reader = new BufferedReader(new FileReader(file));
String temp;
while((temp = reader.readLine()) != null)
{
String bookname = temp.split(",")[0];
String author = temp.split(",")[1];
String pricestr = temp.split(",")[2];
float price = Float.parseFloat(pricestr);
Book book = new Book(bookname,author,price);
BookList.booklist.add(book);
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void save()
{
String fileName = "D:\\Users\\alext\\workspace\\first\\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";
allbook += temp;
}
try {
File file1 = new File(fileName);
FileWriter fileWriter;
fileWriter = new FileWriter(file1);
fileWriter.write(allbook);
fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package ui;
import java.util.ArrayList;
import java.util.Scanner;
import control.Operator;
import model.Book;
import model.BookList;
import tool.IO;
public class MainClass {
/*
public static final int SIZE = 10;
Book[] booklist = new Book[SIZE];
*/
public MainClass()
{
Scanner scan = new Scanner(System.in);
IO io = new IO();
io.load();
printMenu();
while(true)
{
//读取用户输入
int choice = scan.nextInt();
if(choice == 5)
{
io.save();
System.out.println("成功退出系统,欢迎再次光临!");
break;
}
switch(choice)
{
case 1: addBook(); break;
case 2: deleteBoo(); break;
case 3: changeBoo(); break;
case 4: findBoo(); break;
default: System.out.println("输入非法"); printMenu(); continue;
}
}
/*
while(true)
{
//根据用户输入调用不同方法
if(choice == 1)
{
addBook();
}
else if(choice == 2)
{
deleteBoo();
}
else if(choice == 3)
{
changeBoo();
}
else if(choice == 4)
{
findBoo();
}
else if(choice == 5)
{
System.out.println("成功退出系统,欢迎再次光临!");
break;
}
}
*/
}
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);
//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);
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 -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
{
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);
}
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) {
// TODO Auto-generated method stub
new MainClass();
}
}
请期待下一篇:Java入门教程之图书管理系统(由简入繁)(五)
转载请注明出处:http://blog.csdn.net/alextan_/article/details/65450781