Question #296077

Write a program to implement the following: create a class representing a

company where a company has a minimum and maximum limit of the

quantity of products. Allow the user to order from the company product

list (create a list of any ten products). Apply proper constraint if user

order more than the quantity available with the company. Based on the

quantity generate the customer bill.


Expert's answer

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> products = new ArrayList<>();
        products.add("Potato");
        products.add("Tomato");
        products.add("Onion");
        products.add("Cabbage");
        products.add("Carrot");
        products.add("Cucumber");
        products.add("Garlic");
        products.add("Bell Pepper");
        products.add("Eggplant");
        products.add("Pumpkin");
        double[] quantities = {100, 85, 45, 55, 60, 53, 32, 23, 120, 90};
        double[] prices = {1.5, 1.9, 1.6, 1.78, 1.56, 2.3, 1.32, 1.44, 1.65, 1.48};
        double[] shopCart = new double[products.size()];
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("\nSELECT:");
            for (int i = 0; i < products.size(); i++) {
                System.out.println(i + 1 + ". " + products.get(i) + " " + quantities[i] + "kg " + prices[i] + "$ per kg");
            }
            System.out.println("0. Purchase");
            System.out.println("Choice: ");
            int choice = in.nextInt();
            if (choice == 0) {
                StringBuilder builder = new StringBuilder();
                for (int i = 0, j = 1; i < shopCart.length; i++) {
                    if (shopCart[i] > 0) {
                        builder.append(j++).append(". ").append(products.get(i)).append(" Weight: [");
                        builder.append(shopCart[i]).append("kg] Price per kg: [").append(prices[i]).append("$]");
                        builder.append(" Total: [").append(String.format("%.2f", shopCart[i] * prices[i])).append("$]\n");
                    }
                }
                if (builder.length() > 0) {
                    System.out.println("BILL");
                    System.out.println(builder);
                }
                break;
            }
            System.out.println("How many kg(g): ");
            double weigh = in.nextDouble();
            if (quantities[choice - 1] >= weigh) {
                shopCart[choice - 1] += weigh;
                quantities[choice - 1] -= weigh;
            }
        }
    }
}

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!

LATEST TUTORIALS
APPROVED BY CLIENTS