`
郑云飞
  • 浏览: 823106 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

图书管理系统

阅读更多
package myio.mydata;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;

public class Book implements Serializable
{
	private String id;
	private String name;
	private int count;
	private float price;
	public String getId()
	{
		return id;
	}
	public void setId(String id)
	{
		this.id = id;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getCount()
	{
		return count;
	}
	public void setCount(int count)
	{
		this.count = count;
	}
	public float getPrice()
	{
		return price;
	}
	public void setPrice(float price)
	{
		this.price = price;
	}
	//transient Thread th = new Thread();
	public Book(String id, String name, int count,float price)
	{
		super();
		this.id = id;
		this.name = name;
		this.count = count;
		this.price = price;
	}
	public Book()
	{
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString()
	{
		// TODO Auto-generated method stub
		return "编号:"+id+" 书名:"+name+" 库存"+count+"进价:"+price;
	}
	
	public boolean equals(Object o)
	{
		if(this==o)
		{
			return true;
		}
		else
		{
			Book b = (Book)o;
			return this.id==b.id&&this.name.equals(b.name)&&this.count==b.count;
		}
	}
	
	public int hashCode()
	{
		System.out.println(name);
		return id.hashCode();
	}
	
//	private void readObject(ObjectInputStream ois) throws IOException
//	{
//		this.id = ois.readInt();
//		this.name = ois.readUTF();
//		this.count = ois.readInt();
//		//this.price = ois.readFloat();
//	}
//	
//	private void writeObject(ObjectOutputStream oos) throws IOException
//	{
//		oos.writeInt(this.id);
//		oos.writeUTF(this.name);
//		oos.writeInt(this.count);
//	}
	
	public void readObject(RandomAccessFile raf) throws IOException
	{
		this.id = raf.readUTF();
		this.name = raf.readUTF();
		this.count = raf.readInt();
		this.price = raf.readFloat();	
	}
	
	public void writeObject(RandomAccessFile raf) throws IOException
	{
		raf.writeUTF(this.id);
		raf.writeUTF(this.name);
		raf.writeInt(this.count);
		raf.writeFloat(this.price);
	}
}
 package myio.mydata; /** * 书籍管理系统 */ import java.util.*; /** * 系统说明 : 1.首先系统加载 2.提供系统功能服务 3.系统退出 * * @author lzt * */ public class BookStore { static Scanner read = new Scanner(System.in); static FileOp fileOp = FileOp.getInstance(); public static void main(String[] args) { // 设置文件源 fileOp.setFileName("book.txt"); // 初始化 载入所有记录到容器 以供操作 fileOp.initial(); System.out.println("系统正在初始化···"); while (true) { opration(); } } private static void addInfo() { boolean ok = false; do { ok = false; System.out.print("\n请输入书籍编号:"); String id = read.next(); if(!fileOp.checkId(id)) { System.out.print("\n请输入书籍名称:"); String name = read.next(); int count = Integer.parseInt(getString("数量")); float price = Float.parseFloat(getString("价格")); // 根据输入构建书籍对象 Book book = new Book(id, name, count, price); // 将对象写入文件 if (fileOp.addInfo(book)) { System.out.println("书籍" + name + "添加成功!"); ok = true; } else { System.out.println("书籍未能添加!"); } } else { System.out.println("此id已经存在 请重新输入"); } }while(!ok); } private static void deleleInfo() { System.out.println("请输入您要删除的书籍编号:"); String id = read.next(); if(fileOp.checkId(id)) { if (fileOp.deleteInfo(id)) { System.out.println("编号为" + id + "的书籍删除成功!"); } } else { System.out.println("此书籍不存在"); } } private static void updateInfo() { System.out.println("请输入需要修改的书籍编号:"); String id = read.next(); if(fileOp.checkId(id)) { System.out.print("\n请输入书籍名称:"); String name = read.next(); int count = Integer.parseInt(getString("数量")); float price = Float.parseFloat(getString("价格")); // 构建书籍对象 Book book = new Book(id, name, count, price); // 将对象更新到文件 if (fileOp.updateInfo(book)) { System.out.println("编号为" + id + "的书籍的信息修改成功!"); } } else { System.out.println("此书籍不存在"); } } private static void queryInfo() { System.out.println("请输入您要查找的书籍的编号:"); String id = read.next(); if (fileOp.queryInfo(id)) { System.out.println("查询成功!"); } else { System.out.println("没有这样的书籍!"); } } public static void opration() { int choice = 0; String errors = null; System.out.println("**********************************"); System.out.println("*1.查看书籍信息"); System.out.println("*2.添加书籍信息"); System.out.println("*3.删除书籍信息"); System.out.println("*4.修改书籍信息"); System.out.println("*5.查看所有信息"); System.out.println("*6.退出系统"); System.out.println("**********************************"); System.out.println("请选择您的操作:"); do { try { choice = Integer.parseInt(read.next()); errors = null; } catch (NumberFormatException e) { System.out.println("输入有误!!"); errors = "error"; System.out.println("请重新输入:"); } } while (errors != null); switch (choice) { case 1: queryInfo(); break; case 2: addInfo(); break; case 3: deleleInfo(); break; case 4: updateInfo(); break; case 5: // 查询所有 if(fileOp.queryAll()) { System.out.println("查询成功!"); } break; case 6: System.out.println("欢迎再次使用!"); System.exit(0); break; default: System.out.println("输入有误!"); System.exit(-1); } } //按格式正确获取输入内容 public static String getString(String type) { String newString = ""; boolean flag = true; do { if(flag) { System.out.print("\n请输入书籍"+type+":"); flag = false; } else { System.out.print("\n输入有误 请重新输入书籍"+type+":"); } newString = read.next(); } while (!check(type,newString)); return newString; } //格式验证 public static boolean check(String type,String s) { boolean right = false; if(type.equals("数量")) { right = s.matches("^\\d+$"); } if(type.equals("价格")) { right = s.matches("^\\d+($|\\.\\d+$)"); } return right; } }

 

package myio.mydata;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class FileOp
{
	private String fileName;

	private FileWriter fw = null;

	private BufferedWriter bw = null;

	private FileReader fr = null;

	private BufferedReader br = null;

	private ArrayList<Book> listInfo = new ArrayList<Book>();

	String[] header = { "书籍编号", "书名", "书籍数量", "书籍单价" };

	private static FileOp fileOp = new FileOp();

	private FileOp()
	{
	}

	public static FileOp getInstance()
	{
		return fileOp;
	}

	// 设置文件源
	public void setFileName(String fileName)
	{
		this.fileName = fileName;
	}

	// 获取文件源
	public String getFileName()
	{
		return fileName;
	}

	// 获得输入输出流
	public BufferedWriter getOutPutStream()
	{
		if (checkFile())
		{
			try
			{
				fw = new FileWriter(fileName, true);
				bw = new BufferedWriter(fw);
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return bw;
	}

	public BufferedReader getInPutStream()
	{
		if (checkFile())
		{
			try
			{
				fr = new FileReader(fileName);
				br = new BufferedReader(fr);
			} catch (FileNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return br;
		// BufferedReader br = new BufferedReader();
	}

	// 关闭输入输出流
	public void closeInPutStream()
	{
		try
		{
			fr.close();
			br.close();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void closeOutPutStream()
	{
		try
		{
			fw.close();
			bw.close();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 初始化加载
	public boolean initial()
	{
		boolean flag = true;
		// 首先清空集合
		if (listInfo.size() != 0)
		{
			listInfo.clear();
		}
		BufferedReader br = getInPutStream();
		try
		{
			br.readLine();
			String strInfo = br.readLine();
			while (strInfo != null)
			{
				String[] infos = strInfo.split("\t");
				Book book = new Book();
				book.setId(infos[0]);
				book.setName(infos[1]);
				book.setCount(Integer.parseInt(infos[2]));
				book.setPrice(Float.parseFloat(infos[3]));
				listInfo.add(book);
				strInfo = br.readLine();
			}
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = false;
		} finally
		{
			closeInPutStream();
		}
		// while
		return flag;
	}

	// 增加新书籍记录
	public boolean addInfo(Book book)
	{
		boolean flag = false; // 添加失败标志变量
		Book Book = book;

		BufferedWriter bw = getOutPutStream(); // 获得输出流
		try
		{
			bw.newLine();
			bw.write(book.getId() + "\t" + book.getName() + "\t" + book.getCount() + "\t"+ book.getPrice());
			bw.flush(); // 清空缓冲
			flag = true; // 添加记录成功
			// 加入集合类中
			listInfo.add(Book);
			return flag;
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			closeOutPutStream(); // 关闭流
		}

		return flag;
	}

	// 删除书籍记录
	public boolean deleteInfo(String id)
	{
		boolean flag = false;
		for (int i = 0; i < listInfo.size(); i++)
		{
			if (id.equals(listInfo.get(i).getId()))
			{
				listInfo.remove(i); // 从集合中移出
				System.out.println(" 删除书籍");
				// 更新文件
				if (updateFile())
				{
					flag = true; // 删除成功
				}
				break;
			}
		}
		return flag;
	}

	// 修改指定编号的书籍信息
	public boolean updateInfo(Book book)
	{
		boolean flag = false;
		for (int i = 0; i < listInfo.size(); i++)
		{
			if (book.getId().equals(listInfo.get(i).getId()))
			{
				listInfo.set(i, book); // 更新集合
				// 更新文件
				if (updateFile())
				{
					flag = true; // 更新成功
				}
				break;
			}
		}
		return flag;
	}

	// 根据id查询书籍信息
	public boolean queryInfo(String id)
	{
		boolean flag = false; // 无指定书籍信息标志变量
		for (int i = 0; i < listInfo.size(); i++)
		{
			if (id.equals(listInfo.get(i).getId()))
			{
				System.out.println(listInfo.get(i).getId() + "\t"
						+ listInfo.get(i).getName() + "\t"
						+ listInfo.get(i).getCount() + "\t"
						+ listInfo.get(i).getPrice());
				flag = true; // 查询成功
				break;
			}
		}
		return flag;
	}

	// 查询所有书籍信息
	public boolean queryAll()
	{
		boolean flag = false; // 指定书籍信息标志变量
		for (int i = 0; i < listInfo.size(); i++)
		{
			// 在控制台显示
			System.out.println("书籍详细信息如下:");
			System.out.println(header[0] + ":  " + listInfo.get(i).getId()
					+ "  " + header[1] + ":  " + listInfo.get(i).getName()
					+ "\t" + header[2] + ":  " + listInfo.get(i).getCount()
					+ "\t" + header[3] + ":  " + listInfo.get(i).getPrice());
			System.out.println("\n*****************");
		}
		flag = true;
		
		return flag;
	}

	// 更新文件
	private boolean updateFile()
	{
		boolean flag = false;
		// 重写文件-->覆盖文件
		FileWriter fileWriter = null;
		BufferedWriter bufferedWriter = null;
		try
		{
			fileWriter = new FileWriter(fileName);
			bufferedWriter = new BufferedWriter(fileWriter);
			// **********************************************************************************
			bufferedWriter.write(header[0] + "\t" + header[1] + "\t"
					+ header[2] + "\t" + header[3]);
			bufferedWriter.newLine();
			for (int j = 0; j < listInfo.size(); j++)
			{

				bufferedWriter.write(listInfo.get(j).getId() + "\t"
						+ listInfo.get(j).getName() + "\t"
						+ listInfo.get(j).getCount() + "\t"
						+ listInfo.get(j).getPrice());
				bufferedWriter.newLine();
			}
			bufferedWriter.flush(); // 清空缓冲
			flag = true; // 删除成功
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			try
			{
				bufferedWriter.close();
				fileWriter.close(); // 关闭流操作
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}

	// 检查文件源是否存在
	public boolean checkFile()
	{
		File file = new File(fileName);
		boolean flag = false;
		// 如果指定的文件不存在,则创建该文件
		if (!file.exists())
		{
			try
			{
				flag = file.createNewFile();
				if (flag)
				{
					System.out.println(fileName + "文件创建成功!");
				}
				fw = new FileWriter(fileName);
				bw = new BufferedWriter(fw);
				bw.write(header[0] + "\t" + header[1] + "\t" + header[2] + "\t"
						+ header[3]);
				bw.flush();
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally
			{
				closeOutPutStream();
			}
		} else
		{
			flag = true;
		}

		return flag;
	}
	
	//检测id是否重复
	public boolean checkId(String id)
	{
		for (int i = 0; i < listInfo.size(); i++)
		{
			if (id.equals(listInfo.get(i).getId()))
			{
				return true;
			}
		}
		return false;
	}
}

 

分享到:
评论

相关推荐

    图书管理系统(纯HTML)

    图书管理系统(纯HTML)图书管理系统(纯HTML)图书管理系统(纯HTML)图书管理系统(纯HTML)图书管理系统(纯HTML)图书管理系统(纯HTML)图书管理系统(纯HTML)图书管理系统(纯HTML)图书管理系统(纯HTML)图书管理系统(纯HTML)...

    图书管理系统需求分析报告

    图书管理系统需求分析报告 图书管理系统需求分析报告是对图书管理系统的需求进行详细的分析和描述,以确保系统的设计和开发能够满足用户的需求。下面是图书管理系统需求分析报告的详细内容: 引言 图书管理系统...

    图书管理系统图书管理系统图书管理系统图书管理系统

    vb图书管理系统 图书管理系统 图书管理系统vb图书管理系统 图书管理系统 图书管理系统vb图书管理系统 图书管理系统 图书管理系统vb图书管理系统 图书管理系统 图书管理系统vb图书管理系统 图书管理系统 图书管理系统...

    微信小程序 图书管理系统 (源码)

    微信小程序 图书管理系统 (源码)微信小程序 图书管理系统 (源码)微信小程序 图书管理系统 (源码)微信小程序 图书管理系统 (源码)微信小程序 图书管理系统 (源码)微信小程序 图书管理系统 (源码)微信小程序 图书管理...

    PHP图书管理系统源码.zip

    PHP图书管理系统源码(毕业设计),软件架构说明 PHP+MySQL+layui php版本:7.3.4 MySQL版本:5.7.26PHP图书管理系统源码(毕业设计),软件架构说明 PHP+MySQL+layui php版本:7.3.4 MySQL版本:5.7.26PHP图书管理...

    图书管理系统 c# asp .net 图书管理系统 网页

    图书管理系统是一种基于网络的软件应用,它主要用于存储、管理和检索图书馆内的书籍信息,方便读者查询、借阅和归还图书。在这个系统中,通常会包含多个功能模块,如用户管理、书籍管理、借阅管理、预约管理等。本...

Global site tag (gtag.js) - Google Analytics