From c324a4a0a0c2a37b5354ad2b7899a916855b6a38 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Mon, 14 Oct 2019 14:38:22 +0200 Subject: [PATCH] Completed a full exam --- functional_functions.rb | 10 ++++++++++ lol.rb | 23 +++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/functional_functions.rb b/functional_functions.rb index 28b3597..78bedf0 100644 --- a/functional_functions.rb +++ b/functional_functions.rb @@ -8,6 +8,7 @@ def func_find_all(container_obj, attribute, filter_by) end def find_whatever_of_something(container_obj, attribute, filter_by, aggregate_func) + # TODO: Fix comparator to work for strings, such that I use .equals instead of == filters = filter_by.map do |x| "s.get#{attribute}() #{x.comparator} #{x.value}" end @@ -15,6 +16,15 @@ def find_whatever_of_something(container_obj, attribute, filter_by, aggregate_fu "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).#{aggregate_func}();" end +def find_whatever_of_variable(container_obj, attribute, filter_by, aggregate_func, variable) + # TODO: Fix comparator to work for strings, such that I use .equals instead of == + filters = filter_by.map do |x| + "s.get#{attribute}() #{x.comparator} #{x.value}" + end + # TODO: Consider also having an "or" here + "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).mapToInt(s -> s.get#{variable}()).#{aggregate_func}();" +end + def find_one(container_obj, attribute, filter_by) filters = filter_by.map do |x| "s.get#{attribute}() #{x.comparator} #{x.value}" diff --git a/lol.rb b/lol.rb index f6be68b..bc2e2e4 100644 --- a/lol.rb +++ b/lol.rb @@ -8,6 +8,7 @@ require_relative 'Filter' # Hi! Steffan was here... +container_obj = "ferries" def construct_class(name_of_class, parameters, to_string_template) sorting_one = Type.new("String", "Name") @@ -210,7 +211,7 @@ container_methods = [ :type => "Ferry", :name => "f" }], - :method_type => add_method("ferries", "f") + :method_type => add_method(container_obj, "f") }, { :return_type => "ArrayList", @@ -219,7 +220,7 @@ container_methods = [ :type => "int", :name => "maxLength" }], - :method_type => find_all("ferries", "Length", " < ", "#{name}", "maxLength") + :method_type => find_all(container_obj, "Length", " < ", "#{name}", "maxLength") }, { :return_type => "#{name}", @@ -228,7 +229,7 @@ container_methods = [ :type => "String", :name => "Name" }], - :method_type => find_best_with_extra_parameter("ferries", "Length", " > ", "#{name}", "Name", ".equals", "Name", "int") + :method_type => find_best_with_extra_parameter(container_obj, "Length", " > ", "#{name}", "Name", ".equals", "Name", "int") }, { :return_type => "List<#{name}>", @@ -241,7 +242,16 @@ container_methods = [ :type => "int", :name => "max" }], - :method_type => func_find_all("ferries", "Width", [Filter.new("min", ">="), Filter.new("max", "<=")]) + :method_type => func_find_all(container_obj, "Width", [Filter.new("min", ">="), Filter.new("max", "<=")]) + }, + { + :return_type => "int", + :name => "findLength", + :params => [{ + :type => "String", + :name => "Name" + }], + :method_type => find_whatever_of_variable(container_obj, "Name", [Filter.new("Name", "==")], "sum", "Length") } ] @@ -250,7 +260,7 @@ container_methods = [ container_class = "Harbour" another_another_file = File.open("#{container_class}.java", "w") -another_another_file.puts(construct_container(container_class, [{:type => "String", :name => "Name"}], [ {:type => "ArrayList<#{name}>", :name => "ferries"}], container_methods, "ferries")) +another_another_file.puts(construct_container(container_class, [{:type => "String", :name => "Name"}], [ {:type => "ArrayList<#{name}>", :name => container_obj}], container_methods, container_obj)) another_another_file.close # TODO: When creating find algorithms, remember to add a space or two (lol for consistency) for <, >, <=, >= @@ -259,7 +269,8 @@ another_file = File.open("TestDriver.java", "w") another_file.puts(construct_driver name, params, [{:name => "smallFerries", :parameters => [{:type => "int"}], :print => true}, {:name => "longFerry", :parameters => [{:type => "String"}], :print => true}, {:name => "printHarbour", :parameters => [], :print => false}, - {:name => "findFerries", :parameters => [{:type => "int"}, {:type => "int"}], :print => true }], "Harbour", [{:type => "String"}]) + {:name => "findFerries", :parameters => [{:type => "int"}, {:type => "int"}], :print => true }, + {:name => "findLength", :parameters => [{:type => "String"}], :print => true}], "Harbour", [{:type => "String"}]) another_file.close