您的位置:首页 > 编程语言 > Java开发

Java入门教程之图书管理系统(由简入繁)(三)

2017-03-23 22:13 567 查看
作者:AlexTan

E-mail: alextanbz@gmai.com

上一篇博客我们讲到了用ArrayList来实现图书管理系统,用ArrayList弥补了二中所提到的那两个缺点。但是一个漏洞填完,又来了新的一个漏洞,所谓精益求精嘛。接下来我们就在原代码的基础上加上IO流,对TXT文件进行操作。

Java入门之图书管理系统二(IO实现):

这次的代码只需要加上读写两个函数就可以了,即load()和save()。

为了代码看起来不那么臃肿,所以我们建立一个tool包,里面放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 ui.MainClass;

public class IO {

public void load(MainClass mainClass)//读取文件
{
try {
String filename = "D:\\Users\\alext\\workspace\\first\\book.txt";//也可以用d:/xxx/xxx的形式
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);
mainClass.booklist.add(book);
mainClass.count++;
}
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(MainClass mainClass)//写入文件
{
String fileName = "D:\\Users\\alext\\workspace\\first\\book.txt";
String allbook="";
for(int i = 0; i < mainClass.booklist.size(); i++)
{
Book book = (Book)mainClass.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();
}

}

}


然后在MainClass类里的构造函数里加上
IO io = new IO();
io.load(this);
这两段代码就可以读取了,在退出系统那里,即

if(choice == 5)
{
io.save(this);
System.out.println("成功退出系统,欢迎再次光临!");
break;
}


这里改成这样就好了,完整MainClass代码如下:

package ui;

import java.util.ArrayList;
import java.util.Scanner;

import model.Book;
import tool.IO;

public class MainClass {
/*
public static final int SIZE = 10;
Book[] booklist = new Book[SIZE];
*/
public ArrayList booklist = new ArrayList();
public int count = 0;

public MainClass()
{

Scanner scan = new Scanner(System.in);
IO io = new IO(); io.load(this);

printMenu();

while(true)
{
//读取用户输入
int choice = scan.nextInt();

if(choice == 5) { io.save(this); 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()
{
if (count < booklist.size()+1)
{
System.out.println("当前
ca70
共有:"+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();
Book book = new Book(bookname,author,price);
//booklist[count] = book;
booklist.add(book);
count++;
System.out.println("增加成功!");
printAllBook();
}
else
{
System.out.println("图书库已满!");
}

}

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();
id = orderFind(id);
//System.out.println(id);
if(id > -1)
{
/*
for(int i = id; i < count - 1 ; i++)
booklist[i]=booklist[i+1];
*/
booklist.remove(id);
count--;
System.out.println("删除成功!");
printAllBook();
}
else
{
System.out.println("输入错误!");
}
}
else if(choose == 2)
{
System.out.println("请输入您要删除的书名:");
String name = scan.next();
int id = nameFind(name);
if(id > -1)
{
/*
for(int j = id; j<count-1; j++)
{
booklist[j]=booklist[j+1];
}
*/
booklist.remove(id);
count --;
System.out.println("删除成功!");
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();
int id = orderFind(number);
if(id > -1)
{
Book book = (Book)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);
book.setBook(str,author,price);
System.out.println("修改成功!");
printAllBook();
}
else
{
System.out.println("输入错误!");
}
}
else if(choose == 2)
{
System.out.println("请输入您要修改的书名:");
String name = scan.next();
int id = nameFind(name);
if(id > -1)
{
Book book = (Book)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);
book.setBook(str,author,price);
System.out.println("修改成功!");
printAllBook();
}
}
else if(choose == 3)
{
printMenu();
break;
}
else
{
System.out.println("输入非法!");
}
}
}

void findBoo()
{
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();
int id = orderFind(number);
if(id > -1)
{
Book book = (Book)booklist.get(id);
//System.out.println("你要查找的书名为:"+booklist[id].getBookname()+" 作者:"+booklist[id].getAuthor()+" 单价:"+booklist[id].getPrice()+"元/本");
System.out.println("你要查找的书名为:"+book.getBookname()+" 作者:"+book.getAuthor()+" 单价:"+book.getPrice()+"元/本");
}
else
{
System.out.println("输入错误!");
}
}
else if(choose == 2)
{
System.out.println("请输入您要查找的书名:");
String name = scan.next();
int id = nameFind(name);
if(id > -1)
{
Book book = (Book)booklist.get(id);
//System.out.println("查找成功,您查找到的书为第"+(id+1)+"本书!"+"书名为:"+booklist[id].getBookname()+" 作者:"+booklist[id].getAuthor()+" 单价:"+booklist[id].getPrice()+"元/本");
System.out.println("查找成功,您查找到的书为第"+(id+1)+"本书!"+"书名为:"+book.getBookname()+" 作者:"+book.getAuthor()+" 单价:"+book.getPrice()+"元/本");
}
}
else if(choose == 3)
{
printMenu();
break;
}
else
{
System.out.println("输入非法!");
}
}
}

void printAllBook()
{
for (int i = 0; i < count; i++)
{
Book book = (Book)booklist.get(i);
//System.out.println("第"+(i+1)+"本书名:"+booklist[i].getBookname()+" 作者:"+booklist[i].getAuthor()+" 单价:"+booklist[i].getPrice()+"元/本");
System.out.println("第"+(i+1)+"本书名:"+book.getBookname()+" 作者:"+book.getAuthor()+" 单价:"+book.getPrice()+"元/本");
}
}

int orderFind(int number)
{
//System.out.println(number);
if(number <= count)
{
int id = number - 1;
return id;
}
else
return -1;
}

int nameFind(String name)
{
int id = -1;
for(int i = 0; i < count; i++)
{
Book book = (Book)booklist.get(i);
//if(booklist[i].getBookname().equals(name))
if(book.getBookname().equals(name))
{
id = i;
break;
}
else if(i<count)
continue;
else
{
System.out.println("未查找到您想要的书名");
break;
}
}
return id;
}

/*
void load()
{
String fileName = "D:/Users/alext/workspace/first/book.txt";
try {
File file = new File(fileName);
BufferedReader in = new BufferedReader(new FileReader(file));
String tempString;
while ((tempString = in.readLine()) != null)
{
System.out.println(tempString);
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

void save(String bookname,String author,float price)
{
String fileName = "D:\\Users\\alext\\workspace\\first\\book.txt";
File file1 = new File(fileName);
try {
FileWriter fileWriter = new FileWriter(file1,true);
String writeData="书名:"+ bookname +" 作者:"+author+" 价格:"+price+"元/本 \n";
fileWriter.write(writeData);
fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
new MainClass();
}

}

总结:

这样下来,便解决了重新运行程序数据消失的问题了。虽然我们上面把对txt文件读写的函数写在了tool包下的IO类里,但是整个程序看起来还是非常的臃肿,代码不好维护,也不好阅读,因此接下来我们将把以上代码改成MVC的编程模式,来让代码显得更系统化。

请阅读下一篇:Java入门教程之图书管理系统(由简入繁)(四)

转载请注明出处:http://blog.csdn.net/alextan_/article/details/65449952
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: