作者:AlexTan
E-mail: [email protected]
上一篇博客我們講到了用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("當前共有:"+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