228 lines
6.3 KiB
Ruby
228 lines
6.3 KiB
Ruby
require_relative 'find_algos'
|
|
require_relative 'sorting_algos'
|
|
require_relative 'Type'
|
|
require_relative 'functional_functions'
|
|
require_relative 'Filter'
|
|
require_relative 'pdf-parser'
|
|
|
|
# Hi! Steffan was here...
|
|
|
|
prim_class, container, class_fields, container_params, container_methods_maggie = parse "flight.pdf"
|
|
|
|
name = prim_class
|
|
container_class = container
|
|
|
|
|
|
|
|
container_obj = "flights"
|
|
to_string_template = "0, 1 km/hour, 2 seats"
|
|
|
|
sorting_one = Type.new("String", "Airline")
|
|
sorting_two = Type.new("int", "Seats")
|
|
|
|
sorting_two.comparator = ">"
|
|
|
|
sorting_filters = [sorting_one, sorting_two]
|
|
|
|
container_fields = [ {:type => "ArrayList<#{name}>", :name => container_obj}]
|
|
|
|
params = [*class_fields.map! {|x| {:type => x.split(" ")[0], :name => x.split(" ")[1].capitalize}}]
|
|
|
|
container_methods = [
|
|
*container_methods_maggie.map! {|x|
|
|
{
|
|
:return_type => x.type,
|
|
:name => x.name.start_with?("print") ? x.name + "pls" : x.name ,
|
|
:params => [*x.parameters.split(",").map! {|y|
|
|
{
|
|
:type => y.split(" ")[0].strip,
|
|
:name => y.split(" ")[1].strip
|
|
}
|
|
}],
|
|
:method_type => nil
|
|
}
|
|
}
|
|
]
|
|
|
|
container_methods[0][:method_type] = add_method(container_obj, "f")
|
|
container_methods[1][:method_type] = find_amount_of_variable_based_on(container_obj, "Airline", ".equals", "airline", "Seats")
|
|
container_methods[2][:method_type] = find_best_with_extra_parameter(container_obj, "Speed", ">", "Flight", "Airline", ".equals", "airline" )
|
|
container_methods[4][:method_type] = func_find_all(container_obj, "Speed", [Filter.new("min", ">="), Filter.new("max", "<=")])
|
|
# find_whatever_of_variable(container_obj, attribute, filter_by, aggregate_func, variable)
|
|
container_methods[5][:method_type] = find_whatever_of_something(container_obj, "Speed", [Filter.new("minSpeed", ">")], "count")
|
|
|
|
container_params.map! do |x|
|
|
{:type => x.split(" ")[0], :name => x.split(" ")[1].capitalize}
|
|
end
|
|
|
|
# Hack to clear duplicates, which most definitely won't always work
|
|
container_params = container_params|[]
|
|
|
|
driver_params = container_params
|
|
|
|
driver_methods = container_methods.slice(1, container_methods.length-1).map do |x|
|
|
{:name => x[:name], :parameters => x[:params], :print => !x[:name].start_with?("print")}
|
|
end
|
|
|
|
|
|
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|
|
|
to_string_template.sub! "#{x}", "\" + #{parameters[x][:name]} + \""
|
|
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|
|
|
"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
|
|
|
|
file = File.open("#{name}.java", "w")
|
|
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, container_params, container_fields, container_methods, container_obj))
|
|
another_another_file.close
|
|
|
|
another_file = File.open("TestDriver.java", "w")
|
|
another_file.puts(construct_driver name, params, driver_methods, container_class, driver_params)
|
|
another_file.close
|
|
|
|
`javac *.java`
|
|
`java TestDriver`
|