Created
February 15, 2012 14:46
-
-
Save ChrisMoney/df182bb072c278d5fa90 to your computer and use it in GitHub Desktop.
C# --Savings Account Console Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SavingsAccount | |
using System; | |
namespace SimpleSavingsAccount | |
{ | |
// BankAccount – bank account which creates an account ID( which is assigned upon creation) | |
// and a balance | |
public class BankAccount // the base class | |
{ | |
// bank accounts start at 1000 and increase sequentially from there | |
public static int nNextAccountNumber = 1000; | |
// maintain the account number and balance for each object | |
public int nAccountNumber; | |
public decimal mBalance; | |
// Init – initialize a bank account with the next account ID and the specified initial balance | |
// default to zero | |
public void InitBankAccount() | |
{ | |
InitBankAccount(0); | |
} | |
public void InitBankAccount (decimal mInitialBalance) | |
{ | |
nAccountNumber = ++nNextAccountNumber; | |
mBalance = mInitialBalance; | |
} | |
// Balance Property | |
public decimal Balance | |
{ | |
get {return mBalance;} | |
} | |
// Deposit – any positive deposit is allowed | |
public void Depsosit (decimal mAmount) | |
{ | |
if (mAmount >0) | |
{ | |
mBalance += mAmount; | |
}} | |
// Withdraw – you can withdraw any amount up to the balance; return the amount withdrawn | |
public decimal Withdraw(decimal mWithdrawal) | |
{ | |
if (Balance <= mWithdrawal) // use Balance property | |
{ | |
mWithdrawal = Balance; | |
} | |
mBalance -= mWithdrawal; | |
return mWithdrawal; | |
} | |
// ToString – stringify the account | |
public string ToBankAccountString() | |
{ | |
return String.Format(“(0) – (1:C)”, | |
nAccountNumber, Balance); | |
}} | |
// SavingsAccount – a bank account that draws interest | |
public class SavingsAccount : BankAccount // the subclass | |
{ | |
public decimal mInterestRate; | |
// InitSavingsAccount – input the rate expressed as a rate between 0 and 100 | |
public void InitSavingsAccount(decimal mInterestRate) | |
{ | |
InitSavingsAccount(0, mInterestRate); | |
} | |
public void InitSavingsAccount (decimal mInitial, decimal mInterestRate) | |
{ | |
InitBankAccount (mInitial); | |
this.mInterestRate = mInterestRate/ 100; | |
} | |
// Accumulate Interest – invoke once per period | |
public void AccumulateInterest() | |
{ | |
mBalance = Balance + (decimal) (Balance * mInterestRate); | |
} | |
// ToString – stringify the account | |
public string ToSavingsAccountString() | |
{ | |
return String.Format(“{0} ({1}%), | |
ToBankAccountString(), mInterestRate *100); | |
}} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
// create a bank account and display it | |
BankAccount ba = new BankAccount(); | |
ba.InitBankAccount(100); | |
ba.Deposit(100); | |
Console.WriteLine(“Account {0}”, ba.ToBankAccountString()); | |
// now a savings account | |
SavingsAccount sa = new SavingsAccount(); | |
sa.InitSavingsAccount(100, 12.5m); | |
sa.AccumulateInterest(); | |
Console.WriteLine(“Account {0}”, sa.ToSavingsAccountString()); | |
// wait for user to acknowledge the results | |
Console.WriteLine(“Press Enter to terminate…); | |
Console.Read(); | |
}}} | |
Output: | |
Account 1001 - $200.00 | |
Account 1002 - $112.50 (12.500%) | |
Press Enter to terminate… | |
public class Program | |
{ | |
// Direct Deposit – deposit paycheck directly | |
//Direct deposit inherits BankAccounts | |
public static void Direct Deposit (BankAccount ba, decimal mPay { | |
ba.Deposit (mPay); | |
} | |
public static void Main(string[] args) | |
{ | |
// create a bank account and display it | |
BankAccount ba = new BankAccount(); | |
ba.InitBankAccount(100); | |
DirectDeposit (ba, 100); | |
Console.WriteLine(“Account (0)”, ba.ToBankAccountString()); | |
// now a savings account | |
SavingsAccount sa = new SavingsAccount(); | |
sa.InitSavingsAccount(12.5m); | |
DirectDeposit(sa, 100); | |
sa.AccumlateInterest(); | |
Console.WriteLine(“Account (0)”, sa.ToSavingsAccountString()); | |
// wait for user to acknowledge the results | |
Console.WriteLine(“Press Enter to terminate…”); | |
Console.Read(); | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment