Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Read .txt file and create objects using data

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Read .txt file and create objects using data

    My assignment is this:

    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.
    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.

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Read .txt file and create objects using data

    What data from the files go into what classes? When you have decided that, then you can make a constructor for each class and pass the data for that class in its constructor.
    data1 = read data
    data2 = read data
    MyClass mc = new MyClass(data1, data2);

    After creating the objects you will need to save them. An arraylist is good for that.
    arraylst.add(mc);
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  2. Read txt file into array and create new output.
    By Margaret_Girl87 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 19th, 2012, 12:08 PM
  3. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  4. Create read-only data
    By marky8264 in forum JDBC & Databases
    Replies: 10
    Last Post: July 31st, 2010, 02:10 PM
  5. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM