In: Computer Science
CODE:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Derived_Banking
{
// Base Accoutn Class
class Account
{
//Properties
public string Name { get; set; }
public int AccountNumber { get; set; }
public double Balance { get; set; }
}
// Checkings - Inheriting Account's class.
class Checkings : Account
{
//Properties
public double OverDraftLimit { get; set; }
}
// Savings - Inheriting Account's class.
class Savings : Account
{
//Properties
public double InterestRate { get; set; }
}
//Creditcard - Inheriting Account's class.
class Creditcard : Account
{
//Properties
public int CardNumber { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Savings account List(Array)
List<Savings> lstSavings = new List<Savings>();
foreach (KeyValuePair<int, List<string>> item in
ReadFiles("Savings.txt"))
{
// check if line content is empty
if (item.Value.Count > 0)
{
//add the respective account obejct to the list intialized
before
lstSavings.Add(new Savings
{
Name = item.Value[0],
AccountNumber = Convert.ToInt32(item.Value[1]),
Balance = Convert.ToDouble(item.Value[2]),
InterestRate = Convert.ToDouble(item.Value[3])
});
}
}
Console.WriteLine("Printing value for Savings Account Values
-");
// loop through the list of accounts and print them one by
one
for (int i = 1; i <= lstSavings.Count(); i++)
{
Print_AccountValues(lstSavings[i - 1], i);
}
// Checkings account List(Array)
List<Checkings> lstCheckings = new
List<Checkings>();
foreach (KeyValuePair<int, List<string>> item in
ReadFiles("Checkings.txt"))
{
if (item.Value.Count > 0)
{
lstCheckings.Add(new Checkings
{
Name = item.Value[0],
AccountNumber = Convert.ToInt32(item.Value[1]),
Balance = Convert.ToDouble(item.Value[2]),
OverDraftLimit = Convert.ToDouble(item.Value[3])
});
}
}
Console.WriteLine("Printing value for Checkings Account Values
-");
// loop through the list of accounts and print them one by
one
for (int i = 1; i <= lstCheckings.Count(); i++)
{
Print_AccountValues(lstCheckings[i - 1], i);
}
// Credit Card List(Array)
List<Creditcard> lstCredits = new
List<Creditcard>();
foreach (KeyValuePair<int, List<string>> item in
ReadFiles("CreditCards.txt"))
{
if (item.Value.Count > 0)
{
lstCredits.Add(new Creditcard
{
Name = item.Value[0],
AccountNumber = Convert.ToInt32(item.Value[1]),
Balance = Convert.ToDouble(item.Value[2]),
CardNumber = Convert.ToInt32(item.Value[3])
});
}
}
Console.WriteLine("Printing value for Credit Card Values
-");
// loop through the list of accounts and print them one by
one
for (int i = 1; i <= lstCredits.Count(); i++)
{
Print_AccountValues(lstCredits[i - 1], i);
}
Console.ReadLine();
}
// Reads the file line by line and separtes the value in a line
using comma(,)
// stores the file content into dictionay of line and line
content
// returns the dictionary
static Dictionary<int, List<string>> ReadFiles(string
fileName)
{
Dictionary<int, List<string>> tempDict = new
Dictionary<int, List<string>>();
//wrap IO operations in try catch block
try
{
int lineCntr = 1;
foreach (string line in File.ReadAllLines(fileName))
{
if (!string.IsNullOrEmpty(line))
tempDict.Add(lineCntr, line.Split(',').ToList());
lineCntr++;
}
return tempDict;
}
catch (Exception ex)
{
throw;
}
}
// Generalized function to print all the properties
// of an object along with its values
static void Print_AccountValues(object obj, int cntr)
{
Console.WriteLine("Printing Values for Counter: " + cntr);
foreach (var item in obj.GetType().GetProperties())
{
Console.WriteLine(item.Name + " : " + item.GetValue(obj,
null));
}
}
}
}
Text File Content:
Savings.txt:
Robert Langdon,290558,23467.37,3.50
Mike Dothraki,3190056,2364.90,2.67
Demitri Ghorkawski,1404500,78659008.87,6.76
Checkings.txt:
Robert Manning,45678003,23467.67,10000
Mike Dothraki,31907800,2364.67,34000
Sansa Steward,6780003,12348.67,1300
CreditCards.txt:
John Bolton,31897556,39067.47,4100056
Mike Dothraki,31905956,1045.69,4103056
Arya Stark,4317678,12348.67,51067000
RESULT: