We just started to learn decisions, and I'm confused.
Wonder if someone could help me figure this out.

In this assignment, you are going to calculate how much a BahnCard costs to buy, using the following rules.

* BahnCard25. The card costs 110 euros for a first class card and 55 euros for a second class card. However, if the rider is younger than 19, a second class card can be purchased for 10 euros.
* BahnCard50. The card costs 440 euros for a first class card and 220 euros for a second class card. However, both of these cards can be purchased at half price (220 for first class, 110 for second class) if any one of the following three conditions applies:
o The person is seventeen years old or younger.
o The person is sixty years old or older.
o The person is a student who is younger than 27.
* BahnCard100. The card costs 5900 euros for a first class card and 3500 euros for a second class card.


Now I just would like a push in one direction, so I'll just ask for help on the BahnCard100

public int bahnCard100Price (int desiredClass)
    {
       double cost;
        
        if (desiredClass = 1) 
        cost = 5900;
            else
            
        if (desiredClass = 2)
        cost  = 3500;
        return cost;
    }

You didn't post a question...

Member Avatar for scorio
public int bahnCard100Price (int desiredClass)
    {
       double cost;
        
        if (desiredClass = 1) 
        cost = 5900;
            else
            
        if (desiredClass = 2)
        cost  = 3500;
        return cost;
    }

Do you mean:

public int bahnCard100Price (int desiredClass)
{
  if(desiredClass==1)
    cost = 5900;
  else // we can presume at this point it is second class card but you could also use "else if(desiredClass==2)"
    cost = 3500;
  
  return cost;
}

Or did I misunderstand? Your code is otherwise ok, except you used single equal sign instead of double.

"otherwise ok" except he declared the method to return an int, but used a double for cost (and your own code does not define the variable cost at all you might notice).

Also, you should always include braces around the statement block with if(),for(),while(), etc. The spec does allow a single statement without braces, but you're just begging for a bug to arise later when you decide to add another statement to the implied block. It's worth the time to put in the braces.

Member Avatar for scorio

True every word, Ezzaral. Maybe it is time for a nap after that kind of nasty mistakes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.