|
| 1 | +# A ruby program for weight conversions |
| 2 | + |
| 3 | +module WeightConversion |
| 4 | + # Kilogram -> Gram = (kilogram_value * 1000) grams |
| 5 | + def self.kilogram_to_gram(kilogram_input) |
| 6 | + raise StandardError unless Integer === kilogram_input |
| 7 | + |
| 8 | + gram = kilogram_input * 1000 |
| 9 | + |
| 10 | + "#{kilogram_input} kg = #{gram} g" |
| 11 | + end |
| 12 | + |
| 13 | + # Gram -> Kilogram = (gram_value / 1000) kilograms |
| 14 | + def self.gram_to_kilogram(gram_input) |
| 15 | + kilogram = gram_input / 1000 |
| 16 | + |
| 17 | + "#{gram_input} g = #{kilogram} kg" |
| 18 | + end |
| 19 | + |
| 20 | + # Pound -> Ounce = (pound_value * 16) oz |
| 21 | + def self.pound_to_ounce(pound_input) |
| 22 | + ounce = pound_input * 16 |
| 23 | + |
| 24 | + "#{pound_input} lb = #{ounce} oz" |
| 25 | + end |
| 26 | + |
| 27 | + # Ounce -> Pound = (ounce_value / 16) lb |
| 28 | + def self.ounce_to_pound(ounce_input) |
| 29 | + pound = ounce_input / 16 |
| 30 | + |
| 31 | + "#{ounce_input} oz = #{pound} lb" |
| 32 | + end |
| 33 | + |
| 34 | + # Kilogram -> Pound = (kilogram_input * 2.205) lb |
| 35 | + def self.kilogram_to_pound(kilogram_input) |
| 36 | + pound = (kilogram_input * 2.205).round(2) |
| 37 | + |
| 38 | + "#{kilogram_input} kg = #{pound} lb" |
| 39 | + end |
| 40 | + |
| 41 | + # Pound -> Kilogram = (pound_input / 2.205) kg |
| 42 | + def self.pound_to_kilogram(pound_input) |
| 43 | + raise StandardError unless Integer === pound_input |
| 44 | + |
| 45 | + kilogram = (pound_input / 2.205).round(2) |
| 46 | + |
| 47 | + "#{pound_input} lb = #{kilogram} kg" |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +# |
| 52 | +# Valid inputs |
| 53 | +# |
| 54 | + |
| 55 | +puts WeightConversion.kilogram_to_gram(2) |
| 56 | +# 2 kg = 2000 g |
| 57 | +puts WeightConversion.gram_to_kilogram(3000) |
| 58 | +# 3000 g = 3 kg |
| 59 | +puts WeightConversion.pound_to_ounce(16) |
| 60 | +# 16 lb = 256 oz |
| 61 | +puts WeightConversion.ounce_to_pound(16) |
| 62 | +# 16 oz = 1 lb |
| 63 | +puts WeightConversion.kilogram_to_pound(1) |
| 64 | +# 1 kg = 2.21 lb |
| 65 | +puts WeightConversion.pound_to_kilogram(100) |
| 66 | +# 100 lb = 45.35 kg |
| 67 | + |
| 68 | +# |
| 69 | +# Invalid inputs |
| 70 | +# |
| 71 | + |
| 72 | +begin |
| 73 | + puts WeightConversion.kilogram_to_gram("a") |
| 74 | +rescue StandardError |
| 75 | + puts "Error: Please provide number only!" |
| 76 | +end |
| 77 | + |
| 78 | +begin |
| 79 | + puts WeightConversion.kilogram_to_gram("3000") |
| 80 | +rescue StandardError |
| 81 | + puts "Error: Please provide number only!" |
| 82 | +end |
| 83 | + |
| 84 | +begin |
| 85 | + puts WeightConversion.kilogram_to_gram("16") |
| 86 | +rescue StandardError |
| 87 | + puts "Error: Please provide number only!" |
| 88 | +end |
| 89 | + |
| 90 | +begin |
| 91 | + puts WeightConversion.kilogram_to_gram("x ") |
| 92 | +rescue StandardError |
| 93 | + puts "Error: Please provide number only!" |
| 94 | +end |
| 95 | + |
| 96 | +begin |
| 97 | + puts WeightConversion.kilogram_to_gram("weight") |
| 98 | +rescue StandardError |
| 99 | + puts "Error: Please provide number only!" |
| 100 | +end |
| 101 | + |
| 102 | +begin |
| 103 | + puts WeightConversion.kilogram_to_gram("100") |
| 104 | +rescue StandardError |
| 105 | + puts "Error: Please provide number only!" |
| 106 | +end |
0 commit comments