1

Thanks for viewing my question! :)

I'm a noob learning Ruby for a Rails class (at SMC), and I'm stuck on one of the practice assignments having to do with loops and functions. The problem seems to be that I can't figure out how to return the correct value to the main program from a variable I'm using in my functions.

Here is the objective:

Create a Ruby program which calculates student fees for those attending Santa Monica College. IN ORDER TO RECEIVE FULL CREDIT, YOU MUST CREATE FUNCTIONS TO SOLVE THIS PROBLEM WITH PARAMETER. IN ADDITION IN ORDER TO RECEIVE FULL CREDIT, YOU MUST THROW AND CATCH EXCEPTIONS WHEN THE USER ENTERS INVALID DATA VALUES.

Summarized in the chart below is the cost calculations I want your program to perform.

SANTA MONICA COLLEGE STUDENT FEES (as of Fall, 2014)

Enrollment Fee

$ 46.00/ unit for California Residents $ 325.00/ unit for F1/Non-Residents

Student Services Fee (AS Sticker fee is Optional, saving $19.50) (ID Card fee is Optional, saving $13)

$ 47.50 Winter/Summer $ 50.50 Fall/Spring

Parking Decal (Optional)

$ 45.00 Winter/Summer $ 85.00 Fall/Spring

This program dialogue describes the program I am looking for.

SMC Fee Calculator
Enter number of units enrolled: 18
Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: 0
Are you a state resident[0] or not[1]: 0
Want a parking decal? [y/n]: n
Want an AS sticker? [y/n]: n
Want an ID card? [y/n]: n
For Fall semester, your total fees are $ 846.00 

And here is my program that I'm having trouble with (I've commented out some bits of code that I'm unsure of):

# program that calculates student fees for those attending Santa Monica college
# I just can't seem to figure out how to calculate and print out the 'fees' variable correctly
require_relative 'unit5test'

puts "SMC Fee Calculator"

begin

    print "Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: "
    semester = gets.chomp.to_i

    print "Enter number of units enrolled: "
    units = gets.chomp.to_i

    print "Are you a state resident[0] or not[1]: "
    resident = gets.chomp.to_i

    print "Want a parking decal? [y/n]: "
    decal = gets.chomp

    print "Want an AS sticker? [y/n]: "
    sticker = gets.chomp

    print "Want an ID card? [y/n]: "
    card = gets.chomp

    fees = 0
    unit_fee( units, resident, fees )
    decal_fee( decal, semester, fees )
    total_fees = services_fee( sticker, card, semester, fees )
    # fees = '%.2f' % fees
    puts "For ", session_type( semester ), " semester, your total fees are $ ", total_fees # don't know what to put here 

# rescue

    # puts "Sorry, you've entered an incorrect value!"

end

These are my functions that are in the separate file named 'unit5test.rb':

# contains functions for the SMC Fee Calculator

def session_type ( semester )
    if (semester == 0)
        semester = "Fall"
    elsif (semester == 1)
        semester = "Winter"
    elsif (semester == 2)
        semester = "Spring"
    elsif (semester == 3)
        semester = "Summer"
    end

    return semester
end

def unit_fee ( units, resident, fees )

    # if (semester == 0 or semester == 2)
        if (units < 0 or units > 16)
            raise "inappropriate number of units entered for semester"
        end
    # elsif (semester == 1 or semester == 3)
    #   if (units < 0 or units > 8)
    #       raise "inappropriate number of units entered for session"
    #   end
    # end

    if (resident == 0)
        fees += (units * 46.00)
    elsif (resident == 1)
        fees += (units * 325.00)
    end

    return fees
end

def decal_fee ( decal, semester, fees )

    if (semester == 0 or semester == 2)
        if (decal == 'y')
            fees += 85.00
        end
    elsif (semester == 1 or semester == 3)
        if (decal == 'y')
            fees += 45.00
        end
    end

    return fees
end

def services_fee ( semester, sticker, card, fees )

    if (semester == 0 or semester == 2)
        fees += 50.50
        if (sticker == 'n')
            fees -= 19.50
        end
        if (card == 'n')
            fees -= 13.00
        end
    elsif (semester == 1 or semester == 3)
        fees += 47.50
        if (sticker == 'n')
            fees -= 19.50
        end
        if (card == 'n')
            fees -= 13.00
        end
    end

    return fees
end

And the incorrect output I'm getting:

SMC Fee Calculator
Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: 0
Enter number of units enrolled: 12
Are you a state resident[0] or not[1]: 0
Want a parking decal? [y/n]: n
Want an AS sticker? [y/n]: n
Want an ID card? [y/n]: n
For
Fall
 semester, your total fees are $
0

Thanks in advance for helping me out! Despite the difficulties, I love learning Ruby! :)

6
  • You aren't saving the value returned by your unit_fee method. it should be something like fees += unit_fee(...). Passing fees into the function won't return the variable, only the value. Commented Oct 9, 2014 at 1:41
  • Oh okay, so should I initialize the variable fees before I call the functions with fees = 0, and then I call the functions with fees += unit_fee(units, fees) + decal_fee(decal, semester, fees) + services_fee(sticker, card, semester, fees) ?? Commented Oct 9, 2014 at 1:51
  • That's the idea. You may need to wrap the 3 method calls in parenthesis if you plan to do it in one line. Commented Oct 9, 2014 at 1:54
  • Thank you so much! I added parentheses around all of the function calls and set it to the fees variable and it worked! :) Commented Oct 9, 2014 at 2:13
  • 1
    Oh, and I also realized that I should initialize the semester variable by doing: semester = semester_type(semester) ...so that I can get the proper string in the output. Commented Oct 9, 2014 at 2:15

1 Answer 1

1

Your Code:

fees = 0
unit_fee( units, resident, fees )
decal_fee( decal, semester, fees )
total_fees = services_fee( sticker, card, semester, fees )

Change this to:

fees = 0
fees+=unit_fee( units, resident, fees )
fees+=decal_fee( decal, semester, fees )
total_fees = services_fee( sticker, card, semester, fees )

When you are passing fees to unit_fee or decal_fee then just the value of the fees is getting passed so it is not changing the original value of fees that is 0.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is what my code ended up looking like anyway! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.