Guest User

Untitled

a guest
Aug 14th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. Dealing with non-inherited methods in a subclass
  2. public abstract class Voucher
  3. {
  4. public int Id { get; set; }
  5. public decimal Value { get; protected set; }
  6. public const string SuccessMessage = "Applied";
  7. }
  8.  
  9. public class GiftVoucher : Voucher
  10. {
  11. }
  12.  
  13. public class DiscountVoucher : Voucher
  14. {
  15. public decimal Threshold { get; private set; }
  16. public string FailureMessage { get { return "Please spend £{0} to use this discount"; } }
  17. }
  18.  
  19. if (voucher is DiscountVoucher)
  20. {
  21. // cast voucher to a DiscountVoucher and then call the specific methods on it
  22. }
  23.  
  24. public virtual string FailureMessage { get { return string.Empty; } }
  25.  
  26. public abstract class Voucher
  27. {
  28. public int Id { get; set; }
  29. public decimal Value { get; protected set; }
  30. public const string SuccessMessage = "Applied";
  31. public decimal Threshold { get { return 0.0; } }
  32. public string FailureMessage { get { return ""; } }
  33. }
  34.  
  35. public abstract class Voucher
  36. {
  37. public bool isValid(ShoppingCart sc);
  38. public string FailureMessage { get { return "This voucher does not apply"; } }
  39. // ...
  40. }
  41.  
  42. public class DiscountVoucher : Voucher
  43. {
  44. private decimal Threshold;
  45. public override bool isValid(ShoppingCart sc)
  46. {
  47. return (sc.total >= Threshold);
  48. }
  49. public override string FailureMessage
  50. {
  51. get { return FormatString("Please spend £{0} to use this discount", Threshold); }
  52. }
  53.  
  54. public abstract class Voucher
  55. {
  56. public int Id { get; set; }
  57. public decimal Value { get; protected set; }
  58. public virtual string SuccessMessage { get { return "Applied"; } }
  59. public virtual string FailureMessage { get { return String.Empty; } }
  60. public virtual bool Ok { get { return true; } }
  61. }
  62.  
  63. public class GiftVoucher : Voucher { }
  64.  
  65. public class DiscountVoucher : Voucher
  66. {
  67. public decimal Threshold { get; private set; }
  68. public override string FailureMessage { get { return "Please spend £{0} to use this discount"; } }
  69. public override bool Ok { get { return Value >= Threshold; } }
  70. }
  71.  
  72. if (voucher.Ok) {
  73. Console.WriteLine(voucher.SuccessMessage);
  74. } else {
  75. Console.WriteLine(voucher.FailureMessage);
  76. }
Add Comment
Please, Sign In to add comment