Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CountryModule/country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load("country_module.rb")
class Country
include CountryModule
end
33 changes: 33 additions & 0 deletions CountryModule/country_module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module CountryModule
attr_reader :military_strength
def add_country(no_of_states, population, gdp, military_strength, status)
@no_of_states = no_of_states
@population = population
@gdp = gdp
@military_strength = military_strength
@status = status
end

def can_get_loan?
return puts "Unfortunately Your country is not eligible to get loan." unless @gdp >= 4.5 && @status == "DEVELOPING" || @status == "DEVELOPED"
puts "Yes. Your country can get loan."
end

def can_win_war?(rival_country_name)
return puts "Your country will defeat in war." unless @military_strength >= rival_country_name.military_strength
puts "Your country can win in war."
end

def can_register_for_UN?
return puts "Your country can't register for UN." unless @gdp >= 3 && @population >= 10000000 || @status == "DEVELOPED"
puts "Your country can register for UN."
end

def display_details
puts "\tPopulation: #{@population}"
puts "\tNo of states: #{@no_of_states}"
puts "\tMilitary strength: #{@military_strength}"
puts "\tGDP: #{@gdp}"
puts "\tStatus: #{@status}"
end
end
78 changes: 78 additions & 0 deletions CountryModule/country_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
load("country.rb")
Countries = Hash.new
choice = "Y"
while choice == "Y" || choice == "y"
begin
puts "-----Operations-----"
puts "1.Add new country"
puts "2.Can apply for UN?"
puts "3.Can win war?"
puts "4.Can get loan from WB?"
puts "5.Display all countries"
puts "6.Exit"
print "Enter Operation: "
operation = Integer(gets)

case operation
when 1
print "Enter Country Name: "
country_name = gets.chomp.upcase
exit puts "Country name should contain alphabets only" unless country_name.match(/^[A-Z]+\s?[A-Z]*\s?[A-Z]*$/)
print "Enter Number of States: "
no_of_states = Integer(gets)
print "Enter Population: "
population = Integer(gets)
print "Enter GDP: "
gdp = Float(gets)
print "Enter Military Strength: "
military_strength = Integer(gets)
exit puts "Military strength can't be more than population" unless population > military_strength
print "Enter status of country(Developing/Developed): "
status = gets.chomp.upcase
exit puts "Invaild status !!!" unless status == "DEVELOPING" || status == "DEVELOPED"
country_details = Country.new
country_details.add_country(no_of_states,population,gdp,military_strength,status)
Countries.store(country_name,country_details)
puts "Country added successfully..."

when 2
print "Country Name: "
country_name = gets.chomp.upcase
exit puts "No such country exists !!!" unless Countries.has_key?(country_name)
Countries.fetch(country_name).can_register_for_UN?

when 3
print "Country Name: "
country_name = gets.chomp.upcase
exit puts "No such country exists !!!" unless Countries.has_key?(country_name)
print "Rival Country Name: "
rival_country_name = gets.chomp.upcase
exit puts "No such country exists !!!" unless Countries.has_key?(rival_country_name)
Countries.fetch(country_name).can_win_war?(Countries.fetch(rival_country_name))

when 4
print "Country Name: "
country_name = gets.chomp.upcase
exit puts "No such country exists !!!" unless Countries.has_key?(country_name)
Countries.fetch(country_name).can_get_loan?

when 5
Countries.each do |country_name,country_info|
puts "#{country_name} => "
country_info.display_details
end

when 6
exit

else
puts "Invalid Operation !!!"
end

rescue TypeError, ArgumentError
puts "Invalid Input !!!"
end

print "Do you want to continue?(Y/N)... "
choice = gets[0]
end