PART ELEVEN
SHOP
EXPANDING ITEMS
Expanding the Item Class
Add two fields to class item:
String name
int cost
In Item, write appropriate accessors for name and cost.
In the constructor for PotatoSeed, CornSeed, Bucket, and Hoe, assign values to name and cost.
Displaying Items
In class Shop
Declare and initialize an ArrayList of items
Add some sample items to the list
In render, loop through all of the items using a normal for loop. You'll want to display three pieces of information in your label:
The index number + 1 (used for hotkey later)
The item name
The item's cost
It's okay if this looks pretty basic for now. We'll play with fonts, buttons, and icons later on.
DISPLAY MONEY
Displaying Money and Days
In class Shop
Add methods to display the user's money and the current day
Ideally, keep your placement consistent with how you display it in game.
Doing this may require writing a new accessor in Game.
Organizing Render()
In class Shop, in the render() method
Call the methods to display money and current day
Put all of the code that draws the items for sale in a separate method, then call it, so it isn't all directly inside of render.
ITEMBAR
Adding Items to ItemBar
Create a constant integer called MAX_ITEMS and set it equal to a value of your choice.
Write an accessor called hasSpace() that returns true if items has fewer than MAX_ITEMS, false otherwise.
In your ItemBar class, write a method called addItem(Item i)
If there is space in items, add the item to the list.
Making ItemBar Static
In class Game, we create a private ItemBar named items.
The catch is that we'll need to access it inside of Shop.
Since there is only one ItemBar, we can use the Singleton Pattern to make the reference to it in Game static. Make sure to create an accessor to reference it.
Rendering ItemBar in Shop
We still want to show the player's items in the shop menu
Add code to the render method to draw the ItemBar.
CHECKPOINT
Run your code and see...
Your program should show items for sale
Your program should show your ItemBar, even in the shop
BUYING ITEMS
In this next section, we'll be using a very advanced method to make objects dynamically. It's okay if you don't fully understand this and lean on the example code. None of this will be on the AP Test. But it's a powerful tool you may find to be very useful in future projects.
Item Factory
In class Shop, let's make a method called buildItem()
This is using a more advanced concept called Reflection.
We'll make a special kind of method called a Factory. This is a method which takes a class as a parameter, and creates an object of that type dynamically.
Use the code on the right to create this method in your program.
Buying Items
In class Shop, let's make a method to buy items called buy(Item i)
Spend the player's money based on the item's cost
Add the item to the itembar
Detecting a Number Press
In class Shop, if the user presses a key between KEY_1 and KEY_9...
Create a variable called index equal to the key's "matching" position in items.
Hint: KEY_1 is a constant storing the value 2, and KEY_9 is a constant storing the value 10. You can convert this range to an index position with simple math....
If index is within the bounds of your ArrayList AND the player has enough money AND has space in the item bar
Create a copy of the original item by calling buildItem
Call buyItem() with the copied item as the parameter.
CHECKPOINT
Run your code and see...
You should be able to purchase items now
Do a quick test run to make sure items can be used and everything works without bugs!
EARNINGS
Earnings
Create a variable to keep track of the player's lifetime earnings from harvesting crops.
Hint: You can use your gainMoney() method to make this process easy to update automatically.
Present this to the user in terms of score on all game states.
CHECKPOINT
Run your code and see...
Your program should display some sort of "score" or "earnings" in addition to the current money. This value never decreases.