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

Help exception handling OOP java - To show the counts of records successfully an

ID: 3780114 • Letter: H

Question

Help exception handling OOP java

- To show the counts of records successfully and unsuccessfully loaded

- To show the average product price of all the products successfully loaded

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
public class ProductRunner {

   public static void main(String[] args) {
      
      
   Scanner fileInput = null;
   try
   {
       fileInput = new Scanner(new File("product.txt"));
   }
   catch (FileNotFoundException e)
   {
       System.out.println("File not found error");
       System.out.println(e);
   }
  
   ProductList[] prods = new ProductList[10];
   int numProducts = 0 ;
   while (fileInput.hasNext())
   {
       try
       {
           int id = fileInput.nextInt();
           String text = fileInput.next();
           double price = fileInput.nextDouble();
           String name = fileInput.next();
           ProductList p = new ProductList(id,text, price,name);
prods[numProducts++] = p;

}
catch (InputMismatchException e)
{
System.out.println("Error 1");
System.out.println(e);
fileInput.next();
}


   }
  

try
{
for (int c = 0 ;c < numProducts; c++)
System.out.println(prods[c]);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("No room in your products!");
}

ProductList s = new ProductList();



  
}
}

public class ProductList {
  
  
   private int id ;
   private String text;
   private double price;
   private String name;
  
  
  
   public ProductList()
   {
      
   }
   public ProductList(int i,String t,double p, String n)
   {
       id = i;
       text = t;
       price = p;
       name = n;
   }
  
public String toString()
{
return String.format("%d %s $%.2f %s ", id, text, price,name );
}

}

Explanation / Answer

/************************ProductRunner.java**************/

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

public class ProductRunner {
   public static void main(String[] args) {
       // variable for count of unsuccessful loaded record
       int unsuccessfulLoaded = 0;
       Scanner fileInput = null;
       try {
           // reading file
           fileInput = new Scanner(new File("product.txt"));
       } catch (FileNotFoundException e) {
           System.out.println("File not found error");
           System.out.println(e);
       }
       // array of productList for storage
       ProductList[] prods = new ProductList[10];
       int numProducts = 0;
       // reading file content
       while (fileInput.hasNext()) {
           try {
               int id = fileInput.nextInt();
               String text = fileInput.next();
               double price = fileInput.nextDouble();
               String name = fileInput.next();
               ProductList p = new ProductList(id, text, price, name);
               // storing into product list array
               prods[numProducts++] = p;

           } catch (InputMismatchException e) {
               System.out.println("Error 1");
               System.out.println(e);
               unsuccessfulLoaded++;
               fileInput.next();
           }

       }

       try {
           // printing successful loaded record and unsuccessful loaded record
           System.out.println("Total SuccessFul Loaded Records: " + numProducts);
           System.out.println("Total UnSuccessful Loaded records: " + unsuccessfulLoaded);
           double totalPrice = 0;
           // calculating total price
           for (int c = 0; c < numProducts; c++) {
               ProductList list = prods[c];
               totalPrice += list.getPrice();
           }
           // printing average product price
           System.out.println(
                   "Average product price of all the products successfully loaded:" + (totalPrice / numProducts));

       } catch (ArrayIndexOutOfBoundsException e) {
           System.out.println("No room in your products!");
       }

      

   }
}


/*************************ProductList.java***************/

public class ProductList {
   // class variable
   private int id;
   private String text;
   private double price;
   private String name;

   // default constructor
   public ProductList() {

   }

   // parameterized constructor
   public ProductList(int i, String t, double p, String n) {
       id = i;
       text = t;
       price = p;
       name = n;
   }

   // getter of allclass variables
   public int getId() {
       return id;
   }

   public String getText() {
       return text;
   }

   public double getPrice() {
       return price;
   }

   public String getName() {
       return name;
   }

   public String toString() {
       return String.format("%d %s $%.2f %s ", id, text, price, name);
   }
}


Note: Please keep product.txt file in same folder for test and provide same file for test

Thanks a lot