Simple Point of Sale Order Info: How to compile all the Items that a user will input?
Problem Description:
I was doing a simple point of sale system, and I’m having a difficulty on outputting all the items that the user will input. So basically it’s like a receipt.
Here’s the whole code:
public class POS {
public static Scanner input = new Scanner(System.in);
public static String again;
public static String choose;
public static String description;
public static String price;
public static int quantity;
public static double total ;
public static void menu() {
System.out.println("tttt +===========================================+");
System.out.println("tttt PCG Computer Shop ");
System.out.println("tttt ");
System.out.println("tttt");
System.out.println("tttt Code Description Unit Price");
System.out.println("tttt");
System.out.println("tttt A001 CPU Php 6,500");
System.out.println("tttt A002 Mother Board Php 4,500");
System.out.println("tttt A003 GPU Php 25,000");
System.out.println("tttt A004 RAM Php 4,000");
System.out.println("tttt A005 Power Supply Php 5,000");
System.out.println("tttt A006 Mouse Php 500");
System.out.println("tttt A007 Monitor Php 3,000");
System.out.println("tttt A008 Keyboard Php 1,500 ");
System.out.println("tttt A009 Headset Php 600");
System.out.println("tttt A010 Microphone Php 350");
System.out.println("tttt +===========================================+");
}
public static void order() {
System.out.print("nSelect Product Code: ");
choose = input.next();
//condition
if(choose.equals("A001")) {
description = "CPU";
price = "Php 6,500";
System.out.print("Enter Quantity: ");
quantity = input.nextInt();
total = total + (quantity * 6500);
System.out.print("Add Item (y/n)? ");
again = input.next();
if(again.equalsIgnoreCase("Y")) {
order();
}
}
else if(choose.equals("A002")) {
description = "Mother Board";
price = "Php 4,500";
System.out.print("Enter Quantity: ");
quantity = input.nextInt();
total = total + (quantity * 4500);
System.out.print("Add Item (y/n)? ");
again = input.next();
if(again.equalsIgnoreCase("Y")) {
order();
}
}
else if(choose.equals("A003")) {
description = "GPU";
price = "Php 25,000";
System.out.print("Enter Quantity: ");
quantity = input.nextInt();
total = total + (quantity * 25000);
System.out.print("Add Item (y/n)? ");
again = input.next();
if(again.equalsIgnoreCase("Y")) {
order();
}
}
else if(choose.equals("A004")) {
description = "RAM";
price = "Php 4,000";
System.out.print("Enter Quantity: ");
quantity = input.nextInt();
total = total + (quantity * 4000);
System.out.print("Add Item (y/n)? ");
again = input.next();
if(again.equalsIgnoreCase("Y")) {
order();
}
}
else if(choose.equals("A005")) {
description = "Power Supply";
price = "Php 5,000";
System.out.print("Enter Quantity: ");
quantity = input.nextInt();
total = total + (quantity * 5000);
System.out.print("Add Item (y/n)? ");
again = input.next();
if(again.equalsIgnoreCase("Y")) {
order();
}
}
else if(choose.equals("A006")) {
description = "Mouse";
price = "Php 500";
System.out.print("Enter Quantity: ");
quantity = input.nextInt();
total = total + (quantity * 500);
System.out.print("Add Item (y/n)? ");
again = input.next();
if(again.equalsIgnoreCase("Y")) {
order();
}
}
else if(choose.equals("A007")) {
description = "Monitor";
price = "Php 3,000";
System.out.print("Enter Quantity: ");
quantity = input.nextInt();
total = total + (quantity * 3000);
System.out.print("Add Item (y/n)? ");
again = input.next();
if(again.equalsIgnoreCase("Y")) {
order();
}
}
else if(choose.equals("A008")) {
System.out.print("Enter Quantity: ");
description = "Keyboard";
price = "Php 1,500";
quantity = input.nextInt();
total = total + (quantity * 1500);
System.out.print("Add Item (y/n)? ");
again = input.next();
if(again.equalsIgnoreCase("Y")) {
order();
}
}
else if(choose.equals("A009")) {
description = "Headset";
price = "Php 600";
System.out.print("Enter Quantity: ");
quantity = input.nextInt();
total = total + (quantity * 600);
System.out.print("Add Item (y/n)? ");
again = input.next();
if(again.equalsIgnoreCase("Y")) {
order();
}
}
else if(choose.equals("A010")) {
description = "Microphone";
price = "Php 350";
System.out.print("Enter Quantity: ");
quantity = input.nextInt();
total = total + (quantity * 350);
System.out.print("Add Item (y/n)? ");
again = input.next();
if(again.equalsIgnoreCase("Y")) {
order();
}
}
}
//This is supposedly the receipt
public static void orderInfo() {
System.out.println("tttt +======================================================================================+");
System.out.println("tttt PCG Computer Shop ");
System.out.println("tttt ");
System.out.println("tttt");
System.out.println("tttt ORDER INFORMATION ");
System.out.println("tttt");
System.out.println("tttt Code Description Unit Price Quantity Amount");
System.out.println("tttt " + choose + " " + description + " " + price + " " + quantity + " " + total);
System.out.println("tttt ");
System.out.println("tttt");
System.out.println("tttt");
System.out.println("tttt");
System.out.println("tttt");
System.out.println("tttt +======================================================================================+");
}
public static void main(String[] args) {
menu();
order();
orderInfo();
}
}
I tried setter and getter but it didn’t work for me. So I gave up.
This is what suppose the output should look like
+===========================================+
PCG Computer Shop
Code Description Unit Price
A001 CPU Php 6,500
A002 Mother Board Php 4,500
A003 GPU Php 25,000
A004 RAM Php 4,000
A005 Power Supply Php 5,000
A006 Mouse Php 500
A007 Monitor Php 3,000
A008 Keyboard Php 1,500
A009 Headset Php 600
A010 Microphone Php 350
+===========================================+
Select Product Code: A001
Enter Quantity: 2
Add Item (y/n)? y
Select Product Code: A002
Enter Quantity: 2
Add Item (y/n)? y
Select Product Code: A004
Enter Quantity: 2
Add Item (y/n)? n
+======================================================================================+
PCG Computer Shop
ORDER INFORMATION
Code Description Unit Price Quantity Amount
A001 CPU Php 6,500 2 13000.0
A002 Mother Board Php 4,500 2 9000.0
A004 RAM Php 4,000 2 4000.0
+======================================================================================+
And this is the output that I got:
+======================================================================================+
PCG Computer Shop
ORDER INFORMATION
Code Description Unit Price Quantity Amount
A004 RAM Php 4,000 2 30000.0
+======================================================================================+
Solution – 1
Your order
method only allows for the input of exactly one item and you don’t have any loop to call it multiple times. Also you do not collect multiple ordered items in an array or list, so you cannot first collect all ordered items and then print them in a summary. Also your output in orderInfo
does not have a loop and can only output the last ordered item.
What you do right now is a recursive call or the order
method to itself – this will only work for a certain amount of items, then your call stack will be exhausted. (Probably you will never face that error when you try it, since the stack is not small, and you do not use a lot of it in your program.)
You should create a class (e.g. OrderedItem
) for an ordered item that stores data like product code, description, price and quantity. Then in a loop let the user enter the ordered items and create a new object of that class per ordered item. Add that object to a list.
In your orderInfo
method you pass the list of ordered items as parameter and then print the header first, then a loop to print all the individual items from the list, and then you print the footer.