namespace Read_Write_Text_Account_By_Instructor
{
class Account
{
public string Firstname
{ get; set; }
public string Lastname
{ get; set; }
public int AccountNumber
{ get; set; }
public decimal Balance
{ get; set; }
public void Deposit(decimal amount)
{
Balance += amount;
}
public bool Withdrawal(decimal amount)
{
if (Balance >= amount)
{
Balance -= amount;
return true;
}
else
{
Balance -= 25; //fee
return false;
}
}
}
}
//==========================================================
namespace Read_Write_Text_Account_By_Instructor
{
public partial class Form1 : Form
{
List<Account> accountList = new List<Account>(16);
string accountFilePath = "accounts.txt";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//read the accounts file and populate accountList
//if the file exist return reads the file and populate accountList
if (File.Exists(accountFilePath))
{
StreamReader sr = null;
try
{
sr = File.OpenText(accountFilePath);
while (!sr.EndOfStream)
{
string firstname = sr.ReadLine();
string lastname = sr.ReadLine();
int accountNumber = int.Parse(sr.ReadLine());
decimal balance = decimal.Parse(sr.ReadLine());
//create an account number
Account a = new Account
{
Firstname = firstname,
Lastname = lastname,
AccountNumber = accountNumber,
Balance = balance
};
accountList.Add(a);
//call method to display the list to a Listview
DisplayAccounts();
}
}
catch (FormatException fe)
{
MessageBox.Show(fe.Message);
}
catch (ArgumentException ae)
{
MessageBox.Show(ae.Message);
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
}
finally
{
//3. close
if (sr != null)
sr.Close();
}
}
}
private void DisplayAccounts()
{
listView1.Items.Clear();
foreach (Account a in accountList)
{
ListViewItem lvi = new ListViewItem(a.Firstname);
lvi.SubItems.Add(a.Lastname);
lvi.SubItems.Add(a.AccountNumber.ToString());
lvi.SubItems.Add(a.Balance.ToString());
listView1.Items.Add(lvi);
}
}
private void btnOneTimeInitialize_Click(object sender, EventArgs e)
{
Account a = new Account
{
Firstname = "John",
Lastname = "Johnson",
AccountNumber = 123456789,
Balance = 2500
};
accountList.Add(a);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//you could just save the accountList to the accountsFilePath, or ask the user if s/he wants to save
DialogResult dr = MessageBox.Show("Do you want to save accounts?",
"Accounts Dialog",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
StreamWriter sw = null;
try
{
//clear / delete the file
if(File.Exists(accountFilePath))
Directory.Delete(accountFilePath);
sw = File.AppendText(accountFilePath);
foreach (Account a in accountList)
{
sw.WriteLine(a.Firstname);
sw.WriteLine(a.Lastname);
sw.WriteLine(a.AccountNumber);
sw.WriteLine(a.Balance);
}
}
catch (ArgumentException ae)
{
MessageBox.Show(ae.Message);
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
}
finally
{
//3. close
if (sw != null)
sw.Close();
}
}
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Filter = "txt files (*.txt)|*.txt|(*.cs)|*.cs";
//display it
DialogResult dr = fdlg.ShowDialog();
if (dr == DialogResult.OK)
{
//get the selected file and display in the
//textbox (txtFilePath)
txtFilePath.Text = fdlg.FileName;
}
}
private void btnDisplayAccountList_Click(object sender, EventArgs e)
{
DisplayAccounts();
}
private void btnCreateNSaveAccount_Click(object sender, EventArgs e)
{
StreamWriter sw = null;
try
{
//1. open file for appending
sw = File.AppendText(txtFilePath.Text);
//. Write to it
string firstName = txtFirstName.Text;
string lastName = txtLastName.Text;
int accountNumber;
decimal initialBalance;
if (firstName != string.Empty && lastName != string.Empty)
{
if (int.TryParse(txtAccountNumber.Text, out accountNumber) && decimal.TryParse(txtInitialDeposit.Text, out initialBalance))
{
//Account account = new Account(txtFirstName.Text, txtLastName.Text, int.Parse(txtAccountNumber.Text), decimal.Parse(txtInitialDeposit.Text));
//accountList.Add(account);
Account a = new Account();
accountList.Add(a);
sw.WriteLine(firstName);
sw.WriteLine(lastName);
sw.WriteLine(accountNumber);
sw.WriteLine(initialBalance);
MessageBox.Show("Successfully saved to file");
}
else
{
MessageBox.Show("Please input in the correct format");
}
}
}
catch (ArgumentException ae)
{
MessageBox.Show(ae.Message);
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
}
finally
{
//3. close
if (sw != null)
sw.Close();
}
}
private void btnRead_Click(object sender, EventArgs e)
{
StreamReader sr = null;
try
{
//1. Open file for reading
sr = File.OpenText(txtFilePath.Text);
//2. Read it
richTextBox1.Clear();
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
richTextBox1.AppendText(line + "\n");
}
}
catch (ArgumentException ae)
{
MessageBox.Show(ae.Message);
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
}
finally
{
//3. close
if (sr != null)
sr.Close();
}
}
}
}
//Ref: //msdn.microsoft.com/en-us/library/fxeahc5f(v=vs.110).aspx
//LAB
//In form1 Design a layout to allow a user
//to enter the Account Information and save to file
//Add listView then read account file and populate
//a List<Account> (accountList), as well as the listView
//Allow user to deposit and withdraw fraom a selected account
//You need a method: GetAccountByAccountNumber
//that takes an accountNumber and returns and Account object

No comments:
Post a Comment