Answer to Question #268821 in Java | JSP | JSF for Chakalaka

Question #268821

Sale-Receipt: 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 products2

Enter product 1, Qty and CostBanana 4 2.65

Enter product 2, 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


1
Expert's answer
2021-11-19T17:09:51-0500
package com.task;

import java.util.Scanner;

public class Main {

    private static String[] name;
    private static Integer[] quantity;
    private static Double[] cost;
    private static Double[] itemTotal;

    private static String space = "    ";
    private static String column1 = "Product";
    private static String column2 = "Quantity";
    private static String column3 = "Cost";
    private static String column4 = "Total";

    public static void main(String[] args) {
        System.out.print("Enter the number of products: ");
        Scanner sc = new Scanner(System.in);
        Integer length = Integer.valueOf(sc.nextLine());
        initArrays(length);
        String[] prodLine;
        for (int i = 0; i < length; i++) {
            System.out.print("Enter product " + (i+1) + ", Qty and Cost: ");
            prodLine = sc.nextLine().split(" ");
            name[i] = prodLine[0];
            quantity[i] = Integer.valueOf(prodLine[1]);
            cost[i] = Double.valueOf(prodLine[2]);
            itemTotal[i] = quantity[i]*cost[i];
        }
        printout();
    }

    private static void initArrays(int length) {
        name = new String[length];
        quantity = new Integer[length];
        cost = new Double[length];
        itemTotal = new Double[length];
    }

    private static void printout() {
        printLine(column1, column2, column3, column4);
        printLine(underscoreString(column1.length()),
                underscoreString(column2.length()),
                underscoreString(column3.length()),
                underscoreString(column4.length()));
        Double subTotal = 0d;
        for (int i = 0; i < name.length; i++) {
            printLine(name[i], quantity[i].toString(), cost[i].toString(), itemTotal[i].toString());
            subTotal = subTotal + itemTotal[i];
        }
        printLine("Sub-Total", "", "", subTotal.toString());
        Double vat = subTotal*0.15;
        printLine("VAT(15%)", "", "", vat.toString());
        printLine("TOTAL", "", "", String.valueOf(subTotal+vat));
    }

    private static void printLine(String col1, String col2, String col3, String col4) {
        System.out.println(padString(col1, 9) + space
                + padString(col2, column2.length()) + space
                + padString(col3, column3.length()) + space
                + padString(col4, column4.length()));
    }

    private static String padString(String input, Integer length) {
        return String.format("%1$" + length + "s", input);
    }

    private static String underscoreString(Integer length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append('_');
        }
        return sb.toString();
    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS