CS 151 - Introduction to Data Structures

Assignment 6 -- Grocery Store Profit and Loss

Usual Style comments

As usual, read the program design principles and code formatting standards carefully. You are expected to adhere to all stated standards.

Overview

There are three parts to this homework. In the first part you will complete a partial implementation of an array-based stack. In the second and third parts you will use that stack to compute profit or loss in a grocery store.

Data a program files

There are three .java files and 3 data files associated with this assignment. The .java files are: Stack151.java and ArrayStack.java are identical to the files you worked with in lab 6. So you can copy those files from you lab6 folder. Or you can get them anew as below. ReadCSV.java is the, now time-honored, file you have been using all semester.

The data files are: All of the above files are available from the class web site:
		
  • https://cs.brynmawr.edu/cs151/Data/Hw6/Stack151.java
  • etc
    or via scp, for example
    		scp YOURUNIXLOGIN@goldengate.cs.brynmawr.edu:/home/gtowell/Public/151/Data/Hw6/Stack151.java Stack151.java
    	

    Profit and loss in a grocery store

    A very simple model of the the profit or loss at a grocery store is that when the store gets in new product, the store pays the supplier what the supplier demands for the amount supplied. When the store sells to a customer, the customer pays what the grocery store demands for the amount purchased. As a grocery store owner, it would be quite simple to compute profit and loss if you got a complete supply of a product at one time, and then sold all of that supply at another time (for a certain price). But that is not what happens. For example, consider purchases and sales of cans of chicken noodle soup. Further suppose that, as a modern store owner, you take advantage of UPC labels and are able to change the price every day. Similarly, your supplier changes their price with every delivery. Hence, you end up with data that looks like the following:
    	day 1: buy 1000 cans of chicken noodle soup for $0.95 per can
    	day 2: sell 50 cans for for $1.00 per can
    	day 3: sell 20 cans for for $0.95 per can
    	day 4: sell 100 cans for for $0.90 per can
    	day 5: buy 100 cans of chicken noodle soup for $1.05 per can
    	day 6: sell 80 cans for for $1.00 per can
    	day 7: sell 40 cans for for $1.05 per can
    
    So, now the question. Has the store made money on selling cans of soup (ignoring the cost of the unsold soup that is still on the shelf). The answer depends on how you do your accounting. In this homework we will be using Last In, First Out accounting; and doing so using a stack. Specifically, on every purchase, push that purchase onto a stack. On every sale, peek on the stack. Sell as much from the top of the stack as is required (and adjust the amount on the top of the stack). If you are selling more than is on the top of the stack, sell everything from the top of the stack, pop the top item, then sell from the newly top item (repeat as needed to complete the entire sale). On every sale, track your profit and loss on the sale of that product. For example, given the data above:
    		buy 1000 at 0.95 
    			Stack is now 1000@0.95
    		sell 50@1.00 
    			PL on this sale is 50*1.00 - 50*0.95 = 2.50.
    			Overall PL on product 2.50
    			stack is now 950@0.95
    		sell 20@0.95
    			PL on this sale is 20*0.95 - 20*0.95 = 0
    			Overall PL on product is 2.50
    			stack is now 930@0.95
    		sell 100@0.90
    			PL on this sale is 100*0.90-100*0.95 = -5.00
    			Overall PL on product is -2.50
    			stack is now 830@0.95
    		buy 100@1.05
    			stack is now 830@0.95, 100@1.05
    		day 6: sell 80 cans for for $1.00 per can
    			PL on this same is 80*1.00 - 80*1.05 = -4.00
    			Overall PL on product is -6.50
    			stack is now 830@0.95, 20@1.05
    		day 7: sell 40 cans for for $1.05 per can
    			PL on this sale is 40*1.05 - 20*1.05 - 20*0.95 = 2.00
    			the first 20 were from the top of the stack at 1.05 which used up that group. The remainder were from the next item on the stack
    			Overall PL on this product is -4.50
    			stack is now 810@0.95
    	
    You will note that, in the data above, there was a very large purchase on day 1. This is so the grocery store never runs out of stock to sell. (Empty shelves are bad. You want customers to always come in believing that they can get what they want.) Also, this makes the program a little easier to write.

    Data Files

    There are 3 data files associated with this assignment. All are in CSV format so you can use CSVReader. The data files all have the following columns:
    1. day -- an positive integer. This number always ascends
    2. product -- a string. The name of the product. This will always be a single word, for example, corn
    3. p or s. Literally the letter "p" or the letter "s". p = purchase, s=sale
    4. amount -- a positive integer, the size of of the purchase or sale.
    5. price -- a positive floating point number, the price at which the purchase or sale was made.
    So the data file for the example above is
    	1,soup,p,1000,0.95
    	2,soup,s,50,1.00
    	3,soup,s,20,0.95
    	4,soup,s,100,0.90 
    	5,soup,p,100,1.05
    	6,soup,s,80,1.00
    	7,soup,s,40,1.05
    
    This exactly data is contained in small.csv. If a purchase and a sale of the same product occur on the same day, assume that they occurred in the order given in the data file.

    Part 1

    Complete the unfinished parts of the ArrayStack.

    Part 2

    Write a program that works for a single product. I recommend starting with small.csv (the data in the sample above) making your program work on that. Once you have that working, go on to the larger single product data file.

    Part 3

    A grocery store has a hundreds of products. Redesign your system so that you can track the profit/loss on each product independently. Also track the total profit/loss for the store. The goal here is to be able to say something like "I should stop selling dog food because I loose money on it" or "I should sell more products like cookies because I make lots of money on those."

    The program does not know that list of products being sold until it reads the data file.

    Your system should be able to update the Profit/Loss on each product and on the entire store in O(1) time, where "N" is either the number of products or the number of items on the stack. In other words each purchase of sale should take O(1) time to update all of the information you are tracking.

    Suggestion: you will definitely need to have a separate stack for each product.

    Slightly easier problem (for a 5% deduction): Start by assuming you are given a complete list of products in the store. For the file many.csv that list is: soup, corn, pasta, chips, soda. You may read through the data file once each product.

    Output files

    Include recordings of the output of your program on each of the provided data files. The only required output is the profit/loss on each product and the overall profit/loss. However, you will almost certainly find it easier to debug your program if your output is something like the example shown above. Moreover, output of that sort will make it easier to grade your program.

    Other Requirements

    In addition to satisfying the conditions above, your program must:

    What to Turn In

    1. All code your wrote or edited, well commented
    2. All code your program used, even if you did not edit it (e.g., ReadCSV.java)
    3. README with the usual information.
    4. A script file showing the output of your program both parts and all three data files). Your output can be separate files, or a single file, or included in the README.

    Electronic Submissions

    Your program will be graded based on how it runs on the department’s Linux server, not how it runs on your computer. The submission should include the following items:

    The following steps for submission assume that you created a project named AssignmentN in the directory /home/YOU/cs151/

    1. For this assignment N=6
    2. Put the README file into the project directory
    3. Go to the directory /home/YOU/cs151
    4. Enter /home/gtowell/bin/submit -c 151 -p N -d AssignmentN

    For more on using the submit script click here

    Approximate Credit Distribution