Advertisement
R8934

Javascript oop cashregister

Aug 2nd, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function StaffMember(name,discountPercent){
  2.     this.name = name;
  3.     this.discountPercent = discountPercent;
  4. }
  5.  
  6. var sally = new StaffMember("Sally", 5);
  7. var bob = new StaffMember("Bob", 10);
  8.  
  9. // Create yourself again as 'me' with a staff discount of 20%
  10. var me = new StaffMember("Eleison", 20);
  11.  
  12. var cashRegister = {
  13.     total:0,
  14.     lastTransactionAmount: 0,
  15.     add: function(itemCost){
  16.         this.total += (itemCost || 0);
  17.         this.lastTransactionAmount = itemCost;
  18.     },
  19.     scan: function(item,quantity){
  20.         switch (item){
  21.         case "eggs": this.add(0.98 * quantity); break;
  22.         case "milk": this.add(1.23 * quantity); break;
  23.         case "magazine": this.add(4.99 * quantity); break;
  24.         case "chocolate": this.add(0.45 * quantity); break;
  25.         }
  26.         return true;
  27.     },
  28.     voidLastTransaction : function(){
  29.         this.total -= this.lastTransactionAmount;
  30.         this.lastTransactionAmount = 0;
  31.     },
  32.     // Create a new method applyStaffDiscount here
  33.     applyStaffDiscount: function(employee) {
  34.        switch (employee) {
  35.            case "sally":
  36.                this.name = sally.name;
  37.                this.total = this.total - (this.total * (sally.discountPercent / 100));
  38.                break;
  39.             case "bob":
  40.                 this.name = bob.name;
  41.                 this.total = this.total - (this.total * (bob.discountPercent / 100));
  42.                 break;
  43.             case "me":
  44.                 this.name = me.name;
  45.                 this.total = this.total - (this.total * (me.discountPercent / 100));
  46.                 break;
  47.        }
  48.        return true;
  49.     }
  50. };
  51.  
  52. cashRegister.scan('eggs',1);
  53. cashRegister.scan('milk',1);
  54. cashRegister.scan('magazine',3);
  55. // Apply your staff discount by passing the 'me' object
  56. // to applyStaffDiscount
  57. cashRegister.applyStaffDiscount("me");
  58.  
  59. // Show the total bill
  60. console.log('Your bill is '+cashRegister.total.toFixed(2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement