Java 货物进销管理系统

实验原理

编写一个Inventory.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’开头(Order的首字母)的事务表示这是一个发货订单,即某一种货物应该发给特定的客户。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
在这里插入图片描述





1.  import java.io.*;  

2.  import java.util.Collections;  

3.  import java.util.List;  

4.  import java.util.Vector;  

5.  import java.util.Comparator;  

6.    

7.  public class Inventory {  

8.      @SuppressWarnings({ "unchecked", "unused" })  

9.      public static void main(String[] args) throws Exception {  

10.        String Inventoryfile = "E:/exp/Inventory.txt";  

11.        String Transactionsfile = "E:/exp/Transactions.txt";  

12.        String Errorsfile = "E:/exp/Errors.txt";  

13.        String NewInventoryfile = "E:/exp/NewInventory.txt";  

14.        String Shippingfile = "E:/exp/Shipping.txt";  

15.        Vector<Inventoryclass> inventory = readinvenctory(Inventoryfile);//定义一个vector数组,用来存储从文件中读取的数据  

16.        Vector<Transactionsclass> transactions = readtransactions(Transactionsfile);//定义一个vector数组,用来存储从文件中读取的数据  

17.        Vector<shipping>shippings = new Vector<shipping>();//定义vector数组,相当于两个文件传输的媒介,用来像数组中添加数据,然后再将数据存储到文件中  

18.        Vector<error>errors = new Vector<error>();//定义vector数组,相当于两个文件传输的媒介,用来像数组中添加数据,然后再将数据存储到文件中  

19.        Collections.sort( transactions );  

20.        //System.out.println(transactions.get(5).desc);  

21.        for(Transactionsclass t:transactions) {//核心部分,是对于transactions文件中的每一行进行遍历  

22.            if(t.type.equals("O")) {//如果事件类型为"O",即是订单类,对其进行处理  

23.                for(Inventoryclass i:inventory) {//对invenctory进行遍历  

24.                    if(t.itemnum.equals(i.itemnum)) {  

25.                        if(t.quantity>i.quantity) {//如果不满足transactions中的货物数量小于等于invenctory中的货物数量将错误信息记录到error文件当中  

26.                            error er = new error(i.supplier,i.itemnum,i.quantity);  

27.                            errors.add(er);  

28.                        }  

29.                        else {//否则,将invenctry中的数量减去对应数量,并将结果添加到shipping中  

30.                            i.quantity=i.quantity-t.quantity;  

31.                            shipping sh= new shipping(t.supplier, t.itemnum, t.quantity);  

32.                            shippings.add(sh);  

33.                        }  

34.                    }  

35.                }  

36.            }  

37.            else if(t.type.equals("R")) {//如果事件类型为"R",即是到货单记录,对其进行处理  

38.                for(Inventoryclass i:inventory) {//对invenctory进行遍历  

39.                    if(t.itemnum.equals(i.itemnum)) {//invencory中添加对应数量  

40.                        i.quantity+=t.quantity;                   

41.                    }  

42.                }  

43.            }  

44.            else if(t.type.equals("A")) {//如果事件类型为"A",即是增加新货记录,对其进行处理  

45.                Inventoryclass in = new Inventoryclass(t.itemnum, 0, t.supplier, t.desc);//将invenctory中新加入货物  

46.                inventory.add(in);  

47.            }  

48.            else if(t.type.equals("D")) {//如果事件类型为"D",即是删除货物记录,对其进行处理  

49.              

50.                for(int i = inventory.size()-1;i>=0;i--) {  

51.                    if(t.itemnum.equals(inventory.get(i).itemnum)) {  

52.                        if (inventory.get(i).quantity!=0) {//如果invenctory中的货物数量为0,则不能将此种货物而删除,并记录出错信息到errors  

53.                            error er = new error(inventory.get(i).supplier,inventory.get(i).itemnum,inventory.get(i).quantity);  

54.                            errors.add(er);                       

55.                        } else {  

56.  

57.                            inventory.remove(i);  

58.                        }  

59.                    }  

60.                }  

61.                  

62.            }  

63.              

64.            writeS(shippings,Shippingfile);//将shippings中的记录添加到shippingfile文件当中  

65.            WriteE(errors,Errorsfile);//将inventory中的记录添加到inventory文件当中  

66.            WriteN(inventory,NewInventoryfile);//将inventory中的记录添加到inventory文件当中  

67.              

68.        }  

69.    }  

70.    private static void WriteN(Vector<Inventoryclass> inventory, String NewInventoryfile) throws Exception {//将inventory中的记录添加到newinventory文件当中  

71.        BufferedWriter wri = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(NewInventoryfile)));  

72.        for(int i=0;i<inventory.size();i++) {  

73.            wri.write(inventory.get(i).itemnum);  

74.            wri.write ("\t");  

75.            wri.write(Integer.toString(inventory.get(i).quantity));  

76.            wri.write ("\t");  

77.            wri.write (inventory.get(i).supplier);  

78.            wri.write ("\t");  

79.            wri.write(inventory.get(i).desc);  

80.            wri.write ("\r\n");  

81.        }  

82.        wri.flush();  

83.        wri.close();  

84.          

85.    }  

86.    private static void WriteE(Vector<error> errors, String errorsfile) throws Exception {//将inventory中的记录添加到inventory文件当中  

87.        BufferedWriter wri = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(errorsfile)));  

88.        for(int i=0;i<errors.size();i++) {  

89.            wri.write(errors.get(i).Cust);  

90.            wri.write ("\t");  

91.            wri.write (errors.get(i).item);  

92.            wri.write ("\t");  

93.            wri.write(Integer.toString(errors.get(i).quantity));  

94.            wri.write ("\r\n");  

95.        }  

96.        wri.flush();  

97.        wri.close();  

98.          

99.    }  

100.     private static void writeS(Vector<shipping> shippings, String shippingfile) throws Exception {//将shippings中的记录添加到shippingfile文件当中  

101.           

102.         BufferedWriter wri = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(shippingfile)));  

103.         for(int i = 0;i<shippings.size();i++) {  

104.             wri.write(shippings.get(i).itemNum);  

105.             wri.write ("\t");  

106.             wri.write(Integer.toString(shippings.get(i).quantity));  

107.             wri.write ("\t");  

108.             wri.write (shippings.get(i).customer);  

109.             wri.write ("\r\n");  

110.         }  

111.           

112.         wri.flush();  

113.         wri.close();  

114.     }  

115.     @SuppressWarnings("unused")  

116.     private static Vector<shipping> readshipping(String errorsfile) {  

117.           

118.         return null;  

119.     }  

120.     private static Vector<Transactionsclass> readtransactions(String transactionsfile) throws Exception {//定义transactions的vector类,对transactins中的数据进行读取  

121.         Vector<Transactionsclass> traVectory = new Vector<Transactionsclass>();  

122.         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(transactionsfile)));  

123.         String line = "";  

124.         while((line=reader.readLine())!=null) {  

125.             String[] tmp=line.split("\t");  

126.             Transactionsclass tra;  

127.             if(tmp[0].equals("O")) {  

128.                 tra = new Transactionsclass(tmp[0],tmp[1],Integer.parseInt(tmp[2]),tmp[3],"");  

129.                 traVectory.add(tra);  

130.             }  

131.             else if(tmp[0].equals("R")) {  

132.                 tra = new Transactionsclass(tmp[0],tmp[1],Integer.parseInt(tmp[2]),"","");  

133.                 traVectory.add(tra);  

134.             }  

135.             else if(tmp[0].equals("A")){  

136.                 tra = new Transactionsclass(tmp[0],tmp[1],0,tmp[2],tmp[3]);  

137.                 traVectory.add(tra);  

138.             }  

139.             else{  

140.                 tra = new Transactionsclass(tmp[0],tmp[1],0,"","");  

141.                 traVectory.add(tra);  

142.             }  

143.               

144.         }  

145.         reader.close();  

146.         return traVectory;  

147.     }  

148.     private static Vector<Inventoryclass> readinvenctory(String Inventoryfile) throws Exception{//定义transactions的vector类,对transactins中的数据进行读取  

149.         Vector<Inventoryclass> invVectory = new Vector<Inventoryclass>();  

150.         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(Inventoryfile)));  

151.         String line = "";  

152.         while((line=reader.readLine())!=null) {  

153.             String tmp[]=line.split("\t");  

154.             Inventoryclass inv = new Inventoryclass(tmp[0],Integer.parseInt(tmp[1]),tmp[2],tmp[3]);  

155.             invVectory.add(inv);  

156.         }  

157.         reader.close();  

158.         return invVectory;  

159.     }  

160.       

161.       

162. }  

163. class Inventoryclass{  

164.     String itemnum;  

165.     int quantity;  

166.     String supplier;  

167.     String desc;  

168.     public Inventoryclass(String itemnum,int quantity,String supplier,String desc) {  

169.         this.itemnum = itemnum;  

170.         this.quantity = quantity;  

171.         this.supplier = supplier;  

172.         this.desc = desc;  

173.     }  

174. }  

175. @SuppressWarnings("rawtypes")  

176. class Transactionsclass implements Comparable{//继承compareble接口,transactions使用sort方法  

177.     String itemnum;  

178.     int quantity;  

179.     String supplier;  

180.     String desc;  

181.     String type;  

182.     public Transactionsclass(String type,String itemnum,int quantity,String supplier,String desc) {  

183.         this.type = type;  

184.         this.itemnum = itemnum;  

185.         this.quantity = quantity;  

186.         this.supplier = supplier;  

187.         this.desc = desc;  

188.           

189.     }  

190.     @Override  

191.     public int compareTo(Object arg0) {//对transactions事件中的处理顺序设置优先级,设置sort方法的依据  

192.         Transactionsclass s1=(Transactionsclass)arg0;  

193.         int p1=0,p2=0;  

194.         if(s1.type.equals("A")) {  

195.             p1=3;  

196.         }  

197.         else if(s1.type.equals("R")) {  

198.             p1=2;  

199.         }  

200.         else if(s1.type.equals("O")) {  

201.             p1=1;  

202.         }  

203.         else if(s1.type.equals("D")) {  

204.             p1=0;  

205.         }  

206.           

207.         if(this.type.equals("A")) {  

208.             p2=3;  

209.         }  

210.         else if(this.type.equals("R")) {  

211.             p2=2;  

212.         }  

213.         else if(this.type.equals("O")) {  

214.             p2=1;  

215.         }  

216.         else if(this.type.equals("D")) {  

217.             p2=0;  

218.         }  

219.         if(p1==p2) {  

220.             return this.quantity-s1.quantity;  

221.         }  

222.         else {  

223.             return p1-p2;  

224.         }  

225.   

226.                 }  

227.   

228.       

229.     }  

230. class shipping{  

231.     String customer;  

232.     String itemNum;  

233.     int quantity;  

234.     public shipping(String customer,String itemNum,int quantity) {  

235.         this.customer=customer;   

236.         this.itemNum=itemNum;  

237.         this.quantity=quantity;  

238. }  

239. }  

240.   

241.   

242. class error{  

243.     String Cust;  

244.     String item;  

245.     int quantity;  

246.     public error(String Cust,String item,int quantity) {  

247.         this.Cust=Cust;  

248.         this.item=item;  

249.         this.quantity=quantity;   

250. }  

251. }  



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值