Experiment 2 goods purchase and sale management system

Write a Inventory.java Complete the following functions:

1. The program first opens and reads Inventory.txt All inventory records recorded in and read Transactions.txt , process the transactions contained in this file, and record the delivery records to Shipping.txt , and record the error message to Errors.txt Medium. Last update stock to another file NewInventory.txt Medium.

2. Documents Inventory.txt and NewInventory.txt Each line of contains an inventory record, and no record contains the following field information. These fields are separated by a tab (see the following file format):

field

Format and meaning

Item number

String type, goods No

Quantity

Integer, quantity of goods

Supplier

String type, vendor number

Description

String type, description of goods

3. The field Items are written to the file from the smallest to the largest. Note that the Item number does not need to be consecutive, for example, 800 may follow the Item number 752.

4. Documents Transactions.txt Contains several different processing records (one record per line). Each record begins with an uppercase letter, indicating what type of transaction this record is. After different capital letters are different information formats. All fields are also tab separated (see Transactions.txt File format).

5. Transactions beginning with 'O' (the initial of Order) indicate that this is a delivery Order, that is, a certain kind of goods should be sent to a specific customer. Item
The format of number and quantity is defined in the table above. The Custom number is the same as the Supplier number above. Processing an order record (transactions beginning with 'O') means recording the shipment information to Shipping.txt Medium. be careful: Inventory.txt The quantity in should not be less than 0. If the inventory quantity of a certain kind of goods is less than the quantity of the delivery order, the system should stop the delivery order and record the error information to Errors.txt . If there are multiple shipment documents for a certain kind of goods, and the total inventory is less than the sum of these shipment documents, the system should satisfy the customer according to the principle that the quantity in the shipment document is limited from small to large. That is to say, for a certain kind of goods, if a shipment with less quantity is not processed, the shipment with more quantity will never be processed. (this processing principle is not recorded in the invoice Transactions.txt Influence of order)

6. Transactions beginning with 'R' indicate that this is a arrival record, followed by 'R' by Item number and its quantity quantity. Processing an arrival document means increasing the quantity of corresponding goods in the inventory (increased quantity = quantity in the arrival document). Note: if Transactions.txt In the document, after the arrival document appears in the shipment document, the quantity of goods in the arrival document can be used to fill the quantity in the shipment document Transactions.txt In, priority is given to arrival documents).

7. A transaction starting with 'a' indicates adding a new kind of goods to the inventory (i.e. this kind of goods did not exist in the previous inventory), followed by 'a' is Item number, supplier and description of the goods. Processing a new goods record means adding a new Item with a Quantity of 0 to the inventory. You can assume that Transactions.txt New goods records always appear before the first arrival document.

8. A transaction beginning with 'D' indicates the deletion of an Item from inventory, followed by an Item number. The deletion operation is always processed after all the things are processed, so as to ensure that the operation of the same kind of goods delivery order can be correctly processed before deletion. If the inventory Quantity of a certain goods to be deleted is not 0, the system should Errors.txt Record the error message.

9. Documents Shipping.txt Each line in represents the delivery information to a customer. Shipping.txt Each line in is customer number, Item number and goods quantity, which are separated by tab key. If there are two records with the same customer number and Item number in the delivery note, the Shipping.txt These two shipment information should be combined in (i.e. their quantities are added).

10. Errors.txt The file contains unsent delivery records and deleted records with inventory greater than 0. Errors.txt Each line contains the Custom number, Item number, and Quantity quantity on the invoice. For deletion, the Custom number is 0, and the Quantity quantity 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");   //Read into Inventory and store in Vector, each element is an Inventory
  Vector<transaction> tVector=readTransaction("d:/transaction.txt");//Read in the transaction and store it in the Vector. Each element is an event
  Vector<transaction> oVector=new Vector<transaction>();
  for(int i=0;i<tVector.size();i++) {
   if(tVector.get(i).type.equals("R")) {      //Handle the R event first, i.e. stock in operation
    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")) {            //The priority of A and R are the same. They are the same as receipt operations. They are all processed first
    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 is the outbound operation, which should be processed after A and R processing
    oVector.add(tVector.get(i)); //For the convenience of sorting O events, i.e. processing the small quantity of shipments first, when traversing the query to O, store the new Vector and wait for the lag processing
   }
  }
  Collections.sort(oVector);       //Sort O events, i.e. orders with less shipments have higher priority
  for(int i=0;i<oVector.size();i++) {      //Start traversing O Events
   for(int j=0;j<invVector.size();j++) {
    if(invVector.get(j).Item.equals(oVector.get(i).Item)) {  //If inventory quantity > = shipment quantity, perform O operation
     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"); //If the inventory quantity is less than the shipment quantity, it indicates that the O operation is illegal. Read in to d:/Errors.txt in
      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++) {     //Finally, D operation is processed, i.e. deleting a goods
   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 {             //Original d:/Inventory.txt empty
            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");//Store the new inventory information in d:/Inventory.txt in
  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{ //Read 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");    //adopt String.split The () function separates each item of data in each inventory by "\ t" to prepare for reading
   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");   //adopt String.split The () function separates the data contained in the event by "\ t" in preparation for the read operation
   if(temp.length==3) {       //When temp.length==3 , indicating that the event is R
    tVector.add(new transaction(temp[0],temp[1],Integer.parseInt(temp[2]),""));
   }else if(temp.length==4){    //When temp.length==4 , indicating that the event is O or A
    tVector.add(new transaction(temp[0],temp[1],Integer.parseInt(temp[2]),temp[3]));
   }
   else if(temp.length==2){    ////When temp.length==2 , indicating that the event is D
    tVector.add(new transaction(temp[0],temp[1],0,""));
   }
  }
  return tVector;
 }
}

Tags: less Java

Posted on Sat, 27 Jun 2020 05:27:11 -0400 by utherwun