This code represents a simple computer shop program that allows...

December 8, 2023 at 02:19 AM

import javax.swing.JOptionPane; public class TotalSalesofProduct { private static final int[] MAX_STOCK = { 5, 5, 5, 5, 5 }; private static final double[] PRICES = { 100.00, 2500.00, 200.00, 300.00, 1500.00 }; private static final String[] PRODUCT_DESCRIPTIONS = { "Mouse", "Monitor", "Keyboard", "Flash Disk", "Hard Disk" }; public static void main(String[] args) { double totalAmount = 0.0; while (true) { String newCustomerInput = JOptionPane.showInputDialog("New customer? (y/n)"); if (newCustomerInput != null) { if (newCustomerInput.equalsIgnoreCase("y")) { totalAmount = processProductSelection(totalAmount); } else if (newCustomerInput.equalsIgnoreCase("n")) { JOptionPane.showMessageDialog(null, "Thank you for using ITCC105 Computer Shop."); break; } else { JOptionPane.showMessageDialog(null, "Invalid input. Please enter 'y' or 'n'."); } } else { showThankYouMessage(totalAmount); break; } } } private static double processProductSelection(double totalAmount) { StringBuilder receipt = new StringBuilder("Receipt:\n"); while (true) { StringBuilder productOptions = new StringBuilder(); for (int i = 1; i <= 5; i++) { if (MAX_STOCK[i - 1] > 0) { String productCode = "A00" + (i + 0); productOptions.append(productCode).append(". ").append(PRODUCT_DESCRIPTIONS[i - 1]) .append(" - $").append(PRICES[i - 1]).append("\n"); } } if (productOptions.length() == 0) { JOptionPane.showMessageDialog(null, "OUT OF PRODUCT"); break; } String userInput = JOptionPane.showInputDialog( "Select a product:\n" + productOptions + "0. Finish and calculate total"); if (userInput == null || userInput.equals("0")) { break; } else { try { int choice = Integer.parseInt(userInput.substring(3)); if (choice < 1 || choice > 5 || MAX_STOCK[choice - 1] <= 0) { JOptionPane.showMessageDialog(null, "OUT OF PRODUCT"); continue; } String quantityInput = JOptionPane .showInputDialog("Enter quantity for Product " + userInput + ": "); int quantity = Integer.parseInt(quantityInput); if (quantity > MAX_STOCK[choice - 1]) { JOptionPane.showMessageDialog(null, "OUT OF PRODUCT"); continue; } double productTotal = PRICES[choice - 1] * quantity; receipt.append(userInput).append(" Product - $").append(PRICES[choice - 1]) .append(" x ") .append(quantity).append(" = $").append(productTotal).append("\n"); MAX_STOCK[choice - 1] -= quantity; totalAmount += productTotal; } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Invalid input. Please enter a valid product code."); continue; } } } JOptionPane.showMessageDialog(null, receipt.toString() + "\nThank you for using ITCC105 Computer Shop.\nYour total amount is: $" + totalAmount); return totalAmount; } private static void showThankYouMessage(double totalAmount) { JOptionPane.showMessageDialog(null, "Thank you for using ITCC105 Computer Shop.\nYour total amount is: $" + totalAmount); } }

This code represents a simple computer shop program that allows customers to select products and calculate the total amount of their purchase.

The program starts by initializing arrays for the maximum stock, prices, and descriptions of the products.

In the main method, the program enters a loop that asks the user if there is a new customer. If the user enters 'y', the program calls the processProductSelection method to handle the product selection process. If the user enters 'n', a message is displayed and the program ends. If the user enters any other input, an error message is displayed. If the user cancels the input dialog, a thank you message is displayed and the program ends.

The processProductSelection method handles the product selection process. It displays a list of available products and their prices. The user can select a product by entering the corresponding product code. If the user enters '0', the process ends. If the user enters an invalid product code or a product that is out of stock, an error message is displayed. The user is then prompted to enter the quantity of the selected product. If the quantity is greater than the available stock, an error message is displayed. If the input is valid, the program calculates the total amount for the selected product, updates the stock, and adds the product to the receipt.

After the product selection process ends, the receipt and the total amount are displayed in a message dialog.

The showThankYouMessage method displays a thank you message along with the total amount.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node