Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; import java.io.File; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.text.DecimalFormat; public class H3_Q3 { // this function will return the number of unique

import java.util.Scanner; import java.io.File; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.text.DecimalFormat; public class H3_Q3 { // this function will return the number of unique customers who bought "item" public static int getNumberOfCustomersWhoBought (File file, String item) throws Exception { // create an FileReader object to read from the file FileReader fr = new FileReader (file); // create a BufferedReader object to read the file line by line BufferedReader br = new BufferedReader (fr); // initialize important variables // this will store the product name String product = ""; // this will store each line of the file String line = ""; // it will count the number of times the "item" occurred in the list int count = 0; // reading line by line from the file using readLine () function while ((line = br.readLine ()) != null) { // split the line into two strings using split () function // the first argument is the delimiter with respect to which // the split has to be made // the second argument is to specify the number of splits to make String [] arr = line.split ("\t", 2); // first word is the product name product = arr[0]; // if product name matches "item" if (product.equals (item) == true) { // incremente the number of occurences of that "item" ++count; } } return count; } // this function returns the revenue generated by each item public static float getRevenueFromItem (File file, String item) throws Exception { // create an FileReader object to read from the file FileReader fr = new FileReader (file); // create a BufferedReader object to read the file line by line BufferedReader br = new BufferedReader (fr); // this will store the price of product String price = ""; // this will store the number of "item"s String amount = ""; // this will store the product name String product = ""; // this will store each line of the file String line = ""; // find the aggregate revenue generated by the "item" float cost = 0; // reading line by line from the file using readLine () function while ((line = br.readLine ()) != null) { // the first argument is the delimiter with respect to which // the split has to be made String [] arr = line.split ("\t"); // first word is the product name product = arr[0]; // if product name matches "item" if (product.equals (item) == true) { // convert the second string and the third string to float cost += Float.parseFloat (arr[1]) * Float.parseFloat (arr[2]); } } return cost; } public static void main (String [] args) throws Exception { // create a file object File file = new File ("./input_data/ProductInfo.txt"); // get the number of each item sold int saws = getNumberOfCustomersWhoBought (file, "Saw"); int tables = getNumberOfCustomersWhoBought (file, "Table"); int flashlights = getNumberOfCustomersWhoBought (file, "Flashlight"); int batteries = getNumberOfCustomersWhoBought (file, "Battery"); int clamps = getNumberOfCustomersWhoBought (file, "Clamp"); // get the revenue generated from each item float sawRevenue = getRevenueFromItem (file, "Saw"); float tableRevenue = getRevenueFromItem (file, "Table"); float flashlightRevenue = getRevenueFromItem (file, "Flashlight"); float batteryRevenue = getRevenueFromItem (file, "Battery"); float clampRevenue = getRevenueFromItem (file, "Clamp"); // writing into the output file File newFile = new File ("output.txt"); // creating a new file newFile.createNewFile (); // FileWriter is an object which will read the file FileWriter fw = new FileWriter ("output.txt"); // use the write function to directly write into the file using FileWriter // note that the headings columns has been separated by single '\t' character fw.write ("Product Name\tUnique Customers\tTotal Revenue "); // and the rows containing data has been set: // first and second column has been separated by 2 '\t' characters // second and third column has been separated by 3 '\t' characters // The format function of String class has been used to set the US currency format ##.## // "2.2f" f states that the number is float, the 2 before period means there must be 2 digits // before period and and 2 after period means there must be 2 digits after period fw.write ("Saw\t\t" + saws + "\t\t\t" + String.format ("%2.2f", sawRevenue) + " "); fw.write ("Table\t\t" + tables + "\t\t\t" + String.format ("%2.2f", tableRevenue) + " "); fw.write ("Flashlight\t" + flashlights + "\t\t\t" + String.format ("%2.2f", flashlightRevenue) + " "); fw.write ("Battery\t\t" + batteries + "\t\t\t" + String.format ("%2.2f", batteryRevenue) + " "); fw.write ("Clamp\t\t" + clamps + "\t\t\t" + String.format ("%2.2f", clampRevenue) + " "); fw.close (); } }

Question: Change the program! This program is using String arrays, rewrite this without using string arrays. the one rule is no string arrays. (use an approach to read many values per line from the file.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Horse Betting The Road To Absolute Horse Racing 2

Authors: NAKAGAWA,YUKIO

1st Edition

B0CFZN219G, 979-8856410593

More Books

Students also viewed these Databases questions

Question

What are Electrophoresis?

Answered: 1 week ago

Question

=+j Describe how EU directives impact IHRM.

Answered: 1 week ago

Question

=+and reduction in force, and intellectual property.

Answered: 1 week ago