Write a Java program to summarize the data from the input file sprocketorders.tx
ID: 3553904 • Letter: W
Question
Write a Java program to summarize the data from the input file sprocketorders.txt and output a report similar to the one below:
Spacely Sprockets
Taking Sprockets into the Future
Sales Summary Report
Sprocket Number Total Quantity Sold
1 90
2 155
3 50
4 300
5 100
The data in this chart comes from the .txt file I named above. The info contained in this txt file is as such:
3 50
2 20
2 100
5 15
1 90
5 85
4 300
2 35
3 100
The report needs to appear in the output window.
I want to use the switch structure. This is the switch structure fragment I have:
switch (snum)
{
case 1: part1total = part1total + quantity;
break;
case 2: part2total = part2total + quantity;
break;
case 3: part3total = part3total + quantity;
break;
case 4: part4total = part4total + quantity;
break;
case 5: part5total = part5total + quantity;
break;
default: System.out.println("Bad sprocket number");
}
This is the code I have so far to establish that it is inputting from a file:
package spacely.sprockets;
public class SpacelySprockets
{
public static void main(String[] args)
{
InputFile orderinfo;
orderinfo = new InputFile("sprocketorders.txt");
}
}
How can I use the switch structure summarize the data from the txt file and output the report? I also need to make sure it incorporates the InputFile code I already have written.
Explanation / Answer
package spacely.sprockets;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SpacelySprockets {
public static void main(String[] args) {
File orderinfo;
orderinfo = new File("sprocketorders.txt");
int part1total = 0, part2total = 0, part3total = 0, part4total = 0, part5total = 0;
try {
Scanner in = new Scanner(orderinfo);
while (in.hasNext()) {
int quantity = 0;
int snum = in.nextInt();
if (in.hasNext()) {
quantity = in.nextInt();
} else {
System.err.println("Data not complete.");
}
switch (snum) {
case 1:
part1total = part1total + quantity;
break;
case 2:
part2total = part2total + quantity;
break;
case 3:
part3total = part3total + quantity;
break;
case 4:
part4total = part4total + quantity;
break;
case 5:
part5total = part5total + quantity;
break;
default:
System.out.println("Bad sprocket number");
}
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out
.println(" Spacely Sprockets Taking Sprockets into the Future Sales Summary Report Sprocket Number Total Quantity Sold");
for (int snum = 1; snum < 6; snum++) {
switch (snum) {
case 1:
System.out.println(" " + snum + " " + part1total);
break;
case 2:
System.out.println(" " + snum + " " + part2total);
break;
case 3:
System.out.println(" " + snum + " " + part3total);
break;
case 4:
System.out.println(" " + snum + " " + part4total);
break;
case 5:
System.out.println(" " + snum + " " + part5total);
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.