Hello I need assitance setting up the format for the text files. I\'m not sure h
ID: 3585858 • Letter: H
Question
Hello I need assitance setting up the format for the text files. I'm not sure how to do the text files. C#
Please see code below
class Account
{
//
string name;
//
int accountNumber;
//
double balance;
//
public Account()
{
//empty default constructor
}
//
// parameterized constructor
public Account(String nm, int acc, double bal)
{
//
accountNumber = acc;
//
name = nm;
//
balance = bal;
}
}
//
// Savings class derived from Accounts class
class Savings:Account
{
//
double interestRate;
//
public Savings()
{
// empty default constructor
}
//
// parameterized constructor, initializes the base class constructor as well
public Savings(String nm, int accnum, double bal, double rate):base(nm, accnum, bal)
{
//
interestRate = rate;
}
//
}
//
// Checkings class derived from Account class
class Checkings:Account
{
//
double overDraftLimit;
//
public Checkings()
{
// empty default constructor
}
//
/// <summary>
/// Parameterized constructor
/// </summary>
/// <param name="oDLimit">Over darft limit value</param>
public Checkings(String nm, int accnum, double bal, double rate, double oDLimit):base(nm, accnum, bal)
{
//
overDraftLimit = oDLimit;
}
//
}
//
/// <summary>
///
/// CreditCard class derived from Checkings class
/// </summary>
class CreditCard:Checkings
{
//
int cardNumber;
//
public CreditCard()
{
//empty default constructor
}
//
// parameterized constructor
public CreditCard(String nm, int accnum, double bal, double rate, double oDLimit, int num):base(nm, accnum, bal, rate, oDLimit)
{
//
cardNumber = num;
}
}
class Program
{
static void Main(string[] args)
{
//
Savings[] savingsAccounts = new Savings[3]; // declare an array of 3 Savings objects
//
Checkings[] checkingsAccounts = new Checkings[3]; // declare array of 3 Checkings objects
//
CreditCard[] cardAccounts = new CreditCard[3]; // declare array of 3 CreditCard objects
//
// must provide complete file path
String[] fnames = { "c:\Users\sambit\documents\visual studio 2015\Projects\bank\Savings.txt", "c:\Users\sambit\documents\visual studio 2015\Projects\bank\CreditCards.txt", "c:\Users\sambit\documents\visual studio 2015\Projects\bank\Checkings.txt" }; // create an array of all file names
//
String line; // this will store each line that is read
//
int i = 0; // this will be used to index the objects
//
// create a loop to iterate over each file name in the list above and read
foreach (String name in fnames)
{
//
i = 0;
//
StreamReader fread = new StreamReader(name); // create a stream reader object using a file and read data
//
line = fread.ReadLine();
//
String[] substrings;
//
// read till we reach the end of current file
while (line != null)
{
//
substrings = line.Split(':');
//
// verify the file type being currently read and store accordingly in one of the three arrays
if (name.Contains("Savings"))
{
//
savingsAccounts[i] = new Savings(substrings[0], Convert.ToInt32(substrings[1]), Convert.ToDouble(substrings[2]), Convert.ToInt32(substrings[3]));
//
i++;
}
//
if (name.Contains("Checkings"))
{
//
checkingsAccounts[i] = new Checkings(substrings[0], Convert.ToInt32(substrings[1]), Convert.ToDouble(substrings[2]), Convert.ToInt32(substrings[3]), Convert.ToDouble(substrings[4]));
//
i++;
}
//
if (name.Contains("CreditCards"))
{
// read each part of the substring into apporpriate parameters of the constructor
cardAccounts[i] = new CreditCard(substrings[0], Convert.ToInt32(substrings[1]), Convert.ToDouble(substrings[2]), Convert.ToInt32(substrings[3]), Convert.ToDouble(substrings[4]), Convert.ToInt32(substrings[5]));
//
i++;
}
//
line = fread.ReadLine();
}
//Console.WriteLine(line);
//
}
Explanation / Answer
Text formats for each file as follows:
1) Savings.txt(for each line in the file following is the format)
nameOfAccountHolder:accountNum:accountBalance:interestRate
Kumar:31445239501:10000:7.2
Peter:31545239501:1000:6
2) Checkings.txt(for each line in the file following is the format)
nameOfAccountHolder:accountNum:accountBalance:interestRate:overDraftLimit
Kumar:31445239501:10000.5:7.2:500.5
Peter:31545239501:1000.4:6:200:2
3) CreditCards.txt(for each line in the file following is the format)
nameOfAccountHolder:accountNum:accountBalance:interestRate:overDraftLimit:cardNumber
Kumar:31445239501:10000.5:7:500.5:31426501
Peter:31545239501:1000.4:6.5:200:2:31426501
Type of each variable as follows
nameOfAccountHolder-string
accountNum-integer
accountBalance-double
interestRate-double
overDraftLimit-double
cardNumber-integer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.