Exercise
Table + SetOfTables + files
Objetive
Expand the exercise (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.
Example Code
using System;
using System.IO;
class Table
{
public string Name { get; set; }
public string Description { get; set; }
public int[] Data { get; set; }
public Table(string name, string description, int[] data)
{
Name = name;
Description = description;
Data = data;
}
public void DumpToBinaryFile(string fileName)
{
try
{
using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write(Name);
writer.Write(Description);
writer.Write(Data.Length);
foreach (var item in Data)
{
writer.Write(item);
}
}
Console.WriteLine("Data dumped successfully to the binary file.");
}
catch (Exception ex)
{
Console.WriteLine($"Error writing to binary file: {ex.Message}");
}
}
public static Table RestoreFromBinaryFile(string fileName)
{
try
{
using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
string name = reader.ReadString();
string description = reader.ReadString();
int length = reader.ReadInt32();
int[] data = new int[length];
for (int i = 0; i < length; i++)
{
data[i] = reader.ReadInt32();
}
return new Table(name, description, data);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading from binary file: {ex.Message}");
return null;
}
}
}
class SetOfTables
{
public Table[] Tables { get; set; }
public SetOfTables(Table[] tables)
{
Tables = tables;
}
public void DumpAllToBinaryFile(string fileName)
{
try
{
using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write(Tables.Length);
foreach (var table in Tables)
{
table.DumpToBinaryFile(fileName);
}
}
Console.WriteLine("All table data dumped successfully to the binary file.");
}
catch (Exception ex)
{
Console.WriteLine($"Error writing to binary file: {ex.Message}");
}
}
public static SetOfTables RestoreAllFromBinaryFile(string fileName)
{
try
{
using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
int numberOfTables = reader.ReadInt32();
Table[] tables = new Table[numberOfTables];
for (int i = 0; i < numberOfTables; i++)
{
tables[i] = Table.RestoreFromBinaryFile(fileName);
}
return new SetOfTables(tables);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading from binary file: {ex.Message}");
return null;
}
}
}
class Program
{
static void Main(string[] args)
{
int[] tableData1 = new int[] { 1, 2, 3, 4, 5 };
int[] tableData2 = new int[] { 6, 7, 8, 9, 10 };
Table table1 = new Table("Table1", "First Table Description", tableData1);
Table table2 = new Table("Table2", "Second Table Description", tableData2);
SetOfTables setOfTables = new SetOfTables(new Table[] { table1, table2 });
setOfTables.DumpAllToBinaryFile("allTablesData.bin");
SetOfTables restoredSet = SetOfTables.RestoreAllFromBinaryFile("allTablesData.bin");
if (restoredSet != null)
{
foreach (var restoredTable in restoredSet.Tables)
{
Console.WriteLine($"Restored Table Name: {restoredTable.Name}");
Console.WriteLine($"Restored Table Description: {restoredTable.Description}");
Console.WriteLine("Restored Data:");
foreach (var item in restoredTable.Data)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
}
}
}