Assignment 1: Sand Sales

University of Toronto, St. George Campus
Due June 21st, 2001, at the beginning of your tutorial.
Worth 8% of Grade


Overview

This assignment is intended to give you more practice with how objects interact in a Java program. In this assignment you will see an example of a reference to an object used as an instance variable.

The program that you will be completing models how retailers and suppliers manage the sale of sand. The 'Retailer' objects that will be created in your program store the name of the retailer, how much money they currently have, how much sand they currently have in stock, the retail cost of the sand, and each retailer also has a supplier (represented by the 'Supplier' class). When a retailer sells sand to a consumer (using the 'sale' method), they remove the sand from their supply and take the consumer's money in exchange. A retailer can order more sand from its supplier (by using the 'orderStock' method in the Supplier class). These are just some of the actions that your program will model.

In this assignment, you will again be completing the missing parts of some classes that have been provided for you. Please note that you are not allowed to use any types of if statements or loops in this assignment. Also, the only part of the AssignmentOne.java file that you may modify is the section that sets up the files for input and output.

Your main task will be to complete the Retailer and Supplier classes. The instance variables, constructors, and methods that you will need to write in these classes are described in detail in the code that has been given to you. Below you will also find an 'English' description of the classes that are part of this program.


AssignmentOne class

This class has been provided for you. The only modification that you will need to make to this class will be to make it read input from a file called "input1.txt" and to create its output in a file called "output1.txt" (and then later you'll change these to input2.txt and output2.txt). Currently this class reads its input from the keyboard and displays the output on the screen. You'll find it easiest to run the program interactively (input from keyboard and output to screen) when you are trying to get the program working. Once the program is working and you are ready to produce the output that you'll need to submit, you should make the modifications to this class.

This class creates a retailer named "The Sand Man" that has $500 and a sand cost of $20/kg, but has no sand to begin with. It then creates a non-profit supplier called "The Pile" that has a sand cost of $10/kg. This is the supplier for "The Sand Man". The user enters how much money and sand this supplier should have to begin with.

The user enters how much sand stock this retailer should order from its supplier. The appropriate methods are called to allow this restock order to take place. An error is printed if the retailer does not have enough money in order to purchase this sand from its supplier.

The user enters how much sand consumer 1 should purchase from this retailer. The appropriate methods are called to allow this purchase to take place. An error is printed if the retailer does not have enough sand available.

Finally, a method is called to display a summary of the current state of this retailer and supplier.

A second retailer called "Dirt Is Us" is then created. "Dirt Is Us" is initially given $1000, 500 kg sand, and a sand cost of $25/kg. The for-profit supplier for this retailer is called "Sand Inc". This supplier charges $15/kg of sand and a fee of $10 each time a retailer orders stock. The same methods that were called for "The Sand Man" are called for "Dirt Is Us", and then a summary of all information for "Dirt is Us" is displayed.


Retailer class

This class stores information for one sand retailer company. It stores the company's name, how much money it has (as an integer), how much sand it has (as an integer), how much it sells its sand for (as an integer), and a reference to its supplier (data type is the Supplier class).

This class has an overloaded constructor. The first version of the constructor takes a String name, an integer starting amount of money, an integer starting amount of sand, and an integer retail cost of sand. These values are stored in the retailer object when it is first created. The second constructor should be used to create a retailer who has a name, a starting amount of money, and a retail cost, but does not have any sand to initialize (thus the starting amount sand is not passed to this constructor through the parameter list).

This class has a setSupplier() method that assigns an existing supplier object to this retailer. It also has a method called canSell() that determines if this retailer has enough sand in stock in order to sell the amount of sand that is passed to this method as a parameter. This method is often called before calling the sale() method, to make sure that the retailer has as much sand as is required for the sale.

This class also has a restock method that is used by the retailer to order more sand from its supplier. To purchase more sand from the supplier, the orderStock() method is used. This retailer should not order more stock unless this retailer has enough money to do so, so the canRestock() method is provided so that it can be called before the orderStock() method in order to check that this retailer has enough money to place the restock order.

The summary() method returns a string that contains all information about the retailer. It should include the retailer's name, amount of money, amount of sand, retail cost of sand, and all information about their supplier (retrieved using the getInfo() method in the Supplier class). This string must also include the total combined amount of sand that this retailer and its supplier have.

Finally, this class has a private totalSand() method that can be used to return the total amount of sand that this retailer and its supplier have. This method can be called by the summary() method (described above), so that this information is included in the summary string.

You will not need to add any other methods to this class.


Supplier class

This class stores information for one sand supplying company. It stores the company's name and its type (profit or non-profit). It also stores how much money it has (as an integer), how much sand it has (as an integer), how much it sells its sand for (as an integer), and the service fee that it charges on each sand restock order.

This class also has an overloaded constructor. The first version of the constructor takes the supplier's company name, the supplier type (profit or non-profit), the sand cost, and the service charge fee as parameters. The second version of the constructor assumes that this supplier does not charge a service fee, so it only has the first three parameters.

This class has an addSand() method that is used to increase the amount of sand for this supplier, and an addMoney() method that is used to increase the amount of money that this supplier has. Note that we do not model where this sand or money actually comes from; we simply use these methods to increase these amounts for this supplier. The Supplier class also has a getSand() method, which returns the current amount of sand that this supplier has.

This class has an orderStock() method that is used to order the specified amount of sand from this supplier. This method will decrease the supplier's amount of sand and increase the supplier's amount of money. The getSandCost() method returns the cost that the Supplier charges for the specified amount of sand. This method is called from the Retailer's canRestock() method to make sure the retailer has enough money to order this amount of sand stock.

Finally, this class has a getInfo() method that returns a string that contains all information about this supplier. It should return a string containing the supplier's name, type, amount of money, amount of sand, sand cost, and service charge fee.

You will not need to add any other methods to this class.


Submission

For this assignment, you must type in the missing code in the three class files that have been given to you, and then submit a printout of each of these class files.

You must also submit your code on a 3.5" floppy diskette. You must label your diskette with your name and student number.

You must test your program using the input1.txt file that has been given to you. Uncomment the statements in the AssignmentOne class to have your input read from this input1.txt file and the output automatically written to the output1.txt file. Make a printout of output1.txt and write "output1.txt" at the top of it, and submit this printout. (Note that when you run your program in CodeWarrior, the input1.txt file must exist in the same folder that contains your CodeWarrior project.)

You must also create a second test input file on your own. This input file should test something that the first input file does not. Create a duplicate copy of the input1.txt file that has been given to you, and call it input2.txt (you can do this by opening input1.txt in CodeWarrior using File->Open..., and resaving it under a different name File->Save As... and typing input2.txt). Then modify input2.txt to contain your own test input for the program. Now change your AssignmentOne.java file so that it reads its input from input2.txt and writes its output to output2.txt, and then run your program again. Print out both the input2.txt and output2.txt files for your test. Clearly label your input file as "input2.txt" at the top and your output file as "output2.txt" at the top. Write notes on your output file by hand (annotate your output) to explain which aspect of the program you are testing - you will lose marks if you fail to do this.

The pages in your assignment should be stapled together, in the following order: AssignmentOne, Retailer, Supplier, output1.txt for the provided input, your input2.txt, and your output2.txt. Put your printouts and your floppy diskette into an 8.5x11" unsealed envelope, and give it to your TA at the beginning of your tutorial. Attach the cover page to the outside of your envelope.

Late assignments must be left in the CSC108 late drop box (just outside room SF2305A). The penalties stated in the course outline will be applied to late assignments.


Output Format

When you modify your program so that the input comes from a file, and the output is sent directly to a file, this means that the input and output have been separated (instead of seeing the input and output together when you run your program interactively). Don't be alarmed when you look at the output file that has been created and notice that it has has some very long wrapped lines, and that it does not contain the input values that were present when you ran the program interactively. You have separated the input and output, so the carriage return (what occurs when you press the Enter key) that the user usually enters when running the program interactively is read from the input file instead, but is not displayed.

This format of your output file is perfectly fine. When you print your output file, you should make sure that the long lines are not cut off. If the environment that you use for printing cuts off long lines, there are a couple of things that you can do to fix this:

  1. Bring your output file into a wordprocessor, such as MS-Word, and print from here. It will wrap the long lines onto several lines.
  2. Change the printer setup so that it prints in Landscape mode instead of Portrait mode.