My assignment is this:
My trouble is the main method. I can read the file fine but I cannot figure out how to use the data to create new Sales and Inventory objects. My code for all 3 classes is below. Any help is appreciated, thank you in advance.Build two classes, called Inventory and Sales. The Sales class should have the following attributes:
an integer value called productCode
an integer value called quantitySold
There should be get and set methods for each value, as well as a constructor that takes a String and builds a Sales object. This constructor will search the String for a comma, take the beginning of the string through the Integer.parseInt method to create the product code, and take the end of the string through the Integer.parseInt method to get the quantity sold.
The Inventory class should have the following attributes:
an integer value called productCode
a double value called Price
an integer value called quantityOnHand
Again, design get and set methods for each value, as well as a constructor that takes a String with three parts separated by commas: an int for the product code, a double for the price, and an int for the quantity on hand. Also create a method called postSale that takes in an int for the quantity sold and subtracts the quantity from the quantity on hand.
Write a main program that reads the files Sorted.txt containing data for Sales, and Inventory.txt that contains data for Inventory objects. Each line from a file should create a new Sales or Inventory object. Produce a report on the console showing each Inventory object and its new quantity on hand. If any sales items are found that don't have a corresponding inventory item, print an error message that says "Product code xxxx not found". Be sure to handle the case where two or more sales are made for the same product code.
import java.util.Scanner; import java.io.*; public class Main { public static void main(String[] arg) throws IOException { String str; Scanner fileScan, strScan; fileScan = new Scanner (new File("SoldSorted.txt")); while (fileScan.hasNext()) { str = fileScan.nextLine (); System.out.println (); strScan = new Scanner (str); strScan.useDelimiter(","); while (strScan.hasNext()) System.out.println (" " + strScan.next()); System.out.println(); } } }
public class Sales { private int productCode; private int quantitySold; //------------------------------------------------------------------------------------------------ // Constructor: //------------------------------------------------------------------------------------------------ public Sales (String codeSold) { productCode = Integer.parseInt(codeSold); quantitySold = Integer.parseInt(codeSold); } //----------------------------------------------------------------------------------------------------- // Returns the product code. //----------------------------------------------------------------------------------------------------- public int getCode () { return productCode; } //----------------------------------------------------------------------------------------------------- // Sets the product code. //----------------------------------------------------------------------------------------------------- public int setCode (int code) { productCode = code; return productCode; } //----------------------------------------------------------------------------------------------------- // Returns the quantity sold. //----------------------------------------------------------------------------------------------------- public int getSold () { return quantitySold; } //----------------------------------------------------------------------------------------------------- // Sets the quantity sold. //----------------------------------------------------------------------------------------------------- public int setSold (int sold) { quantitySold = sold; return quantitySold; } }
public class Inventory { private int productCode; private double price; private int quantityOnHand; //---------------------------------------------------------------------------------------------------- // Constructor: //---------------------------------------------------------------------------------------------------- public Inventory (int code, double cost, int onHand) { productCode = code; price = cost; quantityOnHand = onHand; } //----------------------------------------------------------------------------------------------------- // Returns the product code. //----------------------------------------------------------------------------------------------------- public int getCode () { return productCode; } //----------------------------------------------------------------------------------------------------- // Sets the product code. //----------------------------------------------------------------------------------------------------- public int setCode (int code) { productCode = code; return productCode; } //----------------------------------------------------------------------------------------------------- // Returns the price. //----------------------------------------------------------------------------------------------------- public double getPrice () { return price; } //----------------------------------------------------------------------------------------------------- // Sets the price. //----------------------------------------------------------------------------------------------------- public double setPrice (double price) { this.price = price; return price; } //----------------------------------------------------------------------------------------------------- // Returns the quantity on hand. //----------------------------------------------------------------------------------------------------- public int getOnHand () { return quantityOnHand; } //----------------------------------------------------------------------------------------------------- // Sets the quantity on hand. //----------------------------------------------------------------------------------------------------- public int setOnHand (int onHand) { quantityOnHand = onHand; return quantityOnHand; } //----------------------------------------------------------------------------------------------------- // Subtracts quantity sold from quantity on hand and returns new on hand amount. //----------------------------------------------------------------------------------------------------- public int postSale (int sold) { quantityOnHand = quantityOnHand - sold; return quantityOnHand; } }
The Inventory.txt file looks like this:
1168,4.25,10
1305,1.80,42
1345,12.56,16
1388,7.42,30
1480,6.54,80
1495,8.36,48
1560,15.27,65
The SalesSorted.txt file looks like this:
1168,16
1305,27
1345,9
1356,10
1388,16
1388,24
1680,36