Write a java program that specifies four parallel one dimensional arrays to store the product name, quantity and cost plus ItemTotal. Each array should be capable of holding a number elements provided by user input. Using a for loop input values for length and width arrays. The entries in the area arrays should be the corresponding values for the product, which is to be used to calculate the costs as follows (thus, itemTotat[i] = quantity [i]* cost [i]) after data has been entered display the following receipt as output:
Sample Run1
Enter the number of products: 2
Enter product 1 name, Qty and Cost: Banana 4 2.65
Enter product 1 name, Qty and Cost: Coke 1 9.18
Output1:
Product Quantity Cost Total
---------- ----------- ------- --------
Banana 4 2.65 10.60
coke 1 9.18 9.18
Sub-Total 19.78
VAT(15%) 2.97
TOTAL N$ 22.75
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of products: ");
String[] name = new String[in.nextInt()];
int[] quantity = new int[name.length];
double[] cost = new double[name.length];
double[] itemTotal = new double[name.length];
for (int i = 0; i < name.length; i++) {
System.out.print("Enter product " + (i + 1) + " name, Qty and Cost: ");
name[i] = in.next();
quantity[i] = in.nextInt();
cost[i] = in.nextDouble();
itemTotal[i] = quantity[i] * cost[i];
}
System.out.printf("%-10s%-10s%-10s%-10s\n", "Product", "Quantity", "Cost", "Total");
System.out.printf("%-10s%-10s%-10s%-10s\n", "-------", "--------", "----", "-----");
double subTotal = 0;
for (int i = 0; i < name.length; i++) {
System.out.printf("%-10s%-10d%-10.2f%-10.2f\n", name[i], quantity[i], cost[i], itemTotal[i]);
subTotal += itemTotal[i];
}
System.out.printf("%-10s%-10s%-10s%-10.2f\n", "Sub-Total", "", "", subTotal);
System.out.printf("%-10s%-10s%-10s%-10.2f\n", "VAT(15%)", "", "", subTotal*0.15);
System.out.printf("%-10s%-10s%-10sN$ %-10.2f\n", "TOTAL", "", "", subTotal*1.15);
}
}
Comments
Leave a comment