Java文件读写与vector运用实例-货物进销管理系统

题目:

编写一个Inventory.java完成以下功能(没有学过Java文件处理之前,各位同学可以使用硬编码将数据放进两个Vector变量里。等学过Java文件处理之后,再补充数据文件读取部分):

1.程序首先打开并读取Inventory.txt中记录的所有库存记录,然后读取Transactions.txt,处理这个文件中包含的事务,记录发货记录到Shipping.txt,并记录错误信息到Errors.txt中。最后更新库存到另外一个文件NewInventory.txt中。

2.文件Inventory.txt和NewInventory.txt的每行包含一个存货记录,没条记录包含下面一些字段息,这些字段之间用一个tab分开(见后面的文件格式):

字段

格式和含义

Item number

字符串型,货物编号

Quantity

整型,货物数量

Supplier

字符串型,供应商编号

Description

字符串型,货物描述

3.字段Items按照从小到大的顺序写入文件的。注意Item号不必连续,如Item号为752的后面可能是800。

4.文件Transactions.txt包含几个不同的处理记录(每行一条记录)。每条记录前面以一个大写字母开头,表示这条记录是什么类型的事务。在不同的大写字母后面是不同的信息格式。所有的字段也是以tab键分开的(见Transactions.txt文件格式)。

5.以'O'开头的事务表示这是一个发货订单,即某一种货物应该发给特定的客户。Item number和Quantity的格式如上面表格定义。Custom编号和上面的Supplier编号一致。处理一条定单记录(以'O'开头的事务)意味着从减少库存记录中相应货物的数量(减少的数量=发货单中的数量),记录发货信息到Shipping.txt中。注意:Inventory.txt中的quantity不应该小于0,如果对于某一种货物,库存的数量小于发货单的数量的话,系统应该停止处理发货单,并记录出错信息到Errors.txt。如果对于某一种货物有多个发货单,而且库存总量小于这些发货单的总和的话,系统应该按照发货单中的数量从小到大的有限原则满足客户。也就是说,对于某一种货物如果一个数量Quantity少的发货单没有处理之前,数量Quantity多的发货单永远不会被处理。(这种处理原则不受发货单记录在Transactions.txt的先后顺序影响)

6.以'R'开头的事务表示这是一个到货单记录,在'R'后面是Item number和它的数量Quanlity。处理一条到货单意味着增加库存中相应货物的数量(增加的数量=到货单中的数量)。注意:如果在Transactions.txt文件中,到货单出现在发货单之后,到货单中的货物数量可以用来填补发货单中的数量(可以理解成在Transactions.txt中,优先处理到货单)。

7.以'A'开头的事务表示向库存中增加一种新的货物(即这种货物以前库存中没有),在'A'后面是Item number,供应商supplier以及货物的描述description。处理一个新增货物记录意味着向库存中增加一个数量Quantity为0的新的Item。你可以假设在一个Transactions.txt中,新增货物记录总是出现在第一个到货单之前。

8.以'D'开头的事务表示从库存中删除一种货物,在'D'后面是Item号。删除操作总是在所有的事物处理之后才被处理,以保证对于可能出现的同一种货物的发货单的操作能在删除之前被正确处理。如果要删除的某种货物的库存量Quantity不为0的话,系统应该向Errors.txt记录出错信息。

9.文件Shipping.txt中的每一行代表给某一客户的发货信息。Shipping.txt中的每一行分别是客户编号、Item号、货物数量,它们之间用tab键分隔。如果发货单中有两条客户编号和Item编号一样的记录,在Shipping.txt中应该将这两条发货信息合并(即将它们的数量相加)。

10.Errors.txt文件包含未发送的发货记录和库存量大于0的删除记录。Errors.txt每一行包含Custom编号、Item编号以及发货单上的数量Quantity。对于删除操作,Custom编号为0,数量Quntity为库存中的Quantity.

11.实验测试数据:

Inventory.txt

Transactions.txt

 

 

题解:

import java.io.*;
import java.util.Vector;
//仓库信息
class Inventory{
	String Itemnumber = null;
	int Quantity =0;
	String Supplier = null;
	String Description = null;
	Inventory(){
	}
}
//发货信息
class Oitem{
	String Itemnumber = null;
	int Quantity = 0;
	String Supplier = null;
	Oitem(){
	}
}
//到货信息
class Ritem{
	String Itemnumber = null;
	int Quantity = 0;
	Ritem(){
	}
}
//货物添加信息
class Aitem{
	String Itemnumber = null;
	String Supplier = null;
	String Description = null;
	Aitem(){
	}
}
//货物删除信息
class Ditem{
	String Itemnumber = null;
	Ditem(){
	}
}
//主类
public class Item {
	  public static void main (String[] args) {
	  	File f = null;
	  	BufferedReader in=null;
	  	PrintWriter out=null;
	  	int len=0;//inv的长度
	  	Vector<Inventory> inv = new Vector<Inventory>();//仓库
	  	Vector<Oitem> O =new Vector<Oitem>();//发货信息
	  	Vector<Ritem> R=new Vector<Ritem>();//收货信息
	  	Vector<Ditem> D=new Vector<Ditem>();//删除信息
	  	Vector<Aitem> A=new Vector<Aitem>();//添加信息
	  	//信息读取
	  	try{
		  		//读取货物信息
		  		f=new File("./Inventory.txt");
		  		in = new BufferedReader(new FileReader(f));
		  		String line=in.readLine();
		  		while(line!=null){
		  			String [] infor=line.split("	");
		  			Inventory a = new Inventory();
		  			a.Itemnumber=infor[0];
		  			a.Quantity=Integer.valueOf(infor[1]);
		  			a.Supplier=infor[2];
		  			a.Description=infor[3];
		  			inv.addElement(a);
		  			line=in.readLine();
		  		}
		  		in.close();
		  		//读取操作信息
		  		f=new File("./Transactions.txt");
		  		in=new BufferedReader(new FileReader(f));
		  		line=in.readLine();
		  		while(line!=null){
		  			String [] infor=line.split("	");
		  			if(line.charAt(0)=='O'){
		  				Oitem a=new Oitem();
		  				a.Itemnumber=infor[1];
		  				a.Quantity=Integer.valueOf(infor[2]);
		  				a.Supplier=infor[3];
		  				O.addElement(a);
		  			}//发货信息
		  			else if(line.charAt(0)=='R'){
		  				Ritem a=new Ritem();
		  				a.Itemnumber=infor[1];
		  				a.Quantity=Integer.valueOf(infor[2]);
		  				R.addElement(a);
		  			}//到货信息
		  			else if(line.charAt(0)=='A'){
		  				Aitem a=new Aitem();
		  				a.Itemnumber=infor[1];
		  				a.Supplier=infor[2];
		  				a.Description=infor[3];
		  				A.addElement(a);
		  			}//货物添加信息
		  			else if(line.charAt(0)=='D'){
		  				Ditem a=new Ditem();
		  				a.Itemnumber=infor[1];
		  				D.addElement(a);
		  			}//货物删除信息
		  			line=in.readLine();
		  		}
		  		in.close();
		  		//货物操作进行操作
		  		//输出环境的搭建
		  		File fs=new File("./Shipping.txt");
		  		File fe=new File("./Error.txt");
		  		File fn=new File("./NewInventory.txt");
		  		//创建文件
		  		if(!fs.exists()){
		  			fs.createNewFile();
		  		}
		  		if(!fe.exists()){
		  			fe.createNewFile();
		  		}
		  		if(!fn.exists()){
		  			fn.createNewFile();
		  		}
		  		//清空文件内容
		  		out=new PrintWriter(new FileWriter(fs));
		  		out.write("");
		  		out.close();
		  		out=new PrintWriter(new FileWriter(fe));
		  		out.write("");
		  		out.close();
		  		out=new PrintWriter(new FileWriter(fn));
		  		out.write("");
		  		out.close();
		  		out=null;
		  		//正常打开文件
		  		PrintWriter outs =new PrintWriter(new FileWriter(fs,true));
		  		PrintWriter oute =new PrintWriter(new FileWriter(fe,true));
		  		PrintWriter outn =new PrintWriter(new FileWriter(fn,true));
		  		//货物添加
		  		int alen=A.size();
		  		for(int i=0;i<alen;i++){
		  			Aitem a=(Aitem)A.elementAt(i);
		  			Inventory item=new Inventory();
		  			item.Itemnumber=a.Itemnumber;
		  			item.Quantity=0;
		  			item.Supplier=a.Supplier;
		  			item.Description=a.Description;
		  			inv.addElement(item);
		  		}
		  		//到货操作
		  		int rlen=R.size();
		  		for(int i=0;i<rlen;i++){
		  			Ritem r=(Ritem)R.elementAt(i);
		  			len=inv.size();
		  			for(int j=0;j<len;j++){
		  				Inventory item=(Inventory)inv.elementAt(j);
		  				if(r.Itemnumber.equals(item.Itemnumber)){
		  					item.Quantity+=r.Quantity;
		  					inv.set(j,item);
		  					break;
		  				}
		  			}
		  		}
		  		//出货操作
		  		//冒泡排序
		  		len=O.size();
		  		Oitem o1=null,o2=null,o=null;
		  		for(int i=0;i<len;i++){
		  			for(int j=0;j<len-1;j++){
		  				o1=(Oitem)O.elementAt(j);
		  				o2=(Oitem)O.elementAt(j+1);
		  				if(o1.Quantity>o2.Quantity){
		  					O.set(j,o2);
		  					O.set(j+1,o1);
		  				}//交换
		  			}
		  		}
		  		//正常出货
		  		int olen=O.size();
		  		len=inv.size();
		  		for(int i=0;i<olen;i++){
		  			o=O.elementAt(i);
		  			for(int j=0;j<len;j++){
		  				Inventory item=(Inventory)inv.elementAt(j);
		  				if(o.Itemnumber.equals(item.Itemnumber)){
		  					if(o.Quantity<=item.Quantity){
		  						item.Quantity-=o.Quantity;
		  						inv.set(j,item);
		  						line=o.Supplier+'	'+o.Itemnumber+'	'+Integer.toString(o.Quantity)+'\n';
		  						outs.write(line);
		  						break;
		  					}//正常出货
		  					else{
		  						line=o.Supplier+'	'+o.Itemnumber+'	'+Integer.toString(o.Quantity)+'\n';
		  						oute.write(line);
		  						break;
		  					}//出货异常
		  				}
		  			}
		  		}
		  		//删除操作
		  		int dlen=D.size();
		  		Ditem d=null;
		  		for(int i=0;i<dlen;i++){
		  			d=D.elementAt(i);
		  			len=inv.size();
		  			for(int j=0;j<len;j++){
		  				Inventory item=inv.elementAt(j);
		  				if(d.Itemnumber.equals(item.Itemnumber)){
		  					if(item.Quantity==0){
		  						inv.removeElementAt(j);
		  						break;
		  					}//正常删除
		  					else{
		  						line='0'+'	'+item.Itemnumber+'	'+Integer.toString(item.Quantity)+'\n';
		  						oute.write(line);
		  						break;
		  					}//删除异常
		  				}
		  			}
		  		}
		  		//更新仓库
		  		len=inv.size();
		  		Inventory item=null;
		  		for(int i=0;i<len;i++){
		  			item=inv.elementAt(i);
		  			line=item.Itemnumber+'	'+Integer.toString(item.Quantity)+'	'+item.Supplier+'	'+item.Description+'\n';
		  			outn.write(line);
		  		}
		  		//关闭资源
		  		outs.close();
		  		oute.close();
		  		outn.close();		
	  	}catch(IOException e){
	  		e.printStackTrace();
	  	}	
	}
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值