Skip to content

Instantly share code, notes, and snippets.

@bharathpathan
Created May 6, 2019 05:20
Show Gist options
  • Save bharathpathan/611e6fabc59abb20169359d89170348a to your computer and use it in GitHub Desktop.
Save bharathpathan/611e6fabc59abb20169359d89170348a to your computer and use it in GitHub Desktop.
class SupplierSelection
#initialize the parameters required
def initialize(suppliers)
@suppliers=suppliers
@grade_weight=0
@works={}
@supplier_data={}
end
#loading the value for the application torun.
def load_values
@suppliers.each do |supplier|
name=supplier[:name]
advaitam_grade=supplier.fetch(:advitam_grade,0)
supplier[:works].each do |work|
work_price=work[:price]
work_type=work[:type]
overall_score=(@grade_weight*advaitam_grade)+work_price
@supplier_data.merge!(name=>advaitam_grade)
if @works.key?(work_type)
@works[work_type].push({name:name,overall_score:overall_score,advaitam_grade:advaitam_grade,work_price:work_price,grade_weight:grade_weight})
else
@works.merge!(work_type=>[{name:name,overall_score:overall_score,advaitam_grade:advaitam_grade,work_price:work_price,grade_weight:grade_weight}])
end
end
end
end
# getter method for grade_weight
def grade_weight
@grade_weight
end
# setter method for grade_weight
def grade_weight=(g_weight) #setter method
@grade_weight = g_weight
load_values
end
# Return the provider with the highest :overall_score
def work(job_type)
if @works.key?(job_type) then
suppliers=@works[job_type]
suppliers.max_by{|k| k[:overall_score] }
else
"No such Job Type"
end
end
# Return all the providers sorted by :overall_score
def work_all(job_type)
if @works.key?(job_type)
suppliers=@works[job_type]
all_suppliers=suppliers.sort_by!{ |x| -x[:overall_score] }
else
"No such Job Type"
end
end
# Return all the providers sorted by :advaitam_grade
def suppliers
if @supplier_data.count>0
sups = Hash[@supplier_data.sort_by{|k, v| v}.reverse]
else
"invalid"
end
end
# Return all the providers and available job types
def all_data
if @supplier_data.count>0&&@works.count>0
sups = @supplier_data.keys
wor = @works.keys
Hash[sups.zip(wor.map {|i| i.include?(',') ? (i.split /, /) : i})]
else
"invalid"
end
end
# Takes the input of provider name and work type
# return the :overall_score for the provider with the work_type specified
def supplier_grade(supplier_name, work_type)
if @works.key?(work_type)
suppliers1=@works[work_type]
if suppliers1.any?{|hash| hash[:name] == supplier_name}
suppliers1.each do |work1|
if work1[:name]===supplier_name
s_grade=work1
return s_grade
end
end
else
return "No value"
end
else
"invalid"
end
end
end
suppliers = [
{ name: "FunePlus", advitam_grade: 3, works: [ { type: "embalming", price: 350 }, {type: "transport_before_casketing", price: 450} ]},
{ name: "FuneTop", works: [ { type: "graving", price: 10 } ]},
{ name: "FuneTruc", advitam_grade: 5,works: [ { type: "embalming", price: 750 }]},
{ name: "FuneTruc1", advitam_grade: 3,works: [ { type: "embalming", price: 350 }]},
{ name: "FuneTruc2", advitam_grade: 5,works: [ { type: "embalming", price: 751 }]},
{ name: "FuneCorp", advitam_grade: 2, works: [ { type: "digging", price: 350 }]}
]
#selection = SupplierSelection.new(suppliers)
#selection.grade_weight=20
#p selection.work("embalming")
#p selection.work_all("embalming")
#p selection.suppliers
#p selection.supplier_grade("FuneTruc","embalming")
#p selection.grade_weight
puts("Loading Data............")
sleep(1)
puts("Loaded and Ready.")
sleep(1)
puts("---------------- Welcome To SupplierSelection Console Program--------------")
puts("")
sleep(1)
puts("Before going to the home page,please provide the grade_weight value.")
puts("This grade_weight can be changed any time.")
print("Enter grade_weigh = ")
a=gets.chomp
a=a.to_i
selection = SupplierSelection.new(suppliers)
selection.grade_weight =a
puts("\n---------All-Providers----------------------All-Works_Types--------------------")
s_data= selection.all_data
s_data.each do |k,v|
print(" ",k||""," ",v||"","\n")
end
puts""
puts("------------------------------ Home Page-----------------------------------")
choice=1
#loops for the home page menu....
while choice!="7" do
puts("1. work(work_type) -> returns the provider with best global_grade")
puts("2. work_all(work_type) -> returns all the providers with sorted global_grade")
puts("3. Suppliers -> returns all the providers with best advaitam_grade")
puts("4. supplier_grade(supplier_name, work_type) -> return the global_grade of the provider for a specific work_type")
puts("5. set grade_weight -> sets the grade_weight and refreshes the data")
puts("6. Show all providers and work_types -> displays all the available providers and work_types.")
puts("7. Quit -> Quit the application.")
print("please select one of the options above in 1-5 : ")
choice=gets.chomp
case choice
when "1"
print(" \n please enter a work type : ")
work_name=gets.chomp
sups_w=selection.work(work_name)
if sups_w==="No such Job Type"
puts("\n No Work Type Available.")
sleep(2)
else
puts("\n Supplier_Name overall_score")
puts("---------------------------------------------------")
sleep(0.5)
print(sups_w[:name]," ",sups_w[:overall_score],"\n")
puts("---------------------------------------------------")
end
when "2"
print("please enter a work type : ")
work_name=gets.chomp
sups_a=selection.work_all(work_name)
if sups_a==="No such Job Type"
puts("\n No Work Type Available.")
sleep(2)
else
puts("\n Supplier_Name overall_score")
puts("---------------------------------------------------")
sups_a.each do |supplier|
sleep(0.5)
print(supplier[:name]," ",supplier[:overall_score].to_i,"\n")
puts("---------------------------------------------------")
end
sleep(2)
end
when "3"
sups = selection.suppliers
if sups==="invalid"
puts("\n No Data Available.")
sleep(2)
else
puts("\n Supplier_Name Advaitam_Grade")
puts("---------------------------------------------------")
sups.each_pair do |k,v|
print(k," ",v,"\n")
puts("---------------------------------------------------")
sleep(0.5)
end
sleep(2)
end
when "4"
print("\n please enter a Supplier Name : ")
sup_name=gets.chomp
print("\n please enter the Work_Type : ")
work_name=gets.chomp
sup_grade=selection.supplier_grade(sup_name,work_name)
if sup_grade==="invalid"
puts("\n No Supplier/work_type combination.")
sleep(2)
break
end
if sup_grade==="No value"
puts("\n Invalid Details. Try Again.")
sleep(2)
else
puts("\n Supplier_Name Advaitam_Grade")
puts("---------------------------------------------------")
sleep(0.5)
print(sup_grade[:name]," ",sup_grade[:overall_score],"\n")
puts("---------------------------------------------------")
sleep(2)
end
when "5"
print("\n Enter grade_weight (as a number) = ")
a=gets.chomp
a=a.to_i
selection.grade_weight =a
print("\n grade_weight is set to : ",a,"\n refreshing the data with new grade_weight. \n")
sleep(2)
when "6"
puts("\n---------All-Providers----------------------All-Works_Types--------------------")
s_data= selection.all_data
s_data.each do |k,v|
print(" ",k||""," ",v||"","\n")
end
when "7"
puts("---------------Quitting-----------------")
else
puts("\n Invalid Choice!!!. Please try again.")
sleep(1)
end
puts
puts("------------------------------ Home Page-----------------------------------")
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment