Table + SetOfTables + files Learn programming C#

Lesson:

Object Persistence


Exercise:

Table + SetOfTables + files


Objetive:

Expand the exercise from April 16th (tables + array + files) by creating three classes: Table, SetOfTables, and a test program. The SetOfTables class should contain an array of tables, as well as two methods to dump all data from the array into a binary file and restore data from the file.


Code:

using System;
using System.Collections;
using System.IO;

namespace Tables
{
    class SetOfTables
    {
        private int size;
        private ArrayList data;
        Random random;

        public SetOfTables(int newSize)
        {
            size = newSize;
            data = new ArrayList();
            random = new Random();
        }

        public SetOfTables()
            : this(10)
        {
        }

        public void CreateAtRandom()
        {
            data = new ArrayList();
            for (int i = 0; i < size; i++)
            {
                data.Add( new Table(random.Next(50, 201); random.Next(50, 201);
             }
        }

        public void ShowData()
        {
            foreach (Table t in data)
            {
                t.ShowData();
            }
            Console.WriteLine();
        }

        public void Save(string name)
        {
            BinaryWriter outputFile = new BinaryWriter(
                File.Open(name, FileMode.Create));
            outputFile.Write((int)size);
            foreach (Table t in data)
            {
                outputFile.Write(t.GetHeight());
                outputFile.Write(t.GetWidth());
            }
            outputFile.Close();
        }

        public void Load(string name)
        {
            BinaryReader inputFile = new BinaryReader(
                File.Open(name, FileMode.Open));
            int size = inputFile.ReadInt32();
            data = new ArrayList();

            for (int i = 0; i < size; i++)
            {
                int height = inputFile.ReadInt32();
                int width = inputFile.ReadInt32();
                data.Add(new Table(width, height));
            }
            inputFile.Close();
        }
    }
}

using System;
using System.IO;
namespace Tables
{
    class Table
    {
        protected int width, height;

        public Table(int tableWidth, int tableHeight)
        {
            width = tableWidth;
            height = tableHeight;
        }

        public int GetHeight()
        {
            return height;
        }

        public int GetWidth()
        {
            return width;
        }

        public void ShowData()
        {
            Console.WriteLine("Width: {0}, Height: {1}", width, height);
        }

        public void Save(string name)
        {
            BinaryWriter outputFile = new BinaryWriter(
            File.Open(name, FileMode.Create));
            outputFile.Write(height);
            outputFile.Write(width);
            outputFile.Close();
        }

        public void Load(string name)
        {
            BinaryReader inputFile = new BinaryReader(
            File.Open(name, FileMode.Open));
            height = inputFile.ReadInt32();
            width = inputFile.ReadInt32();
            inputFile.Close();
        }
    }
}

using System;
namespace Tables
{
    class TestTable
    {
        static void Main(string[] args)
        {
            SetOfTables s = new SetOfTables(5);
            s.CreateAtRandom();
            s.ShowData();

            s.Save("tables.dat");
            s.CreateAtRandom();
            s.ShowData();

            s.Load("tables.dat");
            s.ShowData();

            Console.ReadLine();
        }
    }
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.