package hashMap;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Book implements Serializable
{
private String id;
private String name;
private float price;
private long num;
public Book(String id, String name, float price, long num)
{
super();
this.id = id;
this.name = name;
this.price = price;
this.num = num;
}
@Override
public boolean equals(Object o)
{
if(o==this)
{
return true;
}
if(o instanceof Book)
{
Book bb=(Book)o;
return bb.id.equals(this.id)&&bb.name.equals(this.name);
}
else
{
return false;
}
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
public float getPrice()
{
return price;
}
public long getNum()
{
return num;
}
@Override
public int hashCode()
{
return this.id.hashCode()*this.name.hashCode();
}
public void setName(String name)
{
this.name = name;
}
public void setPrice(float price)
{
this.price = price;
}
public void setNum(long num)
{
this.num = num;
}
@Override
public String toString()
{
return "编号:"+this.id+"\t书名:"+this.name+"\t单价:"+this.price+"\t数量:"+this.num;
}
}
package hashMap;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @content 书籍管理系统
* @author 郑云飞
*@date 2010年10月10日8:44
*/
public class BookTest
{
private static File ff=new File("book.ini");
private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
private static DataOutputStream out;
private static DataInputStream in;
private static ArrayList<Book> al;
private static Book book;
private static String head="*******************书籍管理系统\n"
+"*******************作者:郑云飞\n"
+"*******************创作时间:2010年10月10日";
public static void main(String[] args) throws Exception
{
out=new DataOutputStream(new FileOutputStream(ff,true));
al=new ArrayList<Book>();
System.out.println(head);//输出头文件标题信息
read_Object();
operator();//操作界面
}
private static void operator() throws IOException
{
System.out.println("*******************************************************");
System.out.println("***1 增加书籍\n***2 查询书籍\n***3 删除书籍\n***4 修改书籍\n***5 显示所有书籍\n***6 退出系统");
System.out.println("*******************************************************");
System.out.println("请选择你的操作类型:");
String type=br.readLine();
switch(type.charAt(0))
{
case '1':addBook();operator();break;
case '2':searchBook();operator();break;
case '3':delBook();operator();break;
case '4':makeBook();operator();break;
case '5':allBook();operator();break;
case '6':System.out.println("谢谢使用郑云飞书籍管理系统\n");System.exit(0);break;
default:System.out.println("类型选择有误");operator();break;
}
}
/*****************************创建一个方法用于修改书籍信息
* @throws IOException */
private static void makeBook() throws IOException
{
System.out.println("请输入你要修改的书籍编号:");
String id=br.readLine();
boolean flag=false;
int count=0;
for(int i=0;i<al.size();i++)
{
if(id.equals(al.get(i).getId()))
{
flag=true;
count++;
}
if(flag)
{
boolean ff=true;
while(ff)
{
System.out.println("****************\n**1 修改书籍名称\n**2 修改书籍数量\n**3 修改书籍单价 \n**4 修改完成 \n***************");
System.out.println("请您选择操作类型:");
String type=br.readLine();
try
{
switch (type.charAt(0))
{
case '1':System.out.println("请输入修改后的书籍的名称:");
String name=br.readLine();
al.get(i).setName(name);break;
case '2':System.out.println("请输入修改后的书籍的数量:");
long num=Long.parseLong(br.readLine());
al.get(i).setNum(num);break;
case '3':
System.out.println("请输入修改后的书籍的单价:");
float price=Float.parseFloat(br.readLine());
al.get(i).setPrice(price);break;
case '4':System.out.println("修改信息成功");ff=false;wirte_object();operator();break;
default:System.out.println("输入有误");break;
}
}
catch (Exception e)
{
System.out.println("输入有误");
makeBook();
}
}
}
}
if(count==0)
{
System.out.println("修改书籍信息失败,不存在此信息");
}
}
/*****************************创建一个方法用于显示所有书籍信息*/
private static void allBook()
{
Iterator<Book> it=al.iterator();
while(it.hasNext())
{
Book book=it.next();
System.out.println(book);
}
}
/*****************************创建一个方法用于查询书籍信息
* @throws IOException */
private static void searchBook() throws IOException
{
System.out.println("*************请输入查询类型: 1 按编号查询 2 按书名查询");
String serchtype=br.readLine();
switch(serchtype.charAt(0))
{
case '1':search("id");break;
case '2':search("name");break;
default:System.out.println("输入类型有误");searchBook();break;
}
}
/*****************************创建一个方法用于按书名查询书籍信息
* @throws IOException */
private static void search(String type) throws IOException
{
/**********************遍历容器*********************/
if(type.equals("id"))
{
System.out.println("请输入书籍编号:");
String id_book=br.readLine();
boolean flag=false;
int count=0;
for(int k=0;k<al.size();k++)
{
if(id_book.equals(al.get(k).getId()))//按编号查询到书籍信息
{
/****************输出书籍信息***************/
count++;
flag=true;
}
else
{
flag=false;
}
if(flag==true)
{
System.out.println("查询到的书籍信息为:");
System.out.println(al.get(k));
}
}
if(count==0)
{
System.out.println("没有找到你要查询的任何书籍信息");
}
operator();//页面跳转
}
else if(type.equals("name"))
{
System.out.println("请输入书籍名称:");
String name_book=br.readLine();
int count=0;//查询书籍的个数
for(int j=0;j<al.size();j++)
{
boolean flag=false;
if(name_book.equals(al.get(j).getName()))
{
count++;
flag=true;
}
else
{
flag=false;
}
if(flag==true)
{
System.out.println("查询到的书籍信息为:");
System.out.println(al.get(j));
}
}
if(count==0)
{
System.out.println("没有找到你要查询的任何书籍信息");
}
operator();//页面跳转
}
}
/*****************************创建一个方法用于删除书籍信息
* @throws IOException */
private static void delBook() throws IOException
{
System.out.println("请输入你要删除的书籍编号:");
String id=br.readLine();
boolean flag=false;
int count=0;
for(int i=0;i<al.size();i++)
{
if(id.equals(al.get(i).getId()))
{
flag=true;
count++;
al.remove(al.get(i));
}
if(flag==true)
{
wirte_object();
System.out.println("删除成功");
break;
}
}
if(count==0)
{
System.out.println("删除失败,不存在你要删除的信息");
}
}
/*****************************创建一个方法用于增加书籍信息
* @throws IOException *********************/
private static void addBook() throws IOException
{
try
{
String id = id_isExsit();
System.out.println("请输入书籍名称:");
String name=br.readLine();
System.out.println("请输入书籍单价:");
float price=Float.parseFloat(br.readLine());
System.out.println("请输入书籍数量:");
long num=Long.parseLong(br.readLine());
book=new Book(id,name,price,num);
al.add(book);
wirte_object();
}
catch (Exception e)
{
System.out.println("输入有误");
addBook();
}
}
/*******************定义一个方法,用于判断id是否重复*********/
private static String id_isExsit() throws IOException
{
boolean isexsit=true;
String id="";
while(isexsit)
{
System.out.println("请输入书籍编号:");
id=br.readLine();
for(int i=0;i<al.size();i++)
{
if(id.equals(al.get(i).getId()))
{
isexsit=true;
System.out.println("此编号已经存在,请重新选择一个编号");
break;
}
else
{
isexsit=false;
}
}
}
if(isexsit)
{
return "此编号已经存在";
}
else
{
return id;
}
}
/**************************定义一个方法,用于向文件中写入信息********************************/
private static void wirte_object()
{
try
{
out=new DataOutputStream(new FileOutputStream(ff));
out.writeUTF(""+"***********************************");
out.writeUTF(""+"*******************书籍管理系统");
out.flush();
for(int i=0;i<al.size();i++)
{
out.writeUTF("编号:"+al.get(i).getId());
out.writeUTF("书名:"+al.get(i).getName());
out.writeUTF("单价:"+al.get(i).getPrice());
out.writeUTF("数量:"+al.get(i).getNum());
out.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/******************************定义一个方法用于读取文件中的内容******************************/
private static void read_Object()
{
try
{
in=new DataInputStream(new FileInputStream(ff));
String id="",name="";
float price=0;
long num=0;
while(in.available()!=0)
{
String str=in.readUTF();//接受读取的信息
/******************处理接受的信息,使其能够构成一个书籍对象*************/
if(str.substring(0, 2).equals("编号"))
{
id=str.substring(3);
}
else if(str.substring(0, 2).equals("书名"))
{
name=str.substring(3);
}
else if(str.substring(0, 2).equals("单价"))
{
price=Float.parseFloat(str.substring(3));
}
else if(str.substring(0, 2).equals("数量"))
{
num=Long.parseLong(str.substring(3));
}
if(!id.equals("")&&!name.equals("")&&price!=0&&num>0)
{
/******************将读取到得信息存放的容器中****************/
al.add(new Book(id,name,price,num));
/*******************读取完成一个对象后,必须重新初始化一下内容*************/
id="";
name="";
price=num=0;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}