Tuesday, June 17, 2014
Read Write Text Account.txt_Lab
namespace Read_Write_Txt_Lab_With_Account
{
public class Account
{
//Private Fields
private string _firstName;
private string _lastName;
private string _accountNumber;
private decimal _balance;
//Constructor
public Account(string firstName, string lastName, string accountNumber, decimal balance)
{
_firstName = firstName;
_lastName = lastName;
_accountNumber = accountNumber;
_balance = balance;
}
//Properties
public string firstName
{ get { return _firstName; } }
public string lastName
{ get { return _lastName; } }
public string AccountNumber
{ get { return _accountNumber; } }
public decimal Balance
{ get { return _balance; } }
public void Deposit(decimal amt)
{
_balance += amt;
}
public void Withdrawal(decimal amt)
{
_balance -= amt;
}
}
}
//==================================================
using System.IO; //Add to work with fileSystem
namespace Read_Write_Txt_Lab_With_Account
{
public partial class Form1 : Form
{
List<Account> accountList = new List<Account>();
public Form1()
{
InitializeComponent();
}
private Account GetAccountById(string accountID)
{
listView1.Items.Clear();
foreach (Account account in accountList)
{
if (account.AccountNumber == accountID)
return account;
}
return null;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
//create an open file dialog
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 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;
string accountNumber = txtAccountNumber.Text;
decimal initialBalance;
if (firstName != string.Empty && lastName != string.Empty && accountNumber != string.Empty)
{
if (decimal.TryParse(txtInitialDeposit.Text, out initialBalance))
{
Account account = new Account(txtFirstName.Text, txtLastName.Text, txtAccountNumber.Text, decimal.Parse(txtInitialDeposit.Text));
accountList.Add(account);
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 btnDisplayAccountList_Click(object sender, EventArgs e)
{
DisplayAccounts();
}
private void DisplayAccounts()
{
listView1.Items.Clear();
foreach (Account account in accountList)
{
ListViewItem lvi = new ListViewItem(account.firstName);
lvi.SubItems.Add(account.lastName);
lvi.SubItems.Add(account.AccountNumber);
lvi.SubItems.Add(account.Balance.ToString("c"));
listView1.Items.Add(lvi);
}
}
}
}
Labels:
C#
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment