You work at asoft drink distributorshipthat sells at most
100
different kinds of soft drinks. The program youwrite for this assignment will process weeklytransactionsand allow fora report to bedisplayed that includes the soft- drink name, ID, starting inventory, final inventory, andthe number of transactions received.
There are two data files, data6.txt and data6trans.txt,which hold the initial soft drink information and transactions, respectively.The file data6.txt consists ofat most100 lineswhere each line contains thesoft drink name (one string), ID (string), and thestarting inventory of cases(int).
The file data6trans.txt holds the transactions. Each transactionconsists of the ID followed by the number of cases purchased (positive integer), or the amount sold (negative integer). You can assume the format of the data is correct, but not all IDs arevalid. In the case of an invalidID, do not process thedata (ignore it, no error message).
Sample data6.txt: Sample data6trans.txt:
|
Coke |
123 |
||||||
|
345 |
10 |
||||||
|
Pepsi |
-5 |
||||||
|
CanadaDry |
678 |
||||||
|
DrPepper |
444 |
120 |
8 |
||||
|
20 |
|||||||
|
-20 |
|||||||
|
999 |
5 |
||||||
|
-25 |
The displayReport functiondisplays the drink name, ID, starting inventory, final inventory, andthe number of transactionsprocessed. Display this exact format (numberof blanks separating items does not have to be identical).
For the sample data,the output of your program would be as follows:
Soft Drink ID Starting Inventory Final Inventory # transaction
Coke 123 100 70 2
Pepsi 345 50 80 3
CanadaDry 678 75 83 1
DrPepper 444 120 130 3
Write a SoftDrinkInventoryclasswith the following functionality:
public constructor: Initializes arrays holding softdrink nameand ID to hold all empty strings (calls intitializeString twice to perform the tasks). Initializesarrays holding starting inventory, final inventory, andthe counts of the number of transaction to zero (calls initializeInt three times to perform the tasks).
public buildInventory: Sets the arrays for soft drink name, ID, andstarting inventory from information in the data file. The array holdingfinal inventory is set tothe same valuesas the starting inventory.
public processTransactions: Processes the transactions bycorrectly adjusting the final inventory and transaction counts arrays. Data for IDswhich don’t exist are not processed.
public displayReport: Displays a report including soft drink name,ID, starting inventory,final inventory, and number of transactions processed.
private findID: Takes an ID parameter andreturns the position inthe array (the subscript) where thesoft drink with that id is found. Return-1 if the ID is not found.Remember theid is a String (so use equals to compare).
private initializeInt: Takes an int array parameter and initializes all array values to zero.
private initializeString: Takes a String arrayparameter and initializesall values to the empty String (“”).
So that you are not overwhelmed with assignment 6, I have put together a skeleton SoftDrinkInventory class. This can be found in the dropbox for the assignment. All the places with “…” need you to write code.
Notice all the good comments that tell you exactly what each method does. This should both help you to write the code and teach you what a good comment looks like. Good comments let you know what the code is doing without you having to read code.
I have also provided a completed SoftDrinkTester class to be used in testing the program. This class, SoftDrinkTester, should not be modified. Be sure to read and understand this class before writing any code for the SoftDrinkInventory class.
Now here is an outline of how to attack the problem. Once you think of a large problem as a bunch of smaller ones, it becomes a much less intimidating task. Divide and conquer!
Comment out from main anything that you are not testing.
STEP 1
When developing this code, first write the constructor that initializes all your class variables. Hopefully you wrote most
of this in lab. You will need to write initializeInt and initializeString to be used in the constructor.
Don’t write more code until this compiles!
STEP 2
Next write displayReport(). You’ll need to review using printf to make nice columns. Compile and run. It should display a report with empty strings and zeros.
Having displayReport() written is handy for debugging purposes. If something goes wrong later on, you can always call it inside a loop and watch how your numbers change.
STEP 3
Now write buildInventory. Compile and run along with displayReport(). Now the display should have the softdrink names, ids, and starting inventory. The final inventory should be the same as the starting inventory and # transactions will be zero.
STEP 4
Now all that is left is processTransactions. Read one transaction at a time and handle it. You will need to write findID to handle one transaction. Finding something in an array (officially called linear search) is a common task. The sample code , FindInArray.java, including as an attachment to the assignment demonstrates how to do this.