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! :)