Question

In: Computer Science

Introduction This program will read from a file containing inventory data, and display summary information to...

Introduction This program will read from a file containing inventory data, and display summary information to the user. Instructions and Information For this project, input will be from file, and output will be to standard output (the console). The file will be read, and the information stored in the appropriate data format and data structure. Then, the inventory data will be printed out, listing the frequency of each item next to its name. The frequency means the combined sum of all counts of each item. If an item occurs more than once in the file next to an integer (a count), then that count is added to the combined total, which will be printed out later. If an item is encountered which does not yet exist in the list of items (an ArrayList), then it is to be added as an instance of InventoryItem, a class that you must define, which includes the name and current frequency of occurrence of the item. Input The input will be obtained from a file, named input.txt. The general format of a line will be: ItemName, Count Output The output will be printed to the console, in the general format: ItemName TotalFrequency 2 Example Sample Input File Coke, 5 Pepsi, 4 BBQ Ribs, 7 Coke, 4 Veggie Burgers, 12 Pepsi 6 Sample Output Coke 9 Pepsi 10 BBQ Ribs 7 Veggie Burgers 12 Note that items that only occurred once in the input file only have that single count as their totals, but those that occur multiple times are printed out only once, but have the combined total of all occurrences (which could be 1, 2, 3, 4, or as many times as I want). Hints and Notable Requirements You should consider creating two classes to help you solve the problem: • InventoryItem o Represents a single, unique inventory item group o Maintains the name of the item and the frequency of occurrence of the item (the total count) o You should have a constructor to set the name and original frequency when first encountered o You should have standard setters and getters for the fields o You should have a method, named increaseBy that takes a single parameter, the amount to increase the internal count (frequency) by if lines additional to the original line are encountered o There should be NO print statements inside this class • InventoryItemDemo o This class will contain the main method o You must read the file o You must create instances of InventoryItem, and store them in an ArrayList o If you encounter the name of an item that already exists in the array list, you find that inventory item and increase its count appropriately o Once all information is read from the file and all the objects are created, you should iterate through them and print out their total frequencies

Answer required in java

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________


// InventoryData.txt

Coke, 5
Pepsi, 4
BBQ Ribs, 7
Coke, 4
Veggie Burgers, 12
Pepsi, 6

________________

// InventoryItem.java

public class InventoryItem {
   //Declaring instance variables
   private String name;
   private int count;

   //Parameterized constructor
   public InventoryItem(String name, int count) {
       this.name = name;
       this.count = count;
   }

   // getters and setters
   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getCount() {
       return count;
   }

   public void setCount(int count) {
       this.count = count;
   }

   public void increaseBy(int count) {
       this.count += count;
   }

}
_____________________

// InventoryItemDemo.java

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

public class InventoryItemDemo {

   public static void main(String[] args) {
       String name,str;
       int count;
       ArrayList<InventoryItem> arls=new ArrayList<InventoryItem>();
try {
       Scanner sc=new Scanner(new File("InventoryData.txt"));
       while(sc.hasNext())
       {
           str=sc.nextLine();
           String arr[]=str.split(", ");
           InventoryItem i=new InventoryItem(arr[0],Integer.parseInt(arr[1]));
           for(int j=0;j<arls.size();j++)
           {
               if(arls.get(j).getName().equals(arr[0]))
               {
                   arls.get(j).increaseBy(Integer.parseInt(arr[1]));
               }
           }
           arls.add(i);
          
       }
       sc.close();
      
       System.out.println("Item\t\tFrequency");
       System.out.println("----\t\t--------");
       for(int i=0;i<arls.size();i++)
       {
           System.out.printf("%-15s%10d\n",arls.get(i).getName(),arls.get(i).getCount());
       }
   } catch (FileNotFoundException e) {
      
       e.printStackTrace();
   }

   }

}
__________________________

Output:

Item       Frequency
----       --------
Coke 9
Pepsi 10
BBQ Ribs 7
Coke 4
Veggie Burgers 12
Pepsi 6

_______________Could you plz rate me well.Thank You


Related Solutions

Write a C++ program to read a data file containing the velocity of cars crossing an...
Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing. Data to use. 10,15,20,25,30,35,40,45,50,55. Please use something basic.
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
Write a C program that will read different data types from the following file and store...
Write a C program that will read different data types from the following file and store it in the array of structures. Given file: (This file have more than 1000 lines of similar data): time latitude longitude depth mag magType nst gap dmin 2020-10-19T23:28:33.400Z 61.342 -147.3997 12.3 1.6 ml 12 84 0.00021 2020-10-19T23:26:49.460Z 38.838501 -122.82684 1.54 0.57 md 11 81 0.006757 2020-10-19T23:17:28.720Z 35.0501667 -117.6545 0.29 1.51 ml 17 77 0.1205 2020-10-19T22:47:44.770Z 38.187 -117.7385 10.8 1.5 ml 15 100.22 0.049 2020-10-19T22:42:26.224Z...
Write a C++ program that will display the top internet stories from the stories.txt file The...
Write a C++ program that will display the top internet stories from the stories.txt file The program must display the stories that have a score which the statistical "mode". The "mode" of a set of values is the value that occurs most often (with the greatest frequency) The data about the stories is in a file that contains the following: storyTitle    (a sequence of numbers and/or letters, may contain spaces in it) storyURL    (a regular URL, like http://www.twitter.com/story1,without spaces) score        (an integer number,...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT