Kevin Francis
Kevin Francis

Reputation: 5

How do you call a method from another class?

I'm trying to write a code for a method which has certain conditions which needs to be met. I believe that I need to use methods from a different class to meet the conditions. I've done the last 2 conditions but I have got no clue on how to go about the others because I do need to access methods from a different class.

Upvotes: 0

Views: 100

Answers (2)

Craig Otis
Craig Otis

Reputation: 32054

It seems like using the shtlCode, you can obtain the proper Shuttle instance from your shuttleMap, like so:

public boolean canTravel(int pCardId, String shtlCode)
{    
    Shuttle shuttle = shuttleMap.get(shtlCode);
    ...

Once you have the Shuttle, you can then find the Asteroid it's currently on:

Asteroid currentShuttleAsteroid = shuttle.getSourceAsteroid();

Having these two objects, it's up to you to ensure the conditions have been properly met. (And also, to ensure that your shuttleMap contains a Shuttle with the code specified, etc.)

Upvotes: 2

Bampfer
Bampfer

Reputation: 2220

As Craig suggested above, keep the Shuttle that you fetched from the hashmap. You will need it to implement most of the remaining checks.

canTravel is given a card id, but is going to need the PearlCard itself. But where to get it from? Three possibilities:

  • The caller can pass a PearlCard into canTravel instead of the integer ID, if it has it.
  • canTravel could search for a PearlCard with the matching ID in the source asteroid's list of PearlCards. (And if it's not in there, then you can't travel anyway.)
  • Or you may want to add a HashList of all PearlCards to your program, similar to shuttleMap.

Then get the shuttle's destination asteroid and see if has room for one more PearlCard (compare PearlCard list's length to the asteroid's capacity). Also check to see if the card has enough credit and rating for that asteroid. (You didn't show PearlCard class so I don't know the exact code, but I'm guessing you'll have no trouble with that part.)

Note: your current code seems to have at least one bug. canTravel searches the asteroid list for the card ID. Like I said above you will need to get the card from somewhere, but it's not going to be in asteroidList.

Upvotes: 0

Related Questions