[Java]---图书管理系统(三)

参考:http://blog.csdn.net/alextan_/article/details/65447446

本系列是通过学习AlexTan这位博主的文章搭建一个图书管理系统,顺便再学习一下java这门语言。大二水了一学期java课,完全是菜鸡,所以注释很多自己的理解。

这篇要解决的问题是:每次运行后数据无法保存。

一、项目目录
在这里插入图片描述
二、创建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:/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);
				mainClass.booklist.add(book);   //调用MainClass类,将每本书加载到booklist这个动态数组里
				mainClass.count++;
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void save(MainClass mainClass)//写入文件
	{
		String fileName = "D:/eclipse/Library_manager/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";
			//一本书占一行,以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改动如下
添加的图书只有在选择5退出系统后才会写入文本,如果没有进行退出系统操作,添加的图书无法写入book.txt中。
在这里插入图片描述
完整的MainClass.java代码如下:

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;  //最大可存储图书的数量
	//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(this);
		
		printMenu();  //调用打印菜单的方法
		
		while(true)
		{
			//读取用户输入
			int choice = scan.nextInt();  //获取一个int型输入
			
			if(choice == 5)
			{
				io.save(this);
				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()//增加图书
	{
		if (count > booklist.size()-1)   //注意这里的改动
		{
			System.out.println("当前共有:"+count+"本书!");
			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);//调用ArrayList的add方法
			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++)//用for循环的形式实现对数组的删除
						booklist[i]=booklist[i+1];   //通过数组的后面元素覆盖前面的元素来实现删除
					*/
					booklist.remove(id);  //调用ArrayList的删除方法
					count--;
					System.out.println("删除成功!");
					printAllBook();
				}
				else
				{
					System.out.println("输入错误!");
				}
			}
			else if(choose == 2)
			{
				System.out.println("请输入您要删除的书名:");
				String name = scan.next();
				int id = nameFind(name);    //通过方法将作者名转化为对应的id,但是这样有一个bug,要是作者名相同呢?
				if(id > -1)
				{
					/*
					for(int j = id; j<count-1; j++)//用for循环的形式实现对数组的删除
					{
						booklist[j]=booklist[j+1];
					}
					*/
					booklist.remove(id);
					count --;
					System.out.println("删除成功!");
					printAllBook();
				}
				else
				{
						System.out.println("未查找到您想要的书名");
				}	
			}
			else if(choose == 3)
			{
				printMenu();
				break; //这个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);  //将输入的数字转换为id,因为数组下标从0开始,所以id和number差了1
				if(id > -1)
				{
					Book book = (Book)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);
					book.setBook(str, author, price);  //调用Book类的重置属性方法
					System.out.println("修改成功!");
					printAllBook();
				}
				else
				{
					System.out.println("输入错误!");
				}
			}
			else if(choose == 2)
			{
				System.out.println("请输入您要修改的书名:");
				String name = scan.next();
				int id = nameFind(name);   //将书名转化为对应的id
				if(id > -1)
				{
					Book book = (Book)booklist.get(id);
					//System.out.println("原书名为:"+booklist[id].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)	//按序号查找,返回id
	{
		//System.out.println(number);
		if(number <= count)
		{
			int id = number - 1;
			return id;
		}
		else
			return -1;
	}
	
	int nameFind(String name)//按书名查找,返回id
	{
		int id = -1;
		for(int i = 0; i < count; i++)
		{
			Book book = (Book)booklist.get(i);
			if(book.getBookname().equals(name))  //调用Book类中的获取书名方法
			{
				id = i;
				break;
			}
			else if(i<count)
				continue;
			else
			{
				System.out.println("未查找到您想要的书名");
				break;
			}
		}
		return id;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MainClass();
	}
}

运行结果:
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Duang_993/article/details/86835224