Okay, I have an assignment for school. The program is a GUI that has two tabs. On the first tab there are four textboxes for name, id, age, and account balance. There is also a button on this tab that will add the account to a combobox on the second tab. On the second tab, there is the combobox and four textboxes for name, id, age, and balance. when i select a name from the combobox the four textboxes automatically fill in with their information. The problem i'm running into is that i have to have a withdraw and a deposit button that the user can enter an amount and have it either subtracted or added to the balance in the textbox. Does anyone know how to do this? If you would like to see the code let me know.
class BankAccount
{
//attributes
public string accountID;
public string customerName;
public int customerAge;
public double balance;
public const double DEFAULT_BALANCE = 500.00;
//construct
public BankAccount()
{
}
public BankAccount(string anID, string aName, int anAge, double aBalance)
{
accountID = anID;
customerName = aName;
customerAge = anAge;
balance = aBalance;
if (aBalance == 0)
{
balance = DEFAULT_BALANCE;
}
else
{
balance = aBalance;
}
}
public BankAccount(string anID, string aName, int anAge)
{
accountID = anID;
customerName = aName;
customerAge = anAge;
balance = DEFAULT_BALANCE;
}
//accessors
public void SetID(string anID)
{
accountID = anID;
}
public void SetName(string aName)
{
customerName = aName;
}
public void SetAge(int anAge)
{
customerAge = anAge;
}
public void SetBalance(double aBalance)
{
balance = aBalance;
}
public string GetID()
{
return accountID;
}
public string GetName()
{
return customerName;
}
public int GetAge()
{
return customerAge;
}
public double GetBalance()
{
return balance;
}
this is the form
public partial class Form1 : Form
{
//ArrayList account = new ArrayList();
private List<BankAccount> account = new List<BankAccount>();
public Form1()
{
InitializeComponent();
}
private void btnAddAccount_Click(object sender, EventArgs e)
{
BankAccount aBankAccount = new BankAccount(txtAccountID.Text, txtName.Text,
int.Parse(txtAge.Text), double.Parse(txtBalance.Text));
account.Add(aBankAccount);
AddToComboBox();
ClearText();
}
private void AddToComboBox()
{
cboAccount.Items.Clear();
foreach (BankAccount person in account)
{
cboAccount.Items.Add(person.GetName());
//cboAccount.Items.Add(person);
}
}
private void ClearText()
{
txtName.Clear();
txtAccountID.Clear();
txtBalance.Clear();
txtAge.Clear();
txtAccountID.Focus();
}
private void cboAccount_SelectedIndexChanged(object sender, EventArgs e)
{
//txtNameTab2.Text = cboAccount.SelectedItem.ToString();
txtNameTab2.Text = account[cboAccount.SelectedIndex].customerName;
txtAgeTab2.Text = account[cboAccount.SelectedIndex].customerAge.ToString();
txtAccountIDTab2.Text = account[cboAccount.SelectedIndex].accountID.ToString();
txtBalanceTab2.Text = account[cboAccount.SelectedIndex].balance.ToString();
}
private void btnWithdraw_Click(object sender, EventArgs e)
{
}
}
}
If you represent your Account with an object like the following:
public class Account{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Decimal Balance { get; set; }
}
Then consider that a Withdrawal will involve removing money from the Balance, and a Deposit will involve adding to the Balance. You might represent this the following way:
public class Account{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Decimal Balance { get; set; }
public Decimal DepositMoney(Decimal amount)
{
Balance+=amount;
return Balance;
}
public Decimal WithdrawMoney(Decimal amount)
{
Decimal moneyAfterWithdrawal = Balance-amount;
if(moneyAfterWithdrawal >= 0)
{
Balance = moneyAfterWithdrawal;
return Balance;
}
throw new Exception(String.Format("Withdrawing {0} would leave you overdrawn!", amount.ToString());
}
}
You could then call DepositMoney and WithdrawMoney as required, based on the value in the textbox and which button you clicked. Functions like Decimal.TryParse
will be useful in converting the input string to the required type, and you might also want to update the state of the balance on screen after the deposit or withdrawal.
In your example, you require a type of Double to represent the balance - simply replace Decimal with Double in my example.
EDIT:
Okay, let's take a look at one of your event handlers, the btnWithdraw_Click:
private void btnWithdraw_Click(object sender, EventArgs e)
{
//First, it is necessary to get the amount to be withdrawn
//Am assuming this is in a textbox, which I'll call txtAmount
double amount = 0;
//Check is a valid double
if(Double.TryParse(txtAmount.Text, out amount))
{
//Lets ignore negative amounts as they are technically a deposit ;-)
if(amount > 0)
{
BankAccount currentAccount = account[cboAccount.SelectedIndex];
double currentBalance = currentAccount.GetBalance();
double amountLeft = currentBalance - amount;
if(amountLeft >= 0)
{
currentAccount.SetBalance(amountLeft);
txtBalanceTab2.Text = amountLeft.ToString("D2");
}
else
{
//Warn user they would go overdrawn
}
}
}
}
Note, however, that I believe that the Withdraw and Deposit methods belong on the account itself as they change the state of the class, like in my Account example. The best place to keep this logic is with the class itself.
I leave Deposit as an exercise for the reader ;-)