//TransactionReader.java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.util.Scanner;

public class TransactionReader
{
    public static void main(String[] args)
    {
        String fileName = "Transactions.txt";
        try
        {
            Scanner inputStream = new Scanner(new File(fileName));
            String line = inputStream.nextLine(); //Read header line
            double totalSales = 0;
            System.out.println();
            //Read the rest of the file, line by line
            while (inputStream.hasNextLine())
            {
				line = inputStream.nextLine();
				//Extract SKU(Stock Keeping Unit),quantity,price,description
				String[] items = line.split(",");
				String SKU = items[0];
				int quantity = Integer.parseInt(items[1]);
				double price = Double.parseDouble(items[2]);
				String description = items[3];
				System.out.printf("Sold %d of %s (SKU: %s) at $%1.2f each.\n",
					quantity, description, SKU, price);
				totalSales += quantity * price;
			}
			System.out.printf("Total sales: $%1.2f\n", totalSales);
            inputStream.close( );
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error opening the file " + fileName + ".");
        }
        catch(IOException e)
        {
            System.out.println("Problem with input file " + fileName + ".");
        }
    }
}

