require_relative 'find_algos' require_relative 'sorting_algos' require_relative 'Type' require_relative 'functional_functions' require_relative 'Filter' # Hi! Steffan was here... container_obj = "ferries" name = "Ferry" container_class = "Harbour" to_string_template = "0 1 x 2 meter" sorting_one = Type.new("String", "Name") sorting_two = Type.new("int", "Length") sorting_three = Type.new("int", "Width") sorting_two.comparator = "<" sorting_three.comparator = "<" sorting_filters = [sorting_one, sorting_two, sorting_three] def construct_class(name_of_class, parameters, to_string_template, sorting_filters) sorting_string = compare_to(sorting_filters) fields = parameters.map do |x| "\tprivate #{x[:type]} #{x[:name]};" end constructor_parameters = parameters.map { |x| "#{x[:type]} #{x[:name]}"}.join "," getters = parameters.map do |x| "\tpublic #{x[:type]} get#{x[:name]}(){ \t\treturn #{x[:name]}; \t}" end.join "\n" lol = parameters.map do |x| "\t\tthis.#{x[:name]} = #{x[:name]};" end (0...parameters.length).each do |x| pls = "\" + #{parameters[x][:name]} + \"" to_string_template.sub! "#{x}", pls end " public class #{name_of_class} implements Comparable<#{name_of_class}> { #{fields.join "\n"} \tpublic #{name_of_class}(#{constructor_parameters}) { #{lol.join("\n")} \t} #{getters} \tpublic String toString() { \t\treturn " + '"' + "#{to_string_template}" + '"' + "; \t} \tpublic int compareTo(#{name_of_class} other) { #{sorting_string} \t} }" end def construct_driver(class_name, parameters, methods, container_class, container_params) args = lambda {|parameters| parameters.map do |x| type = x[:type] type == "int" ? "#{rand(max=100)}" : "\"#{(0...10).map { ('a'..'z').to_a[rand(26)] }.join}\"" end.join "," } maggie = methods.map do |x| if x[:print] "System.out.println(lol.#{x[:name]}(#{args.call x[:parameters]}));" else "lol.#{x[:name]}(#{args.call x[:parameters]});" end end "public class TestDriver { \tpublic TestDriver(){} \tpublic static void test() { \t\tvar tmp1 = new #{class_name}(#{args.call parameters}); \t\tvar tmp2 = new #{class_name}(#{args.call parameters}); \t\tvar tmp3 = new #{class_name}(#{args.call parameters}); \t\tvar tmp4 = new #{class_name}(#{args.call parameters}); \t\tvar tmp5 = new #{class_name}(#{args.call parameters}); \t\tSystem.out.println(tmp1); \t\tSystem.out.println(tmp2); \t\tSystem.out.println(tmp3); \t\tSystem.out.println(tmp4); \t\tSystem.out.println(tmp5); \t\t#{container_class} lol = new #{container_class}(#{args.call container_params}); \t\tlol.add#{class_name}(tmp1); \t\tlol.add#{class_name}(tmp2); \t\tlol.add#{class_name}(tmp3); \t\tlol.add#{class_name}(tmp4); \t\tlol.add#{class_name}(tmp5); \t\tSystem.out.println(\"\"); \t\tSystem.out.println(\"Some checkpoint\"); \t\t#{maggie.join "\n\n\t\tSystem.out.println(\"Some Checkpoint\"); \n\t\t"} \t} \tpublic static void main(String[] args) { \t\tTestDriver.test(); \t} }" end def construct_container(name, parameters, given_fields, methods, arraylist_name) fields = "" parameters.each do |x| fields += "\tprivate #{x[:type]} #{x[:name]};\n" end given_fields.each do |x| fields += "\tprivate #{x[:type]} #{x[:name]};\n" end maggie = methods.map do |x| " \tpublic #{x[:return_type]} #{x[:name]} (#{x[:params].map {|y| "#{y[:type]} #{y[:name]}"}.join ","}) { \t\t#{x[:method_type]} \t} " end construtor_params = parameters.map do |x| "#{x[:type]} #{x[:name]}" end constructor_handlers = parameters.map do |x| print(x[:name]) "this.#{x[:name]} = #{x[:name]};" end "import java.util.*; import java.util.stream.Collectors; public class #{name} { #{fields} \tpublic #{name}(#{construtor_params.join ","}) { \t\t#{constructor_handlers.join "\n"} \t\tthis.#{arraylist_name} = new ArrayList<>(); \t} #{maggie.join ""} \tpublic void print#{name}() { \t\tSystem.out.println(\"#{name}\"); \t\tCollections.sort(#{arraylist_name}); \t\tfor (var i : #{arraylist_name}) { \t\t\tSystem.out.println(i); \t\t} \t} }" end container_methods = [ { :return_type => "void", :name => "addFerry", :params => [{ :type => "Ferry", :name => "f" }], :method_type => add_method(container_obj, "f") }, { :return_type => "ArrayList", :name => "smallFerries", :params => [{ :type => "int", :name => "maxLength" }], :method_type => find_all(container_obj, "Length", " < ", "#{name}", "maxLength") }, { :return_type => "#{name}", :name => "longFerry", :params => [{ :type => "String", :name => "Name" }], :method_type => find_best_with_extra_parameter(container_obj, "Length", " > ", "#{name}", "Name", ".equals", "Name", "int") }, { :return_type => "List<#{name}>", :name => "findFerries", :params => [{ :type => "int", :name => "min", }, { :type => "int", :name => "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", ".equals")], "sum", "Length") } ] file = File.open("#{name}.java", "w") params = [{:type => "String", :name => "Name"}, {:type => "int", :name => "Length"}, {:type => "int", :name => "Width"}] file.puts(construct_class name, params, to_string_template, sorting_filters) file.close 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 => 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 <, >, <=, >= 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 }, {:name => "findLength", :parameters => [{:type => "String"}], :print => true}], container_class, [{:type => "String"}]) another_file.close `javac *.java` `java TestDriver`