Question

In: Computer Science

Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale...

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

Solutions

Expert Solution

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


Related Solutions

You need to create a Java class library to support a program to simulate a Point-...
You need to create a Java class library to support a program to simulate a Point- of-Sale (POS) system. General Requirements: You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; You should create identifiers with sensible names; You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. Logical structures...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create the code so that when the program is run the user will see in the console the following: We are going to play rock paper scissors. Please choose 1 for Rock 2 for Paper or 3 for scissors. The program will have your choice which is the integer-valued typed in by the user and compChoice which will be the randomly generated value of either...
In this assignment, you will create a Java program to search recursively for a file in...
In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively in the given...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, and write them in reverse order to an output file. 1. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. For example, assume your package name is FuAssign5 and your main class name is FuAssignment5, and your executable files are in “C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin”. The...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to search recursively for...
I. General Description In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT