货物进销管理系统

货物进销管理系统

一.实验目的

    1.掌握Java中文件的读写操作。

    2.学会使用Java提供的实用类(Vector, ArrayList)来完成特定的功能。

    3.掌握字符串类(String, StringBuffer)的使用。

    4.掌握用面向对象的方法分析和解决复杂问题。

二.实验内容

    编写一个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

 接下来上传本人的代码:

package inventory;

import java.io.*;
import java.util.*;

class Goods{               //商品类
	String Item;           //货物编号
	int Quantity;          //货物数量
	String Supplier;       //供应商编号
	String Description;    //货物描述
}

class Thing{                  //事件类
	String thing_type;        //事件编号               
	String Item;            //货物编号
	int Quantity;           //货物数量
	String Supplier;        //供应商编号
	String Description;    //货物描述
}


public class Inventory {
	
	static Vector<Goods> v_goods=new Vector<Goods>(); //创建Goods对象数组
	static Vector<Thing> O_thing=new Vector<Thing>(); //创建Thing的到货(O)事件对象数组
	static Vector<Thing> R_thing=new Vector<Thing>(); //创建Thing的到货(R)事件对象数组
	static Vector<Thing> D_thing=new Vector<Thing>(); //创建Thing的到货(D)事件对象数组
	static Vector<Thing> A_thing=new Vector<Thing>(); //创建Thing的到货(A)事件对象数组
	
	
	static RandomAccessFile out_Shipping=null;        
	static RandomAccessFile out_Errors=null;
	static RandomAccessFile in=null;
	static RandomAccessFile out_NewInventory=null;
	
	static void read_Inventory() {       //读取库存记录
		
		try {
			in=new RandomAccessFile("Inventory.txt","r");
			String str1=null;
			while((str1=in.readLine())!=null) {
				String str2[]=str1.split("\\s+"); //分解字符串
				
				Goods goods_temp=new Goods();
				goods_temp.Item=str2[0];
				goods_temp.Quantity=Integer.parseInt(str2[1]);
				goods_temp.Supplier=str2[2];
				goods_temp.Description=str2[3];
				v_goods.addElement(goods_temp);
			}
			
			in.close();
		}
		catch(IOException e) {
			System.out.println(e);
		}
	}
	
	static void read_Transactions() {         //读取事件信息
		
		try {
			in=new RandomAccessFile("Transactions.txt","r");
			String str1=null;
			while((str1=in.readLine())!=null) {
				String str2[]=str1.split("\\s+"); //分解字符串
				
				Thing thing_temp=new Thing();
				if(str2[0].equals("O")) {
					
					thing_temp.Item=str2[1];
					thing_temp.Quantity=Integer.parseInt(str2[2]);
					thing_temp.Supplier=str2[3];
					
					int O_thing_size=O_thing.size();
					int O_thing_index=O_thing_size-1;
					
					if(O_thing_size==0)
						O_thing.addElement(thing_temp);
					else {
						while(O_thing_index>0) {          
							if((O_thing.elementAt(O_thing_index).Quantity)>(thing_temp.Quantity))
								O_thing_index--;
						}
						
						//将发货事件进行排序后插入
						O_thing.insertElementAt(thing_temp, O_thing_index);
					}
					
				}
				else if(str2[0].equals("R")) {
					thing_temp.Item=str2[1];
					thing_temp.Quantity=Integer.parseInt(str2[2]);
					R_thing.addElement(thing_temp);
				}
				else if(str2[0].equals("D")) {
					thing_temp.Item=str2[1];
					D_thing.addElement(thing_temp);
				}
				else {
					thing_temp.Item=str2[1];
					thing_temp.Supplier=str2[2];
					thing_temp.Description=str2[3];
					A_thing.addElement(thing_temp);
				}
			}
			in.close();          
		}
		catch(IOException e) {
			System.out.println(e);
		}
	}
	
	static void handle_A_thing() {               //处理新添货物事件
		for(int i=0;i<A_thing.size();i++) {
			
			Goods goods_temp=new Goods();
			goods_temp.Item=A_thing.elementAt(i).Item;
			goods_temp.Quantity=0;
			goods_temp.Supplier=A_thing.elementAt(i).Supplier;
			goods_temp.Description=A_thing.elementAt(i).Description;
			
			//按商品编号从小到大插入
			for(int j=0;j<v_goods.size();j++) {
				if(Integer.parseInt(goods_temp.Item)<Integer.parseInt(v_goods.elementAt(j).Item)) {
					v_goods.insertElementAt(goods_temp, j);
					break;
				}
			}
		}
	}
	
	static void handle_R_thing() {            //处理到货事件
		for(int i=0;i<R_thing.size();i++) {
			for(int j=0;j<v_goods.size();j++) {
				if(R_thing.elementAt(i).Item.equals(v_goods.elementAt(j).Item)) {  //找到对应的商品位置
					v_goods.elementAt(j).Quantity+=R_thing.elementAt(i).Quantity;
				}
			}
		}
	}
	
	static void handle_O_thing() {             //处理发货事件
		
		Vector<Goods> S_goods=new Vector<Goods>();  //发货单 数组
		
		try {
			out_Shipping=new RandomAccessFile("Shipping.txt","rw"); //发货信息文件
			out_Errors=new RandomAccessFile("Errors.txt","rw");     //错误信息文件
		} 
		catch(IOException e) {
			System.out.println(e);
		}
		
		for(int i=0;i<O_thing.size();i++) {
			for(int j=0;j<v_goods.size();j++) {
				if(O_thing.elementAt(i).Item.equals(v_goods.elementAt(j).Item)) { //找到对应的商品位置
					if(v_goods.elementAt(j).Quantity>=O_thing.elementAt(i).Quantity) {//判断商品数量是否够
						//数量够
						v_goods.elementAt(j).Quantity-=O_thing.elementAt(i).Quantity; //库存量减去发货的数量
						
						boolean b=false;         //标志 判断此次发货信息是否被记录
						
						//先将发货信息存入发货单数组
						for(int k=0;k<S_goods.size();k++) {  //判断是否有客户和物品一样的发货信息
							if((O_thing.elementAt(i).Supplier.equals(S_goods.elementAt(k).Supplier))&&(O_thing.elementAt(i).Item.equals(S_goods.elementAt(k).Item))) {
								S_goods.elementAt(k).Quantity+=O_thing.elementAt(i).Quantity;    //相同信息合并
								b=true;
							}
						}
						if(!b) {
							
							Goods goods_temp=new Goods();
							goods_temp.Item=O_thing.elementAt(i).Item;
							goods_temp.Quantity=O_thing.elementAt(i).Quantity;
							goods_temp.Supplier=O_thing.elementAt(i).Supplier;
							S_goods.addElement(goods_temp);
						}
					}
					else {           //商品数量不够,将错误信息写入Errors.txt
						try {
							out_Errors.writeBytes(O_thing.elementAt(i).Supplier+"\t"+O_thing.elementAt(i).Item+"\t"+O_thing.elementAt(i).Quantity+"\n");
						} catch (IOException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();
						}
					}
				}
				
			}
		}
		//写入发货单
		try {
			for(int i=0;i<S_goods.size();i++) {
				out_Shipping.writeBytes(S_goods.elementAt(i).Supplier+"\t"+S_goods.elementAt(i).Item+"\t"+S_goods.elementAt(i).Quantity+"\n");
			}
			
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		try {
			out_Shipping.close();   
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}
	
	static void handle_D_thing() {        //删除商品
		for(int i=0;i<D_thing.size();i++) {
			for(int j=0;j<v_goods.size();j++) {
				if(D_thing.elementAt(i).Item.equals(v_goods.elementAt(j).Item)) 
				{
					if(v_goods.elementAt(j).Quantity!=0)       //货物数量不为0,记录错误到Errors.txt
						try {
							out_Errors.writeBytes(D_thing.elementAt(i).Supplier+"\t"+D_thing.elementAt(i).Item+"\t"+D_thing.elementAt(i).Quantity+"\n");
						} catch (IOException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();
						}
					v_goods.removeElement(v_goods.elementAt(j));   //删除元素
				}
			}
		}
		
		try {
			out_Errors.close();      //关闭
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	
	static void w_NewInventory() {        //将货物信息读入NewInventory.txt
		
		try {
			out_NewInventory=new RandomAccessFile("NewInventory.txt","rw");
		} catch (FileNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		for(int i=0;i<v_goods.size();i++)
			try {         //写入货物信息
				out_NewInventory.writeBytes(v_goods.elementAt(i).Item+"\t"+v_goods.elementAt(i).Quantity+"\t"+v_goods.elementAt(i).Supplier+"\t"+v_goods.elementAt(i).Description+"\n");
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		
		try {
			out_NewInventory.close();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	
	public static void main(String args[]) {
		read_Inventory();          //读取Inventory.txt(商品信息)
		read_Transactions();       //读取Transactions.txt(事件信息)
		handle_A_thing();          //处理添加新商品事件
		handle_R_thing();          //处理到货事件
		handle_O_thing();          //处理发货事件
		handle_D_thing();          //处理删除商品事件
		w_NewInventory();          //商品信息写入NewInventory.txt
	}
}

 

  • 22
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值