Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 Debugging Exercise Create a Java Application using Netbeans, called ReceiptOut

ID: 3697669 • Letter: 1

Question

1

Debugging Exercise

Create a Java Application using Netbeans, called ReceiptOutput. This Java Application will prompt the user to enter product information for up to five different products, then output the data to a comma separated text file.

Download the Java Application template, ReceiptOutput.template.java, from Blackboard under Week 15. Copy and Paste the source code into your newly created Java Application.

Define a class ProductInformation, which contains three instance variables:

String              productName;

int                    productQuantity;

double            productPrice;

Define a class NoExtryException, which extends Exception.

Follow the instructions dictated by the comments /* */

2

Programming Exercise

Create a Java Application using Netbeans, called ReceiptInput. This Java Application will read in the text file that you just created in Debugging Exercise #1, then output the data to the screen.

Download the Java Application template, ReceiptInput.template.java, from Blackboard under Week 16. Copy and Paste the source code into your newly created Java Application.

Use the FileNotFoundException to determine the end of file input.

Follow the instructions dictated by the comments /* */

ReceiptInput

1

Debugging Exercise

Create a Java Application using Netbeans, called ReceiptOutput. This Java Application will prompt the user to enter product information for up to five different products, then output the data to a comma separated text file.

Download the Java Application template, ReceiptOutput.template.java, from Blackboard under Week 15. Copy and Paste the source code into your newly created Java Application.

Define a class ProductInformation, which contains three instance variables:

String              productName;

int                    productQuantity;

double            productPrice;

Define a class NoExtryException, which extends Exception.

Follow the instructions dictated by the comments /* */

2

Programming Exercise

Create a Java Application using Netbeans, called ReceiptInput. This Java Application will read in the text file that you just created in Debugging Exercise #1, then output the data to the screen.

Download the Java Application template, ReceiptInput.template.java, from Blackboard under Week 16. Copy and Paste the source code into your newly created Java Application.

Use the FileNotFoundException to determine the end of file input.

Follow the instructions dictated by the comments /* */

Explanation / Answer

package receiptoutput;

import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.FileNotFoundException;

public class ReceiptOutput {
   static final int MAX_PRODUCTS = 5;

   public static void main(String[] args) {
       String outputFile = "c:/cs182/receipt.txt";
       FileOutputStream outputStream = null;
       PrintWriter output = null;
       Scanner keyboard = new Scanner(System.in);
       int productCount = 0;
       ProductInformation[] productArray = new ProductInformation[MAX_PRODUCTS];
       try {
           /* Instantiate FileOutputStream class and create outputStream object */
           outputStream = new FileOutputStream(outputFile);

       } catch (FileNotFoundException e) {
           System.out.println("FileNotFoundException: Output File <"
                   + outputFile + "> Does Not Exist");
           System.exit(0);
       }
       output = new PrintWriter(outputStream);

       /*
       * Add a try block to throw the NoEntryException if no ProductName is
       * entered
       */
       try {
           while (productCount < productArray.length) {
               productArray[productCount] = new ProductInformation();

               System.out
                       .print("Enter Product Name for Receipt ( <CR> to Quit ): ");
               productArray[productCount].productName = keyboard.nextLine();
               /* Determine if no productName is entered */
               /* If no productName, throw NoEntryException */
               if ("".equals(productArray[productCount].productName)) {

                   throw new NoEntryException("No entry ");

               }

               if ("CR".equals(productArray[productCount].productName)) {

                   break;

               }

               System.out.print("Enter Product Quantity: ");
               productArray[productCount].productQuantity = keyboard.nextInt();
               System.out.print("Enter Product Price : ");
               productArray[productCount].productPrice = keyboard.nextDouble();
               keyboard.nextLine();
               productCount++;
           }
       } catch (NoEntryException e) {
           e.printStackTrace();
       }
       System.out.println("Number of Products: " + productCount);
       for (int lp = 0; lp < productCount; lp++) {
           /*
           * Add code to output the product Name, Quantity and Price to the
           * output file, separated by commas
           */
           output.write(productArray[lp].toString());
           if (!(lp == productCount - 1))
               output.write(" ");
       }
       output.close();
   }
}

package receiptoutput;

public class ProductInformation {
   String productName;
   int productQuantity;
   double productPrice;

   public ProductInformation() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param productName
   * @param productQuantity
   * @param productPrice
   */
   public ProductInformation(String productName, int productQuantity,
           double productPrice) {
       this.productName = productName;
       this.productQuantity = productQuantity;
       this.productPrice = productPrice;
   }

   /**
   * @return the productName
   */
   public String getProductName() {
       return productName;
   }

   /**
   * @param productName
   * the productName to set
   */
   public void setProductName(String productName) {
       this.productName = productName;
   }

   /**
   * @return the productQuantity
   */
   public int getProductQuantity() {
       return productQuantity;
   }

   /**
   * @param productQuantity
   * the productQuantity to set
   */
   public void setProductQuantity(int productQuantity) {
       this.productQuantity = productQuantity;
   }

   /**
   * @return the productPrice
   */
   public double getProductPrice() {
       return productPrice;
   }

   /**
   * @param productPrice
   * the productPrice to set
   */
   public void setProductPrice(double productPrice) {
       this.productPrice = productPrice;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return productName + "," + productQuantity + "," + productPrice;
   }

}

package receiptoutput;

/**
* @author srinu
*
*/
public class NoEntryException extends Exception {

   public NoEntryException() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param message
   */
   public NoEntryException(String message) {
       // TODO Auto-generated constructor stub

       super(message);
   }

}

OUTPUT 1: if file is not exist
FileNotFoundException: Output File <c:/cs182/receipt.txt> Does Not Exist

OUTPUT 2: if file is exist
Enter Product Name for Receipt ( <CR> to Quit ): ProdName1
Enter Product Quantity: 12
Enter Product Price : 332.32
Enter Product Name for Receipt ( <CR> to Quit ): ProdName2
Enter Product Quantity: 3
Enter Product Price : 433.12
Enter Product Name for Receipt ( <CR> to Quit ): CR
Number of Products: 2

c:/cs182/receipt.txt

ProdName1,12,332.32
ProdName2,3,433.12

package receiptinput;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;

/* Add appropriate import for required java.io.* */

public class ReceiptInput {
   public static void main(String[] args) {
       String fileName = "c:/cs182/receipt.txt";
       File inputFile;
       Scanner fileInput = null;
       String inputLine;
       String[] inputArray;
       String productName;
       int productCount;
       double productPrice;

       inputFile = new File(fileName);
       try {
           /*
           * Instatiate the Scanner class of type inputFile and create the
           * fileInput object
           */
           fileInput = new Scanner(inputFile);
       } catch (FileNotFoundException e) {
           System.out.println("FileNotFoundException: Input File <" + fileName
                   + "> Does Not Exist");
           System.exit(0);
       }

       while (fileInput.hasNext()) {
           try {
               /* Read a line from the text file and assign it to inputLine */
               /* Error will throw NoSuchElementException */
               inputLine = fileInput.nextLine();

               inputArray = inputLine.split(",");

               // Assign array[0] to productName
               productName = inputArray[0];

               /* Parse array[1] and store to integer variable productCount */
               productCount = Integer.parseInt(inputArray[1]);

               /* Parse array[2] and store to double variable productPrice */
               productPrice = Double.parseDouble(inputArray[2]);
               System.out.printf("Product [%s], Count [%d], Price [%.2f] ",
                       productName, productCount, productPrice);
           } catch (NoSuchElementException e) {
               System.out.println("End of File Reached");
               System.exit(0);
           }
       }
   }
}

OUTPUT:
Product [ProdName1], Count [12], Price [332.32]
Product [ProdName2], Count [3], Price [433.12]