Question: Below is my following homework assignment. I am having trouble getting the average of the single item price and the count of all the items
Below is my following homework assignment. I am having trouble getting the average of the single item price and the count of all the items in the quantity. I will attach what my code looks like currently below the assignment. Thanks ahead of time for any help! This is in java by the way.
Part 1: Create a program that creates a text file from user input with the following data, which is a list of tools, prices and quantities. There should be 3 columns of data, including 1 String for toolName, 1 double for toolPrice, and 1 int for toolQty.
| Hammer | 15.00 | 5 | |
| Drill | 49.99 | 3 | |
| Screwdriver | 3.00 | 6 | |
| Wrench | 6.25 | 4 | |
| Pliers | 4.00 | 2 | |
| Ratchet | 12.50 | 1 | |
| Chisel | 8.50 | 7 | |
| Prybar | 7.25 | 4 | |
| Saw | 14.50 | 6 | |
| Sander | 27.99 | 2 |
Part 2: Create a program which reads the data file created in Part 1, and displays it as a table report with headings and a 4th calculated column.
| Item | Price | Qty | Ext Price |
| Hammer | 15.00 | 5 | 75 |
| Drill | 49.99 | 3 | 149.97 |
| Screwdriver | 3.00 | 6 | 18 |
| Wrench | 6.25 | 4 | 25 |
| Pliers | 4.00 | 2 | 8 |
| Ratchet | 12.50 | 1 | 12.5 |
| Chisel | 8.50 | 7 | 59.5 |
| Prybar | 7.25 | 4 | 29 |
| Saw | 14.50 | 6 | 87 |
| Sander | 27.99 | 2 | 55.98 |
Also, after the table is printed, include a sum of the Ext. Price at the end, an average of the Price column, and a count of the Qy of items. Sample output:
Total of Ext Price: $999.99
Average Single Item Price: $99.99
Count of all items Qty: 99
Below is my code
import java.util.*;
import java.io.*;
public class WriteReadFilept2
{
public static void main(String[] args) throws IOException
{
//var
FileInputStream inFile = null;
Scanner scFile = null;
Scanner kb = new Scanner(System.in);
String toolName = "";
double toolPrice = 0.0;
double toolTotal= 0.0;
int toolQuantity = 0;
String fileName = "";
double totalToolPrice = 0.0;
int totalToolQuantity = 0;
//enter file name and open
System.out.print("Enter input filename: ");
fileName = kb.next();
//attempting to open
System.out.println("Opening tool list file");
String hddFileName = "D:\\assign8Folder\\" + fileName;
inFile = new FileInputStream(hddFileName);
//inFile = new FileInputStream("D:/assign8Folder/" + fileName);
scFile = new Scanner(inFile);
//reading file
System.out.println("Reading tool list data.");
//header
System.out.format("%10s%10s%10s%15s ", "Item", "Price", "Quantity", "Extend Price");
//read file, display
while(scFile.hasNext())
{
toolName = scFile.next();
toolPrice = scFile.nextDouble();
toolQuantity = scFile.nextInt();
System.out.format("%10s%8.2f%8d%15.2f ", toolName, toolPrice, toolQuantity, toolPrice * toolQuantity);
toolTotal += toolPrice * toolQuantity;
}
//display total for tools
System.out.format(" Total of all tools entended price: %10.2f ", toolTotal);
//aver of single item
totalToolPrice += toolPrice;
totalToolPrice /= toolQuantity;
System.out.format("Average price of single item: %10.2f ", totalToolPrice);
//total tool quantity
totalToolQuantity += toolQuantity;
System.out.format("Count of all items: %10d ", totalToolQuantity);
//close file
System.out.println("Closing tool list file.");
inFile.close();
kb.close();
scFile.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
