Four checkpoints

This commit is contained in:
Alexander Munch-Hansen 2019-10-14 12:54:53 +02:00
parent a7374d2c67
commit e040269cce
3 changed files with 139 additions and 7 deletions

12
Type.rb Normal file
View File

@ -0,0 +1,12 @@
class Type
attr_accessor :type
attr_accessor :variable
attr_accessor :comparator
def initialize(type, var)
@type = type
@variable = var
end
end

42
lol.rb
View File

@ -1,10 +1,21 @@
require_relative 'find_algos' require_relative 'find_algos'
require_relative 'sorting_algos' require_relative 'sorting_algos'
require_relative 'Type'
# Hi! Steffan was here... # Hi! Steffan was here...
def construct_class(name_of_class, parameters, to_string_template) def construct_class(name_of_class, parameters, to_string_template)
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_string = compare_to_three_vars(sorting_one, sorting_two, sorting_three)
fields = "" fields = ""
parameters.each do |x| parameters.each do |x|
fields += "private #{x[:type]} #{x[:name]};" fields += "private #{x[:type]} #{x[:name]};"
@ -33,7 +44,7 @@ def construct_class(name_of_class, parameters, to_string_template)
end end
" "
public class #{name_of_class} { public class #{name_of_class} implements Comparable<#{name_of_class}> {
#{fields} #{fields}
public #{name_of_class}(#{constructor_parameters}) { public #{name_of_class}(#{constructor_parameters}) {
@ -46,6 +57,10 @@ def construct_class(name_of_class, parameters, to_string_template)
public String toString() { public String toString() {
return " + '"' + "#{to_string_template}" + '"' + "; return " + '"' + "#{to_string_template}" + '"' + ";
} }
public int compareTo(#{name_of_class} other) {
#{sorting_string}
}
} }
" "
@ -72,10 +87,13 @@ def construct_driver(class_name, parameters, methods, container_class, container
methods_to_print = "" methods_to_print = ""
maggie = methods.map do |x| maggie = methods.map do |x|
if x[:print]
" "
System.out.println(lol.#{x[:name]}(#{args x[:parameters]})); System.out.println(lol.#{x[:name]}(#{args x[:parameters]}));
" "
else
"lol.#{x[:name]}(#{args x[:parameters]});"
end
end end
driver_string = " driver_string = "
@ -147,6 +165,7 @@ def construct_container(name, parameters, given_fields, methods, arraylist_name)
container_string = " container_string = "
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class #{name} { public class #{name} {
#{fields} #{fields}
@ -157,7 +176,17 @@ def construct_container(name, parameters, given_fields, methods, arraylist_name)
} }
#{maggie.join "\n"} #{maggie.join "\n"}
public void print#{name}() {
System.out.println(\"#{name}\");
Collections.sort(#{arraylist_name});
for (var i : #{arraylist_name}) {
System.out.println(i);
}
} }
}
" "
container_string container_string
@ -166,7 +195,7 @@ end
name = "Ferry" name = "Ferry"
file = File.open("#{name}.java", "w") file = File.open("#{name}.java", "w")
params = [{:type => "String", :name => "Name"}, {:type => "int", :name => "Length"}, {:type => "int", :name => "width"}] params = [{:type => "String", :name => "Name"}, {:type => "int", :name => "Length"}, {:type => "int", :name => "Width"}]
file.puts(construct_class name, params, "0 1 x 2 meter") file.puts(construct_class name, params, "0 1 x 2 meter")
file.close file.close
@ -200,6 +229,10 @@ container_methods = [
:method_type => find_best_with_extra_parameter("ferries", "Length", " > ", "#{name}", "Name", ".equals", "Name", "int") :method_type => find_best_with_extra_parameter("ferries", "Length", " > ", "#{name}", "Name", ".equals", "Name", "int")
} }
] ]
container_class = "Harbour" container_class = "Harbour"
another_another_file = File.open("#{container_class}.java", "w") 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 => "ferries"}], container_methods, "ferries"))
@ -209,7 +242,8 @@ another_another_file.close
another_file = File.open("TestDriver.java", "w") another_file = File.open("TestDriver.java", "w")
another_file.puts(construct_driver name, params, [{:name => "smallFerries", :parameters => [{:type => "int"}], :print => true}, another_file.puts(construct_driver name, params, [{:name => "smallFerries", :parameters => [{:type => "int"}], :print => true},
{:name => "longFerry", :parameters => [{:type => "String"}], :print => true}], "Harbour", [{:type => "String"}]) {:name => "longFerry", :parameters => [{:type => "String"}], :print => true},
{:name => "printHarbour", :parameters => [], :print => false}], "Harbour", [{:type => "String"}])
another_file.close another_file.close

View File

@ -1,7 +1,93 @@
def compare_to_two_vars
# TODO: Make variadic, such that we only need one method
def compare_to_two_vars(first, second)
if first.type == "String"
if_statement = "
if (!this.#{first.variable}.equals(other.#{first.variable}) {
return this.#{first.variable}.compareTo(other.#{first.variable});
}
"
else
if first.comparator == ">"
return_string = "return this.#{first.variable} - other.#{first.variable};"
else
return_string = "return other.#{first.variable} - this.#{first.variable};"
end
if_statement = "
if (this.#{first_variable} != other.#{first.variable} {
#{return_string}
}
"
end
if second.type == "String"
return_string = "return this.#{second.variable}.compareTo(other.#{second.variable});"
else
if second.comparator == ">"
return_string = "return this.#{second.variable} - other.#{second.variable};"
else
return_string = "return other.#{second.variable} - this.#{second.variable};"
end
end
"
#{if_statement}
#{return_string}
"
end end
def compare_to_three_vars def compare_to_three_vars(first, second, third)
if first.type == "String"
if_statement = "
if (!this.#{first.variable}.equals(other.#{first.variable})) {
return this.#{first.variable}.compareTo(other.#{first.variable});
}
"
else
if first.comparator == ">"
return_string = "return this.#{first.variable} - other.#{first.variable};"
else
return_string = "return other.#{first.variable} - this.#{first.variable};"
end
if_statement = "
if (this.#{first.variable} != other.#{first.variable}) {
#{return_string}
}
"
end
if second.type == "String"
else_if_statement = "
else if (!this.#{second.variable}.equals(other.#{second.variable})) {
return this.#{second.variable}.compareTo(other.#{second.variable};
}
"
else
if second.comparator == ">"
return_string = "return this.#{second.variable} - other.#{second.variable};"
else
return_string = "return other.#{second.variable} - this.#{second.variable};"
end
else_if_statement = "
else if (this.#{second.variable} != other.#{second.variable}) {
#{return_string}
}
"
end
if third.type == "String"
return_string = "return this.#{third.variable}.compareTo(other.#{third.variable});"
else
if third.comparator == ">"
return_string = "return this.#{third.variable} - other.#{third.variable};"
else
return_string = "return other.#{third.variable} - this.#{third.variable};"
end
end
"
#{if_statement}
#{else_if_statement}
#{return_string}
"
end end