Experiment 2 Goods Purchase and Sale Management System

Write an Inventory.java to complete the following functions:

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

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

Field

Format and meaning

Item number

String type, cargo number

Quantity

Integer, quantity of goods

Supplier

String type, supplier number

Description

String type, cargo description

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

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

5. A transaction beginning with'O' (the first letter of Order) indicates that this is a shipment order, that is, a certain kind of goods should be sent to a specific customer.
The format of Item number and Quantity is as defined in the table above. The Custom number is the same as the Supplier number above. Processing an order record (a transaction beginning with'O') means to reduce the quantity of the corresponding goods in the inventory record (reduced quantity = quantity in the invoice), and record the shipping information to Shipping.txt. Note: The quantity in Inventory.txt should not be less than 0. If the inventory quantity is less than the quantity of the invoice for a certain kind of goods, the system should stop processing the invoice and record the error message to Errors.txt. If there are multiple invoices for a certain kind of goods, and the total inventory is less than the sum of these invoices, the system should satisfy the customer according to the limited quantity in the invoice from small to large. In other words, for a certain kind of goods, if an invoice with a small quantity is not processed before, the invoice with a large quantity will never be processed. (This processing principle is not affected by the order in which the invoice is recorded in Transactions.txt)

6. A transaction beginning with'R' indicates that this is an arrival order record. After the'R' is the Item number and its quantity Quanlity. Processing an arrival order means increasing the quantity of the corresponding goods in the inventory (increased quantity = quantity in the arrival order). Note: If in the Transactions.txt file, the delivery note appears after the delivery note, the quantity of goods in the delivery note can be used to fill the quantity in the delivery note (it can be understood as in Transactions.txt, priority processing to manifest).

7. A transaction starting with "A" means adding a new kind of goods to the inventory (that is, this kind of goods did not exist in the inventory before). After the "A" is the Item number, the supplier and the description of the goods. Processing a new item record means adding a new Item with a Quantity of 0 to the inventory. You can assume that in a Transactions.txt, the new goods record always appears before the first arrival order.

8. A transaction starting with'D' means to delete a kind of goods from the inventory, and the Item number is after the'D'. The delete operation is always processed after all transactions are processed to ensure that the operation of the invoice for the same kind of goods that may appear can be processed correctly before the deletion. If the inventory quantity of a certain kind of goods to be deleted is not 0, the system should record error information in Errors.txt.

9. Each line in the file Shipping.txt represents the shipping information to a certain customer. Each line in Shipping.txt is customer number, item number, and quantity of goods, separated by tab key. If there are two records with the same customer number and item number in the invoice, the two pieces of shipping information should be merged in Shipping.txt (that is, their quantities are added together).

10. The Errors.txt file contains unsent shipping records and deleted records with inventory greater than 0. Each line of Errors.txt contains the Custom number, Item number and Quantity on the invoice. For the delete operation, the Custom number is 0, and the quantity Quntity is the Quantity in the inventory.

11. Experimental test data:

Inventory.txt

Transactions.txt

package test;
public class Inventory{
 String Item;
 int Quantity;
 String Supplier;
 String Description;
 Inventory(String _Item,int _Quantity,String _Supplier,String _Description){
  Item=_Item;
  Quantity=_Quantity;
  Supplier=_Supplier;
  Description=_Description;
 }
}
package test;
public class transaction implements Comparable<Object>{
 String type;
 String Item;
 int num;
 String customer;
 transaction(String _type,String _Item,int _num,String _customer){
  type=_type;
  Item=_Item;
  num=_num;
  customer=_customer;
 }
 @Override
 public int compareTo(Object o) {
  transaction t=(transaction)o;
  return num-t.num;
 }
}
package test;
import java.io.*;
import java.util.Collections;
import java.util.Vector;
public class example {
 private static BufferedReader reader;
 private static BufferedReader reader2;
 public static void main(String[] args) throws Exception{
  Vector<Inventory> invVector=readInventory("d:/Inventory.txt");   //读入Inventory并存入Vector中,每个元素为一项库存
  Vector<transaction> tVector=readTransaction("d:/transaction.txt");//读入transaction并存入Vector中,每个元素为一个事件
  Vector<transaction> oVector=new Vector<transaction>();
  for(int i=0;i<tVector.size();i++) {
   if(tVector.get(i).type.equals("R")) {      //先处理R事件,即入库操作
    for(int j=0;j<invVector.size();j++) {
     if(invVector.get(j).Item.equals(tVector.get(i).Item)) {
      invVector.get(j).Quantity+=tVector.get(i).num;
      break;
     }
    }
   }else if(tVector.get(i).type.equals("A")) {            //A和R的优先级相同,同为入库操作,都是首先处理
    invVector.add(new Inventory(tVector.get(i).Item,0,Integer.toString(tVector.get(i).num),tVector.get(i).customer));
   }else if(tVector.get(i).type.equals("O")) {   //O为出库操作,应待A和R处理完成后在处理
    oVector.add(tVector.get(i)); //为了方便为O事件排序,即先处理出货量少的,当遍历查询到O时,存入新Vector,等待滞后处理
   }
  }
  Collections.sort(oVector);       //为O事件排序,即出货量少的订单优先级更高
  for(int i=0;i<oVector.size();i++) {      //开始遍历处理O事件     
   for(int j=0;j<invVector.size();j++) {
    if(invVector.get(j).Item.equals(oVector.get(i).Item)) {  //若库存量>=出货量,则执行O操作
     if(invVector.get(j).Quantity>=oVector.get(i).num) {
      invVector.get(j).Quantity-=oVector.get(i).num;
     }else {
      FileOutputStream fos=new FileOutputStream("d:/Errors.txt"); //若库存量<出货量,则说明该O操作违规,读入到d:/Errors.txt中
      OutputStreamWriter writer=new OutputStreamWriter(fos,"UTF-8");
      BufferedWriter bwriter=new BufferedWriter(writer);
      bwriter.write(oVector.get(i).type+"\t"+oVector.get(i).Item+"\t"+oVector.get(i).num+"\t"+oVector.get(i).customer);
      bwriter.newLine();
      bwriter.flush();
      bwriter.close();
     }
    }
   }
  }
  for(int i=0;i<tVector.size();i++) {     //最后处理D操作,即删除某货物
   if(tVector.get(i).type.equals("D")) {
    for(int j=0;j<invVector.size();j++) {
     if(invVector.get(j).Item.equals(tVector.get(i).Item)) {
      invVector.remove(j);
     }
    }
   }
  }
  File file =new File("d:/Inventory.txt");
        try {             //把原d:/Inventory.txt清空
            if(!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileOutputStream fos=new FileOutputStream("d:/Inventory.txt");//把新的库存信息存入到d:/Inventory.txt中
  OutputStreamWriter writer=new OutputStreamWriter(fos,"GBK");
  BufferedWriter bwriter=new BufferedWriter(writer);
        for(int i=0;i<invVector.size();i++) {
      bwriter.write(invVector.get(i).Item+"\t"+invVector.get(i).Quantity+"\t"+invVector.get(i).Supplier+"\t"+invVector.get(i).Description);
      bwriter.newLine();
        }
        bwriter.flush();bwriter.close();
 } 
 private static Vector<Inventory> readInventory(String filename) throws Exception{ //读 Inventory   
  Vector<Inventory> invVector=new Vector<Inventory>();
  reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
  String line="";
  while((line=reader2.readLine())!=null) {   
   String[] temp=line.split("\t");    //通过String.split()函数把每条库存的每项数据通过"\t"分开,为读操作做准备
   if(temp.length==4) {
    invVector.add(new Inventory(temp[0],Integer.parseInt(temp[1]),temp[2],temp[3]));
   }
  }
  return invVector;
 }
 private static Vector<transaction> readTransaction(String filename) throws Exception{
  Vector<transaction> tVector=new Vector<transaction>();
  reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
  String line="";
  while((line=reader.readLine())!=null) {
   String[] temp=line.split("\t");   //通过String.split()函数把事件包含的数据通过"\t"分开,为读操作做准备
   if(temp.length==3) {       //当temp.length==3,说明该事件为R
    tVector.add(new transaction(temp[0],temp[1],Integer.parseInt(temp[2]),""));
   }else if(temp.length==4){    //当temp.length==4,说明该事件为O或A
    tVector.add(new transaction(temp[0],temp[1],Integer.parseInt(temp[2]),temp[3]));
   }
   else if(temp.length==2){    当temp.length==2,说明该事件为D
    tVector.add(new transaction(temp[0],temp[1],0,""));
   }
  }
  return tVector;
 }
}

Guess you like

Origin blog.csdn.net/Warmmm/article/details/106984719