This is what I coded and I need to change so that it accepts the text,
and table rows and columns in a dynamic way.
This was the comments I obtained. Can you please correct the mistake.
Table and Text are not "is-a" DocumentBuilder
It is not clear how TableElt and TextElt contribute to the requirements
The DataTable class (which appears to be how you addressed the Table
requirements) is not related to the DocumentBuilder stuff. In fact, I'm
not sure how to create a document with a working table in it at all
Also, I don't see how to add a Text element to a document
Please put your classes in separate .rb files in the future.
The design document includes a lot of boilerplate text about the
components of an abstract factory, but it is not clear to me how any of
this relates to fulfilling the requirements of the core classes in the
assignment.
The "alter" method should just accept the row/column to "alter" rather
than have to set them prior.
Hope you can help me with this.
class DocumentBuilder
def new_document
end
end
class Table < DocumentBuilder
end
class Text < DocumentBuilder
end
# 2. implementations of specializations
class TableElt < Table
def new_document
puts "I am a word document "
end
end
class TextElt < Text
def new_document
puts "I am a word document"
end
end
# 3. interface for factory (abstract factory)
class DocumentBuilderFactory
def create_doc
end
end
# 4. different factories implementations
class TableFactory < DocumentBuilderFactory
def create_doc
TableElt.new
end
end
class TextFactory < DocumentBuilderFactory
def create_doc
TextElt.new
end
end
class DynamicFactory
def initialize(doc_class)
@doc_class = doc_class
end
def create_doc
@doc_class.new
end
end
# 5. factories manager (optional)
class DocumentBuilderManager
def create_doc_factory(type)
if(type == :Table)
TableFactory.new
elsif(type == :Text)
TextFactory.new
end
end
end
# 6. test
doc_manager = DocumentBuilderManager.new
DocumentBuilder_factory1 = doc_manager.create_doc_factory(:Table)
DocumentBuilder_factory2 = doc_manager.create_doc_factory(:Text)
DocumentBuilder_factory3 = DynamicFactory.new(TableElt)
DocumentBuilder1 = DocumentBuilder_factory1.create_doc
DocumentBuilder2 = DocumentBuilder_factory2.create_doc
DocumentBuilder3 = DocumentBuilder_factory3.create_doc
DocumentBuilder1.new_document
DocumentBuilder2.new_document
DocumentBuilder3.new_document
# creating table entries
class DataTable
attr_accessor :current_line, :current_column
attr_reader :file_name
# A table is basically an array of arrays.
# So start by creating the host array.
def initialize
@table = Array.new
@current_line = 0
@current_column = 0
end
# Allows to add elements into the table
def add_line(*entries)
@table << entries
end
# Alters the content in a table
# (note first line = 0, and first cell = 0)
def alter_current_cell(entry)
@table[@current_line][@current_column] = entry
end
# Deletes a row in a table
def remove_line(line_number)
@table.delete_at(line_number)
end
# Create an output file
def output_to_file(file_name)
@file_name = file_name
create_file
put_data_into_file
end
private
def create_file
File.delete(@file_name) if File.exist?(@file_name)
File.new(@file_name, 'w+')
end
# Add the table info, and close
def put_data_into_file
output_file = File.open(@file_name, 'w+')
for line in @table
output_file.puts line.join(" ")
end
output_file.close
end
end
data_table = DataTable.new
data_table.add_line('Country', 'Animal', 'Fruit')
data_table.add_line('India', ' Tiger ', ' Mango')
data_table.add_line('China', ' Cheeta ', 'Apple')
data_table.add_line('Japan',' Monkey ','Orange')
puts "View display_table.txt to see the contents of the table"
data_table.output_to_file('display_table.txt')
data_table.current_line = 2 # entry in this cell is changed
data_table.current_column = 1 # entry in this cell is changed
data_table.alter_current_cell(' DONKEY ') # entry in this cell is
changed to DONKEY
puts "View modified_ouput.txt to see modified content"
data_table.output_to_file('modified_ouput.txt')
data_table.current_line = 1
data_table.current_column = 2
data_table.remove_line(1)
puts "View output_without_line_1.txt to see the final output with the
removal of row 1"
data_table.output_to_file('output_without_line_1.txt')
Marnen Laibow-Koser wrote:
···
Krithika San wrote:
Sub: Create Api to create document and add, edit, delete text and
table(add row, modify and delete row)
Can somebody please tell me how to implement this using inheritence /
composition / patterns ?
Do your own homework. If you're stuck, let us know what you've tried
and what isn't working.
Thanks,
skrithikaa
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.