Tosker

Vending Machine Tutorial - Payment View Model

Nov 29th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. public class PaymentViewModel : ObservableObject
  2.     {
  3.         //Customer information
  4.         private double _total;
  5.         private double _inserted;
  6.         private double _change;
  7.  
  8.         //Machine information
  9.         private double _bankTotal;
  10.  
  11.         public double Total
  12.         {
  13.             get
  14.             {
  15.                 return _total;
  16.             }
  17.             set
  18.             {
  19.                 _total = value;
  20.                 OnPropertyChanged("Total");
  21.             }
  22.         }
  23.  
  24.         public double Inserted
  25.         {
  26.             get
  27.             {
  28.                 return _inserted;
  29.             }
  30.             set
  31.             {
  32.                 _inserted = value;
  33.                 OnPropertyChanged("Inserted");
  34.             }
  35.         }
  36.  
  37.         public double Change
  38.         {
  39.             get
  40.             {
  41.                 return _change;
  42.             }
  43.             set
  44.             {
  45.                 _change = value;
  46.                 OnPropertyChanged("Change");
  47.             }
  48.         }
  49.  
  50.         public double BankTotal
  51.         {
  52.             get
  53.             {
  54.                 return _bankTotal;
  55.             }
  56.             set
  57.             {
  58.                 _bankTotal = value;
  59.                 OnPropertyChanged("BankTotal");
  60.             }
  61.         }
  62.  
  63.         public PaymentViewModel()
  64.         {
  65.             Total = 0;
  66.             Inserted = 0;
  67.             Change = 0;
  68.             BankTotal = 0;
  69.         }
  70.  
  71.         //Insert monetary value
  72.         public void Insert(double value)
  73.         {
  74.             Inserted += value;
  75.         }
  76.  
  77.         //Set the total the requested item costs
  78.         public void SelectedPrice(double value)
  79.         {
  80.             Total = value;
  81.         }
  82.  
  83.         //Confirm the payment can be made
  84.         public bool Confirm()
  85.         {
  86.             if (Inserted >= Total)
  87.                 return true;
  88.  
  89.             return false;
  90.         }
  91.  
  92.         //Finalize payment
  93.         public void Pay()
  94.         {
  95.             Change = Total - Inserted;
  96.             BankTotal += Total;
  97.             Inserted = 0;
  98.             Total = 0;
  99.         }
  100.  
  101.         public void Collect()
  102.         {
  103.             Console.WriteLine("Collected Payments: $" + BankTotal);
  104.             BankTotal = 0;
  105.         }
  106.  
  107.     }
Add Comment
Please, Sign In to add comment