Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    The singleton class Lottery - used by LotteryAccount.Lecture 15 - slide 10 : 13
Program 5
using System;

/// <summary> A class which assists LotteryAccount with the selection 
///           of lucky accounts. Encapsulates a secret winning number
///           and the amount won. A singleton class. </summary>
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;
  }

  /// <summary> Returns the unique instance of this class</summary>
  /// <param name = "difficulty"> A measure of the difficulty of winning. </param>
  public static Lottery Instance(int difficulty){
    if (uniqueInstance == null)
      uniqueInstance = new Lottery(difficulty);
    return uniqueInstance;
  }

  /// <value>Draw and return a lottery number </value>
  public int DrawLotteryNumber{
    get {return rdm.Next(difficulty);}
  }

  /// <summary> Return if n is equal to the winning number </summary>
  public bool WinningNumber(int n){
    return n == winningNumber;
  }

  /// <summary> 
  ///   Return the encapsulated amount won if
  ///   luckyNumber is the winning number.
  /// </summary>
  /// <param name = "luckyNumber">Some integer number </param>
  public decimal AmountWon(int luckyNumber){
    decimal res;
    if (WinningNumber(luckyNumber))
       res = amountWon;
    else
       res = 0.0M;
    return res;
  }
}