PurposeIn this assignment, we will gain experience with one-dimensional arrays. To date, we have been using the Vector class in situations in which an array is appropriate. Specifically, you will now design the Inventory and Market classes for the trading game. The Market class represents a market in a location -- where you buy and sell goods. The Inventory represents an inventory of a vehicle or a market. When you are in a Market, there are 5 commands you can enter:
Note that some of the details about this assignment we are purposely leaving out -- they can be found in the Java documentation. The Java documentation for all the classes and methods in this "Trader" project can be found here. Lots 'o filesTo get started, create a new directory for HW J7, and download the following files. Right click on each link, select "save as", and save them all to the same directory. A number of the files are "placeholder" classes -- meaning they are mostly not yet implemented, but are included so this homework will compile. And some of them are .class files -- these are files that you will be developing in other assignments. Skeleton codeThese are the files that you will need to submit.
Code from previous assignmentsYou are welcome to use your code (from HWs J5 and J6) instead of these. If you are unsure about the correctness of the code you previously developed, then you should use our versions. Note that it is fine for you to modify your Vehicle.java, Location.java, and Parser.java for whatever reason for this assignment, but none will be turned in for this HW J7. If you do modify your classes, be sure not to change the method names (or parameter types).
Other code
Part I: Cargo classThe Cargo class is a fairly simple class -- it has three fields, one constructor, the normal accessors and mutators, and two methods: getTotalPrice() and toString() method. The default constructor creates a random Cargo -- the cargo type, amount, and cost are all randomly chosen. (Note that we set up a random number generator in the Game class, and we invoke operations on it to get our random values.) In an effort to reduce the amount of work for this assignment, we are giving you the code for the Cargo.java. However, it is important that you understand how it works! Descriptions of all the methods can be found here. You will notice in this file that the comments in Cargo.java are in a rather peculiar format. These comments are used to generate the Java documentation for the Cargo class -- in fact, in all of our solution files for the project, we have these comments for every method and field, which is how we generate the Java documentation. You are NOT required to put your comments in this form -- we just mention it in case you wondered (or were confused by) why we wrote comments like that. To be absolutely clear, your only requirement for Part I of this assignment is to understand the Cargo.java code that we provide.
Part II: Inventory classIn Part II of HW J7, you will create the ability to carry any number of Cargos (e.g., bananas, diamonds, gold, etc.) This "inventory" will be used to both represent what is being held on a vehicle as well as represent what is being sold in a particular Location in the game. This inventory is in an array of Cargo objects. The various methods are used to manipulate that array of Cargo objects. Note that you can compile your code for the Inventory class as you develop it, but you will not be able to execute it (by executing "Game") until after you implement the Market class. We also recommend "pretty printing" numbers to screen by using the "Game.style.format" routine (see Cargo.java for an example of how this is used). Attributes
Constructors
Accessors/Mutators Of course, the instance variable in this class should be declared private and manipulated via accessor and mutator methods as we have discussed during lecture. Thus, you must include an accessor and mutator method for the above field. The name of the accessor/mutator should follow the standard Java naming conventions (i.e. getPosX(), setName(), etc.). Note that your mutators should check for and (thus prevent) nonsense values (such as null for a setCargo). Exiting the program in this case is reasonable. Other methodsThe Inventory class should also support the following methods:
Part III: Market classNow that once you have the Inventory Class, you can build the Market Class that each Location will use. Attributes
Constructor
Accessors/Mutators As with all of our Java classes, these instance variables in this class should be declared private and manipulated via accessor and mutator methods as we have discussed during lecture. Thus, you must include an accessor and mutator method for each of the above fields in both classes. The name of the accessor/mutator should follow the standard Java naming conventions (i.e. getPosX(), setName(), etc.). Note that your mutators should check for and (thus prevent) nonsense values (such as negative values -- or zero -- for the cost factor). Exiting the program in this case is reasonable. Other methodsThe Market class should also support the following methods:
Game SimulationHere is a sample output of your game at this point (note: We have not shown the ability to move, but your program should be able to do this). The input that the user entered is shown in red. Welcome to CS101 Space Trader v3! Your Intergallactic Thunderbird is starting at Cygni (6.0,7.0). You burn fuel at the rate of 5.0 gazonkas per million kilometers. You are at a Cygni You have $1,000 on hand Valid commands are: travel (to travel to another location) market (to enter the market) worldmap (to print out the map) inventory (to show your inventory) quit (to leave the game) You can abbreviate any command by its first letter Enter next command: m Welcome to the market at Cygni (6.0,7.0). The cost factor here is 0.89 You are at the market. You have $1,000 on hand. Valid commands are: buy (to buy cargo) sell (to sell cargo) available (to see what's available to buy) inventory (to show your inventory) exit (to exit the market and return to the location) Enter next command: a 0: 6 units of Food at $153.335 per unit (total price in market: $920.01) 1: 1 unit of Aluminum at $14.527 per unit (total price in market: $14.527) 2: 8 units of Cloth at $21.598 per unit (total price in market: $172.787) 3: 2 units of Robots at $330.563 per unit (total price in market: $661.126) 4: 3 units of Platinum at $756.097 per unit (total price in market: $2,268.29) You are at the market. You have $1,000 on hand. Valid commands are: buy (to buy cargo) sell (to sell cargo) available (to see what's available to buy) inventory (to show your inventory) exit (to exit the market and return to the location) Enter next command: b Possible items to trade: 0: 6 units of Food at $153.335 per unit (total price in market: $920.01) 1: 1 unit of Aluminum at $14.527 per unit (total price in market: $14.527) 2: 8 units of Cloth at $21.598 per unit (total price in market: $172.787) 3: 2 units of Robots at $330.563 per unit (total price in market: $661.126) 4: 3 units of Platinum at $756.097 per unit (total price in market: $2,268.29) Which one do you want to trade? 2 It will cost $172.787 for this purchase. You are at the market. You have $827.213 on hand. Valid commands are: buy (to buy cargo) sell (to sell cargo) available (to see what's available to buy) inventory (to show your inventory) exit (to exit the market and return to the location) Enter next command: e You are at a Cygni You have $827.21 on hand Valid commands are: travel (to travel to another location) market (to enter the market) worldmap (to print out the map) inventory (to show your inventory) quit (to leave the game) You can abbreviate any command by its first letter Enter next command: q You ended the game with $827.21 Goodbye! Congratulations, Space Trader! Your Intergallactic Thunderbird traveled 0.0 million kilometers. You still have 500.0 gazonkas of fuel available. Take the rest of the day off, Space Trader -- you deserve it! Testing and SubmissionThe Game.java file includes most of what you will need to test your classes. It is important to confirm that the output is what you expect it to be. The good programming practices from HW J1 need to be in this homework as well, with the exception of the test code -- you do not need to show that you tested this program. When you are finished, submit the Inventory.java and Market.java files. |