- #1
iamjon.smith
- 117
- 3
Develop an application that reads a file of inventory information containing product ID and quantity. An example inventory file:
10401 32
10522 25
10401 1
10402 15
The file can contain several entries for the same Product ID. Your application should accumulate the quantities for each Product ID as you read the inventory information from the file. The application should print total inventory for each Product ID.
Subclass:
Main class:
I am having a problem getting the values into the array for product quantity, and it only prints out the first element of the tree set. Someone please take a look and see what I am doing wrong.
10401 32
10522 25
10401 1
10402 15
The file can contain several entries for the same Product ID. Your application should accumulate the quantities for each Product ID as you read the inventory information from the file. The application should print total inventory for each Product ID.
Subclass:
Code:
package myInventory;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jon and Jessica
*/
public class Inventory {
private int productID;
private int productQuantity;
// no-argument constructor calls other constructor with default values
public Inventory()
{
this( 0,0 ); // call two-argument constructor
} // end no-argument CityRecord constructor
// initialize a record
public Inventory( int productID, int productQuantity )
{
setProductID(productID);
setProductQuantity(productQuantity);
}
public int getProductID() {
return productID;
}
public void setProductID(int productID) {
this.productID = productID;
}
public int getProductQuantity() {
return productQuantity;
}
public void setProductQuantity(int productQuantity) {
this.productQuantity = productQuantity;
}
}
Main class:
Code:
package myInventory;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.*;
/**
*
* @author Jonathan Smith
*/
public class InventoryTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int productID,productQuantity;
Scanner scanner = null; // scanner to read input
FileOutputStream invOut; // declare a file output object
PrintStream p1; // declare a print stream
TreeSet< Integer > invTree = new TreeSet< Integer >(); // Treeset to contain product data
int[] invQuantity = null; // integer array to hold quantity
Inventory inventory = new Inventory(); // inventory constructor
try{
// Create new file output streams
invOut = new FileOutputStream("currentInventory.txt", true); // output stream for current inventory with boolean value for appending to file
// Connect print streams to the output stream
p1 = new PrintStream( invOut );
scanner = new Scanner(new File("inventory.txt")).useDelimiter("\t"); // Scanner construct that reads the input file
while (scanner.hasNextLine())
{ //Checks if there's any more lines
invTree.add(scanner.nextInt()); // reads to first tab in line, sets the variable
productQuantity = scanner.nextInt(); // reads to integer, sets the variable
invQuantity[productQuantity] = productQuantity; // add product quantity to invQuantity array
if (productQuantity !=0)
{
productQuantity = productQuantity + scanner.nextInt(); // if product quantity contains value add new value to old value
}
else
{
productQuantity = scanner.nextInt(); // else enter value for product quantity
}
}
}
catch (Exception e)
{
System.err.println ("Error writing to file"); // print error to screen
}
printTreeSet(invTree); // call print method to print the inventory tree set
}
// This method prints the elements of a given TreeSet object.
public static void printTreeSet(TreeSet invTree) {
System.out.print( "Inventory: " + "\n" );
// Use an Iterator to print each element of the TreeSet.
Iterator iterator = invTree.iterator();
while (iterator.hasNext())
System.out.print( iterator.next() + "\n" );
System.out.println();
}
}
I am having a problem getting the values into the array for product quantity, and it only prints out the first element of the tree set. Someone please take a look and see what I am doing wrong.