• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

trouble setting object value

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created an object of a Player that holds the value of 500 gold.  That gold can be used to purchase weapons at the blacksmith shop.  Once I purchase a weapon, the weapon is sent to my inventory.  I created a method that allows me to print the amount of gold that the Player object has left over after a purchase.  The problem that I am having is that the amount of gold seems to deplete right after a purchase but then returns back to its original value after I call the amazonInventory() method.  Any advice as to how I can set the Player object's gold value after a purchase.

MAIN:


PLAYER:



WEAPON:











 
Saloon Keeper
Posts: 1313
40
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's good to see you working on this, and there's some good ideas in here!

The short answer is that your Main class has a somewhat inappropriate static variable that you are using here:
  private static int currentGold = amazon1.getGold();

It only gets initialized once, when the class first loads.

The bad news is that your confusion seems to involve basic OOPS principles.

The great news is that your confusion involve basic OOPS principles!

You have getters, setters and a Constructor for your Player and Weapon classes, and they appear to be written correctly, so that part is good.

Before you code, or before you return to your code, it is important to have an idea of where which data is going to reside, and the related question of where methods should live.

It will require a little thinking about things like "Which objects are involved when the Player makes a purchase?  Which data changes when this occurs?  Which class will this method go in?"

Someone will likely be along soon to help you thru asking these questions and finding useful answers (there are no hard and fast rules, only good guidelines).

I predict that when the answer to these "Which class does this method belong to?" questions are clear to you, the code will just fall out.

I think you just started coding some of this a little too soon before you had clear answers to some of those questions yet.
 
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have:
you should do away with this "currentGold" variable and any time you want to know how much gold you have use "amazon1.getGold()".
 
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The problem that I am having is that the amount of gold seems to deplete right after a purchase but then returns back to its original value after I call the amazonInventory() method.



Can you post the program's output that shows what you are talking about?  Be sure to add some comments where the output is not what you expected.
 
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . you should do away with this "currentGold" variable . . .

You should also usually not make such a variable static; that would allow only one gold value to exist at any one time.
I presume, in this case, Amazon means a woman warrior, not a large online retailer nor a large river.
 
Kory Perry
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

The problem that I am having is that the amount of gold seems to deplete right after a purchase but then returns back to its original value after I call the amazonInventory() method.



Can you post the program's output that shows what you are talking about?  Be sure to add some comments where the output is not what you expected.




The output is:
Inventory: Wrath Flight
Gold: 500

I set the gold to 300 just to test the setter() method.  I expected the output to be:
Inventory: Wrath Flight
Gold: 300

My setter() method doesn't remain after I call the amazonInventory() method.  The gold just goes back to 500.
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every time a new Player object is created, the value of currentGold will be set to 500 because currentGold is static and there is only one copy of the variable available for all instances of the Player class.
Remove the static so that each instance of the Player class will have its own copy of currentGold.

What is the purpose of the amazon1 variable?  It is always created when a new Player object is created and then its gold value is copied to currentGold???

Whoops, I thought code was in Player not in Main class

I was remembering the code from this site:  https://www.dreamincode.net/forums/topic/421635-trouble-accessing-object-value/
There seems to be a big change from that code to what is posted here.
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the setter method do? Does it update the inventory? Please note Carey's suggestion that you shouldn't have a gold variable in the first place. I am getting all confused about where the gold value is stored. You appear to be storing it in the Player object, and you appear to be storing it in the inventory. That is a sure‑fire recipe for confusion, having the same value stored in two places.
Don't iterate a List to print out its contents; Lists usually have a toString() method which does that already. Please try simply System.out.println(myList); and see whether that suits your requirements.
 
Marshal
Posts: 16594
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kory Perry wrote:
The output is:
Inventory: Wrath Flight
Gold: 500

I set the gold to 300 just to test the setter() method.  I expected the output to be:
Inventory: Wrath Flight
Gold: 300

My setter() method doesn't remain after I call the amazonInventory() method.  The gold just goes back to 500.


These are the relevant lines that cause the behavior you're seeing:

If you can reconcile that code with where you think you're setting the value of gold to 300, then you'll understand where your problem is.
 
Kory Perry
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey was correct.  Removing the the currentGold variable and just simply calling the getGold() method solved my issue.  I was overthinking this one.  Thanks everyone for the help.
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have code at this site: https://www.dreamincode.net/forums/topic/421635-trouble-accessing-object-value/
could you add a post there saying the problem is solved?
 
Kory Perry
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:If you have code at this site: https://www.dreamincode.net/forums/topic/421635-trouble-accessing-object-value/
could you add a post there saying the problem is solved?



Will do.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic