Here are all the classes in my solution: The following sample output from the program illustrates the case where difficulty in Lottery is 3 (one
out of three wins):using System;
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 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 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 LotteryPlusAccount: LotteryAccount {
public LotteryPlusAccount(string owner, decimal balance):
base(owner,balance){
}
public override void Deposit(decimal amount){
int luckyNumber = lottery.DrawLotteryNumber;
if (lottery.IsWinningNumber(luckyNumber))
base.Deposit(amount*2);
else base.Deposit(amount-15);
}
public override void Withdraw(decimal amount){
int luckyNumber = lottery.DrawLotteryNumber;
if (lottery.IsWinningNumber(luckyNumber))
base.Withdraw(0);
else base.Withdraw(amount + 50);
}
}
public class AccountClient{
public static void Main(){
BankAccount accNormal = new BankAccount("Maria", 1000, 0.1),
accPlus = new LotteryPlusAccount("Maria", 1000);
for(int i = 1; i < 20; i++){
accPlus.Deposit(100);
Console.WriteLine("{0}", accPlus);
accPlus.Withdraw(100);
Console.WriteLine("{0}\n", accPlus);
}
}
}
Maria's lottery account holds 1200 kroner
Maria's lottery account holds 1050 kroner
Maria's lottery account holds 1135 kroner
Maria's lottery account holds 985 kroner
Maria's lottery account holds 1185 kroner
Maria's lottery account holds 1035 kroner
Maria's lottery account holds 1120 kroner
Maria's lottery account holds 970 kroner
Maria's lottery account holds 1170 kroner
Maria's lottery account holds 1020 kroner
Maria's lottery account holds 1105 kroner
Maria's lottery account holds 955 kroner
Maria's lottery account holds 1040 kroner
Maria's lottery account holds 890 kroner
Maria's lottery account holds 975 kroner
Maria's lottery account holds 825 kroner
Maria's lottery account holds 1025 kroner
Maria's lottery account holds 875 kroner
Maria's lottery account holds 960 kroner
Maria's lottery account holds 810 kroner
Maria's lottery account holds 895 kroner
Maria's lottery account holds 895 kroner
Maria's lottery account holds 1095 kroner
Maria's lottery account holds 1095 kroner
Maria's lottery account holds 1295 kroner
Maria's lottery account holds 1145 kroner
Maria's lottery account holds 1230 kroner
Maria's lottery account holds 1080 kroner
Maria's lottery account holds 1165 kroner
Maria's lottery account holds 1015 kroner
Maria's lottery account holds 1215 kroner
Maria's lottery account holds 1065 kroner
Maria's lottery account holds 1150 kroner
Maria's lottery account holds 1000 kroner
Maria's lottery account holds 1200 kroner
Maria's lottery account holds 1200 kroner
Maria's lottery account holds 1400 kroner
Maria's lottery account holds 1400 kroner