Write a Java program that will create a report to display the top three mobile device sales per month from January to March 2018. The rows and columns represent the monthly sales of each device. JAN FEB MAR TOTAL IPhone 7 30 15 35 80 Samsung S8 20 25 30 75 Huawei Mate 10 25 11 32 68 MONTHLY TOTAL 75 51 97 Using a Two-Dimensional array, produce the monthly mobile device sales report and the total sales for each device.
public class Main {
public static void main(String[] args) {
String[] header = {"NAME", "JAN", "FEB", "MAR", "TOTAL"};
String[] names = {"IPhone 7", "Samsung S8", "Huawei Mate 10"};
int[][] sales = {{30, 15, 35, 80}, {20, 25, 30, 75}, {25, 11, 32, 68}};
int[] monthlyTotal = {75, 51, 97};
for (int i = 0; i < header.length; i++) {
if (i == 0) {
System.out.printf("%-17s ", header[i]);
} else {
System.out.printf("%-8s ", header[i]);
}
}
System.out.println();
for (int i = 0; i < sales.length; i++) {
System.out.printf("%-17s ", names[i]);
for (int j = 0; j < sales[i].length; j++) {
System.out.printf("%-8s ", sales[i][j]);
}
System.out.println();
}
System.out.printf("\n\n%-17s ", "MONTHLY TOTAL");
for (int i = 0; i < monthlyTotal.length; i++) {
System.out.printf("%-8s ", monthlyTotal[i]);
}
System.out.println();
}
}
Comments
Leave a comment