Objective 1: Create a Java Application called ReceiptOutput . This Java Applicat
ID: 3839370 • Letter: O
Question
Objective 1:
Create a Java Application 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.
Step 1:
Use the following template ReceiptOutput.java:
Step 2:
Define a class ProductInformation, which contains three instance variables:
String productName;
int productQuantity;
double productPrice;
Step 3:
1. Define a class NoExtryException, which extends Exception.
2. Follow the instructions dictated by the comments /* */
Objective 2:
Create a Java Application called ReceiptInput. This Java Application will read in the text file that you just created in Debugging Objective 1, then output the data to the screen.
Step 4:
Use the following template ReceiptInput.java:
Step 5:
1. Use the FileNotFoundException to determine the end of file input.
2. Follow the instructions dictated by the comments /* */
Explanation / Answer
ReceiptOutput.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReceiptOutput
{
static final int MAX_PRODUCTS = 5;
public static void main(String[] args)
{
String outputFile = "D: eceipt.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(new File(outputFile));
}
catch ( FileNotFoundException e )
{
System.out.println( "FileNotFoundException: Output File <" + outputFile + "> Does Not Exist" );
System.exit(0);
}
output = new PrintWriter( outputStream );
try{
/* Add a try block to throw the NoEntryException if no ProductName is entered */
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(productArray[productCount].productName.trim().equals("")){
throw new NoEntryException("");
}
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++;
}
}
/* Add a catch block to catch the NoEntryException */
catch( NoEntryException e )
{
System.out.println("no ProductName is entered ");
}
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].productName+","+productArray[lp].productQuantity+","+productArray[lp].productPrice+" ");
}
output.close();
}
}
NoEntryException.java
public class NoEntryException extends Exception{
String errorMsg ;
public NoEntryException(String s){
this.errorMsg = s;
}
public String toString(){
return (errorMsg ) ;
}
}
ProductInformation.java
public class ProductInformation {
public String productName;
public int productQuantity;
public double productPrice;
public ProductInformation() {
}
}
Output:
Enter Product Name for Receipt ( <CR> to Quit ): aaa
Enter Product Quantity: 1
Enter Product Price : 11.1
Enter Product Name for Receipt ( <CR> to Quit ): bbbb
Enter Product Quantity: 2
Enter Product Price : 22.2
Enter Product Name for Receipt ( <CR> to Quit ):
no ProductName is entered
Number of Products: 2
ReceiptInput.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ReceiptInput
{
public static void main(String[] args)
{
String fileName = "D: eceipt.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 ( true )
{
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 [aaa], Count [1], Price [11.10]
Product [bbbb], Count [2], Price [22.20]
End of File Reached
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.