In: Computer Science
Program Requirements:
This assignment requires you to create one program in
Java to simulate a Point-of-Sale (POS) system. The
solution must be in JAVA language.
You create the main program called POSmain.java and two classes:
ShoppingCart and CashRegister.
Your POSmain program should take three file names from command line
arguments. The first file contains a list of products and their
prices; the second and third files are lists of items in two
shopping carts of two customers. The POSmain program should first
read the price file, then read each of the cart files to load a
list of items in a shopping cart and store them in a ShoppingCart
objects. The price file may contain a variable number of products
and the cart files may contain a variable number of items.
POSmain then will create a CashRegister object by passing the price
list to it. The POSmain program then will use the CashRegister
object to scan items in a cart and print a receipt for each
shopping cart one by one. At last, POSmain will use the
CashRegister object to print a report for the day.
The students will the report for the day, which requires design of
your CashRegister class.
cart1.txt:
TV
Table
Table
Bed
Bed
Chair
Chair
Chair
Chair
cart2.txt:
Milk
Butter
Tomato
Tomato
Tomato
Tomato
Tomato
Onion
Onion
Onion
Lettuce
Milk
Ham
Bread
Bread
price.txt:
TV
999.99
Table 199
Bed 499.99
Chair 45.49
Milk 3.00
Butter 2.84
Tomato 0.76
Onion 0.54
Lettuce 1.00
Ham 2.50
Bread 1.75
Program Output:
One customer is checking out ...
========================================
Product Price Qty Subtotal
----------------------------------------
Bed $499.99 2 $999.98
Char $45.49 4 $181.96
TV $999.99 1 $999.99
Table $199.0 2 $398.0
-------------------------
Total $2579.93
========================================
One customer is checking out ...
========================================
Product Price Qty Subtotal
----------------------------------------
Bread $1.75 2 $3.5
Butter $2.84 1 $2.84
Ham $2.5 1 $2.5
Lettuce $1.0 1 $1.0
Milk $3.0 2 $6.0
Onions $0.54 3 $1.62
Tomato $0.76 5 $3.8
-------------------------
Total $21.26
========================================
Report for the day
========================================
Number of customers: 2
Total sale: $2601.19
List of products sold:
----------------------------------------
Product Qty
----------------------------------------
Bed 2
Bread 2
Butter 1
Char 4
Ham 1
Lettuce 1
Milk 2
Onions 3
TV 1
Table 2
Tomato 5
NOTE:-
-> Your txt Files has two many spaces after
prices and Item Names, So I removed before using, You should do
same or Use txt Files I am providing in End
-> I am putting Files in C:// to avoid path
errors, You Should Do Same or Change path POSmain.java line 31, 40,
47
Path pricePath = Paths.get("C:\\" + priceFile + ".txt");
-> I am taking only file name as input, don't enter .txt
RESULT:
CODE: (Also Including text code in end)
POSmain.java
ShoppingCart.java
CashRegister.java
CODE:
POSmain.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Scanner;
import java.util.stream.Stream;
public class POSmain {
// Variables
private static Scanner scanner;
private static ShoppingCart cart1;
private static ShoppingCart cart2;
private static HashMap<String, Double>
priceMap;
public static void main(String[] args) throws IOException {
// Initializing
Variables
scanner = new
Scanner(System.in);
ShoppingCart cart1 = new
ShoppingCart();
ShoppingCart cart2 = new
ShoppingCart();
priceMap = new
HashMap<>();
//Reading Files
print("***POS
Program***");
while (true) {
try {
print("Input Price File Name:");
String priceFile = input();
Path pricePath = Paths.get("C:\\" + priceFile + ".txt");
//Reading File
Stream<String> lines = Files.lines(pricePath);
//Saving Price Data
lines.forEach(s -> savePrice(s));
//Saving Cart 1 Items
print("Input 1st Customer's Cart File Name:");
String cart1File = input();
Path cart1Path = Paths.get("C:\\" + cart1File + ".txt");
Stream<String> lines2 = Files.lines(cart1Path);
lines2.forEach(s -> cart1.addItem(s));
//Saving Cart 2 Items
print("Input 2nd Customer's Cart File Name:");
String cart2File = input();
Path cart2Path = Paths.get("C:\\" + cart2File + ".txt");
Stream<String> lines3 = Files.lines(cart2Path);
lines3.forEach(s -> cart2.addItem(s));
break;
} catch (Exception e) {
print("Error: File Not Found" + e.getMessage());
}
}
// Sell Items
CashRegister
cashRegister = new CashRegister(priceMap);
cashRegister.sellItems(cart1);
cashRegister.sellItems(cart2);
cashRegister.printReportOfDay();
}
private static void savePrice(String data)
{
String[] splited =
data.split("\\s+");
priceMap.put(splited[0],
Double.parseDouble(splited[1]));
}
private static String input() {
return
scanner.nextLine();
}
private static void print(String s) {
System.out.println(s);
}
}
ShoppingCart.java
import java.util.ArrayList;
public class ShoppingCart {
private ArrayList<String> items;
public ShoppingCart() {
items = new
ArrayList<>();
}
public void addItem(String item) {
items.add(item);
}
public ArrayList<String> getItems()
{
return items;
}
}
CashRegister.java
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class CashRegister {
private HashMap<String, Double>
priceMap;
private int customerCount;
private double totalSale;
private HashMap<String, Integer>
productsSold;
// For Priniting Line
public String line = new String(new
char[48]).replace('\0', '-');
public String doubleLine = new String(new
char[48]).replace('\0', '=');
private DecimalFormat df = new
DecimalFormat("#.##");
public CashRegister(HashMap<String,
Double> prices) {
priceMap = prices;
customerCount = 0;
totalSale = 0;
productsSold = new
HashMap<>();
}
public void sellItems(ShoppingCart cart)
{
print("One Customer is
Checking Out...");
print(doubleLine);
print("Product Price Qty
SubTotal");
print(line);
double total =
0;
ArrayList<String>
purchaseItems = cart.getItems();
ArrayList<String>
uniqueItems = new ArrayList<>();
// Finding Unique
Items In Card
Set<String> set =
new HashSet<>(purchaseItems);
uniqueItems.addAll(set);
for (String item :
uniqueItems) {
double price = priceMap.get(item);
int qty = Collections.frequency(purchaseItems, item);
double subTotal = price * qty;
print(item + " $" + price + " " + qty + " $" + subTotal);
total += subTotal;
//Update Register
totalSale += subTotal;
if (productsSold.containsKey(item)) {
productsSold.put(item, productsSold.get(item) + qty);
} else {
productsSold.put(item, qty);
}
}
print(line);
print("Total $" +
df.format(total));
print(doubleLine);
customerCount++;
}
public void printReportOfDay() {
print(doubleLine);
print("Report for the
day");
print(doubleLine);
print("Number of
Customers: " + customerCount);
print("Total Sale: $" +
df.format(totalSale));
print("List of products
sold:");
print(line);
print("Product
Qty");
print(line);
for (String key :
productsSold.keySet()) {
print(key + " " + productsSold.get(key));
}
}
private void print(String s) {
System.out.println(s);
}
}
price.txt
TV 999.99
Table 199
Bed 499.99
Chair 45.49
Milk 3.00
Butter 2.84
Tomato 0.76
Onion 0.54
Lettuce 1.00
Ham 2.50
Bread 1.75
cart1.txt
TV
Table
Table
Bed
Bed
Chair
Chair
Chair
Chair
cart2.txt
Milk
Butter
Tomato
Tomato
Tomato
Tomato
Tomato
Onion
Onion
Onion
Lettuce
Milk
Ham
Bread
Bread