Create Api to create document and add,edit,delete text and t

Discussion in 'Ruby' started by Krithika San, Nov 13, 2009.

  1. Krithika San

    Krithika San Guest

    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 ?

    Thanks,
    skrithikaa
    --
    Posted via http://www.ruby-forum.com/.
     
    Krithika San, Nov 13, 2009
    #1
    1. Advertising

  2. Re: Create Api to create document and add,edit,delete text a

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Marnen Laibow-Koser, Nov 14, 2009
    #2
    1. Advertising

  3. Krithika San

    Krithika San Guest

    Re: Create Api to create document and add,edit,delete text a

    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
    >


    --
    Posted via http://www.ruby-forum.com/.
     
    Krithika San, Nov 14, 2009
    #3
  4. Re: Create Api to create document and add,edit,delete text a

    Krithika San wrote:
    > 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.
    >


    No! The comments provide all the information you need for you to do
    your own correcting. Try it yourself first. If you're stuck, read a
    reference book, search the Web, or ask your professor. If you have a
    specific question to ask, most of us here will be happy to answer it,
    but simply dumping a homework assignment that you obviously haven't even
    *tried* to work on is not OK.

    Best,
    --
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
     
    Marnen Laibow-Koser, Nov 14, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry Burns

    Re: Add/Edit/Delete Fields in Datagrid

    Terry Burns, Aug 12, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,136
    Terry Burns
    Aug 12, 2003
  2. =?Utf-8?B?UW5hdnJ5?=

    one page to edit, delete, and add data to a database

    =?Utf-8?B?UW5hdnJ5?=, Nov 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    368
    Tod Birdsall
    Nov 3, 2004
  3. =?Utf-8?B?TU4gSmF5YXNhbmthcg==?=

    Add, Edit and Delete in VWD2005

    =?Utf-8?B?TU4gSmF5YXNhbmthcg==?=, Feb 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    285
    =?Utf-8?B?TU4gSmF5YXNhbmthcg==?=
    Feb 20, 2005
  4. =?Utf-8?B?ZGVubmlzdDY4NQ==?=

    Can't edit, delete or add row in an Access database in a website 2

    =?Utf-8?B?ZGVubmlzdDY4NQ==?=, Nov 24, 2005, in forum: ASP .Net
    Replies:
    12
    Views:
    946
    Steven Cheng[MSFT]
    Dec 5, 2005
  5. =?Utf-8?B?a2Vu?=
    Replies:
    1
    Views:
    10,362
    Wiktor Zychla [C# MVP]
    Jan 23, 2006
Loading...

Share This Page