I'm currently working on a program that generates a CLI receipt in the same way that any other shop would. However, I'm looking for recommendations on how to improve the code I developed for inputting and outputting the data as a receipt.
I didn't include the complete code; instead, I highlighted the bits that were relevant to my inquiry.
Explanation:
I made several distinct arrays, each with its own set of data. The first array is similar to temporary storage in that it stores all of the input values for each input query. The second array has all of the products in the shop, while the fourth array contains the prices of each product, with their elements aligned. Finally, the third array holds all of the questions that were asked in the given program.
Moving on to the screenInput() method, I simply sought to loop through the questions array, storing all of its input in the answer[] array. The data is just output by the print receipt.
I provided the expected output of the program below the code.
Specified Code:
// Arrays for products, prices, questions and answers.
private static String[] answers = new String[10];
private static final String[] products = { "Wintermelon", "Orea Cheescake", "Matcha Cheescake", "Cream Cheese Cocoa", "Okinawa Milk Tea" };
private static final String[] questions = { "Username: ", "Password: ", "\nCashier Name : ", "Customer Name: ", "\nPurchase: ", "Quantity: ", "Money: ", "Option: " };
private static final int[] prices = { 150, 140, 130, 120, 110 };
// Method of asking inputs
public static void screenInput() {
System.out.println("\n[Welcome to our store!]\n");
/* Loops through the questions array to display all questions
then stores all inputs in answer array */
for (int ctr1 = 0; ctr1 < questions.length; ctr1++) {
String input;
switch (ctr1) {
case 1:
char[] pwdInput = console.readPassword(questions[ctr1]);
answers[ctr1] = String.valueOf(pwdInput);
if (answers[0].equals(usr) && answers[1].equals(pwd)) break;
else System.exit(0);
case 4:
System.out.println("\nSelect an item to purchase:\n");
for (int ctr2 = 0; ctr2 < products.length; ctr2++) System.out.printf("[%d] %s\n", (ctr2 + 1), products[ctr2]);
input = console.readLine(questions[ctr1]);
answers[ctr1] = String.valueOf(Integer.parseInt(input) - 1);
break;
case 7:
System.out.println("\nSelect Option:\n\n[p] Print Receipt\n[x] Logout\n");
input = console.readLine(questions[ctr1]);
answers[ctr1] = input;
if (input.equalsIgnoreCase("p")) {
printReceipt();
break;
} else System.exit(0);
default:
input = console.readLine(questions[ctr1]);
answers[ctr1] = input;
}
}
}
// Method for outputting the receipt
public static void printReceipt() {
double quantity = Integer.parseInt(answers[5]);
double price = (prices[Integer.parseInt(answers[4])]);
double cash = Integer.parseInt(answers[6]);
double total = quantity * price;
double change = cash - total;
System.out.println("\n=================\n MILK TEA SHOP\n=================\n");
System.out.println("Cashier : " + answers[2]);
System.out.println("Customer : " + answers[3]);
System.out.printf("\nPurchased:\n\n%s (P%s) x %s\n\n",
products[Integer.parseInt(answers[4])],
prices[Integer.parseInt(answers[4])],
answers[5]);
System.out.printf("Total : %.2f\n", total);
System.out.printf("Cash : %.2f\n", cash);
System.out.printf("Change : %.2f\n\n", change);
}
Expected Output:
[Welcome to our store!]
Username: [input]
Password: [input]
Cashier Name : [input]
Customer Name: [input]
Select an item to purchase:
[1] Wintermelon
[2] Orea Cheescake
[3] Matcha Cheescake
[4] Cream Cheese Cocoa
[5] Okinawa Milk Tea
Purchase: [input]
Quantity: [input]
Money: [input]
Select Option:
[p] Print Receipt
[x] Logout
Option: [input]
=================
MILK TEA SHOP
=================
Cashier : [Output]
Customer : [Output]
Purchased:
[Product] ($[Price]) x [Quantity]
Total : [Output]
Cash : [Output]
Change : [Output]