In: Computer Science
Java program
In the main(), create and load a 2-dimentional array to hold the following sales data
Number of Tacos Sold (hard code the data into the array when you create it)
Truck3 Truck4 . Truck5
Monday 250 334 229
Wednesday 390 145 298
Friday . 434 285 . 156
• Write a method to calculate the grand total of all Tacos sold
Write a method to allow the user to enter a truck number and get the total sales for that truck
Write a method to allow the user to enter a Day and get the total sales for that day.
Invoke all of the methods from the main
Validate all entries the user makes
Comment your code
write the grand total to a .txt file
If you have any problem with the code feel free to comment.
Program
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws
IOException {
Scanner sc = new
Scanner(System.in);
//hardcoded 2d array
int[][] tacosSold = { { 250, 334,
229 }, { 390, 145, 298 }, { 434, 285, 156 } };
String str;
//taking input and pasing it o the
methods
System.out.print("Enter Truck
number: ");
str = sc.nextLine();
truckTotalSales(tacosSold,
str);
System.out.print("Enter Day:
");
str = sc.nextLine();
dayTotalSales(tacosSold,
str);
grandTotal(tacosSold);
sc.close();
}
private static void grandTotal(int[][] tacosSold)
throws IOException {
int sum =0;
for(int i=0; i<tacosSold.length;
i++) {
for(int j=0;
j<tacosSold.length; j++) {
sum+=tacosSold[i][j];//calculating grad
total
}
}
System.out.println("Grand total:
$"+sum);
//change the name of the file and
path accordingly
BufferedWriter bw = new
BufferedWriter(new FileWriter("output.txt"));
bw.write("Grand total:
$"+sum);
bw.close();
}
private static void truckTotalSales(int[][]
tacosSold, String truckNumber) {
int sum =0;
int index = 0;
//indetifying the index
if(truckNumber.equalsIgnoreCase("truck3"))
index = 0;
if(truckNumber.equalsIgnoreCase("truck4"))
index = 1;
if(truckNumber.equalsIgnoreCase("truck5"))
index = 2;
for(int i=0; i<tacosSold.length;
i++) {
sum+=tacosSold[i][index];//calculating track number total
}
System.out.println("Total sales for
"+truckNumber+": $"+sum);
}
private static void dayTotalSales(int[][] tacosSold,
String day) {
int sum =0;
int index = 0;
//indentifying index
if(day.equalsIgnoreCase("monday"))
index = 0;
if(day.equalsIgnoreCase("wednesday"))
index = 1;
if(day.equalsIgnoreCase("friday"))
index = 2;
for(int i=0; i<tacosSold.length;
i++) {
sum+=tacosSold[index][i];//calculating day total
}
System.out.println("Total sales for
"+day+": $"+sum);
}
}
Output