| using System;
public class BankAccount {
protected double interestRate;
protected string owner;
protected decimal balance;
public BankAccount(string o, decimal b, double ir) {
this.interestRate = ir;
this.owner = o;
this.balance = b;
}
public BankAccount(string o, double ir):
this(o, 0.0M, ir) {
}
public virtual decimal Balance {
get {return balance;}
}
public virtual void Withdraw (decimal amount) {
balance -= amount;
}
public virtual void Deposit (decimal amount) {
balance += amount;
}
public virtual void AddInterests() {
balance += balance * (Decimal)interestRate;
}
public override string ToString() {
return owner + "'s account holds " +
+ balance + " kroner";
}
}
public class CheckAccount: BankAccount {
// Instance variables are inherited
public CheckAccount(string o, double ir):
base(o, 0.0M, ir) {
}
public CheckAccount(string o, decimal b, double ir):
base(o, b, ir) {
}
// Method Balance is inherited
// Method Deposit is inherited
// Method AddInterests is inherited
public override void Withdraw (decimal amount) {
base.Withdraw(amount);
if (amount < balance)
interestRate = -0.10;
}
public override string ToString() {
return owner + "'s check account holds " +
+ balance + " kroner";
}
}
public class SavingsAccount: BankAccount {
// Instance variables are inherited
public SavingsAccount(string o, double ir):
base(o, 0.0M, ir) {
}
public SavingsAccount(string o, decimal b, double ir):
base(o, b, ir) {
}
// Method Balance is inherited
// Method Deposit is inherited
public override void Withdraw (decimal amount) {
if (amount < balance)
base.Withdraw(amount);
else
throw new Exception("Cannot withdraw");
}
public override void AddInterests() {
balance = balance + balance * (decimal)interestRate
- 100.0M;
}
public override string ToString() {
return owner + "'s check account holds " +
+ balance + " kroner";
}
}
public class LotteryAccount: BankAccount {
// Instance variables are inherited
protected static Lottery lottery = Lottery.Instance(3);
public LotteryAccount(string o, decimal b):
base(o, b, 0.0) {
}
// Method Balance is inherited
// Method Deposit is inherited
// Method Withdraw is inherited
public override void AddInterests() {
int luckyNumber = lottery.DrawLotteryNumber;
balance = balance + lottery.AmountWon(luckyNumber);
}
public override string ToString() {
return owner + "'s lottery account holds " +
+ balance + " kroner";
}
}
public class Lottery{
private static Random rdm = new Random(unchecked((int)DateTime.Now.Ticks));
private int difficulty;
private readonly int winningNumber;
private readonly decimal amountWon;
private static Lottery uniqueInstance = null;
private Lottery(int difficulty){
this.difficulty = difficulty;
this.winningNumber = rdm.Next(difficulty);
this.amountWon = 500000.00M;
}
public static Lottery Instance(int difficulty){
if (uniqueInstance == null)
uniqueInstance = new Lottery(difficulty);
return uniqueInstance;
}
public int DrawLotteryNumber{
get {return rdm.Next(difficulty);}
}
public bool IsWinningNumber(int number){
return number == winningNumber;
}
public decimal AmountWon(int luckyNumber){
decimal res;
if (IsWinningNumber(luckyNumber))
res = amountWon;
else
res = 0.0M;
return res;
}
}
public class AccountClient{
public static void Main(){
BankAccount[] accounts =
new BankAccount[5]{
new CheckAccount("Per",1000.0M, 0.03),
new SavingsAccount("Poul",1000.0M, 0.07),
new CheckAccount("Kurt",10.0M, 0.02),
new LotteryAccount("Bent",10500.0M),
new LotteryAccount("Lone",11500.0M)
};
foreach(BankAccount ba in accounts){
ba.Deposit(1000.0M);
}
accounts[2].Withdraw(1050.0M); // Kurt's
foreach(BankAccount ba in accounts){
ba.AddInterests();
}
foreach(BankAccount ba in accounts){
Console.WriteLine("{0}", ba);
}
}
} |
|
| | All bank account classes - for exercise.
|