Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java script help (cash register)

    I need to do a cash register for a school assignment and i am currently able to make it give out the total price and take off a percentage discount.
    I need help to make the register also account for location charges and discounts on location charges.

    I have tried to work it out but i keep getting errors, any help would be appreciated.

    Here is my current code
    Code:
    function Discount(name,discountPercent){
        this.name = name;
        this.discountPercent = discountPercent;
    }
    
    var discount = new Discount("Discount",5);
    var bigDiscount = new Discount("BIGDiscount",10);
    
    
    var cashRegister = {
        total:0,
        lastTransactionAmount: 0,
        add: function(itemCost){
            this.total += (itemCost || 0);
            this.lastTransactionAmount = itemCost;
        },
        scan: function(item,quantity){
            switch (item){
            case "P01": this.add(16.50 * quantity); break;
            case "P02": this.add(14.75 * quantity); break;
            case "P03": this.add(14.75 * quantity); break;
            case "P04": this.add(19.00 * quantity); break;
    		case "P05": this.add(12.35 * quantity); break;
    		case "P06": this.add(13.00 * quantity); break;
    		case "P07": this.add(18.50 * quantity); break;
    		case "P08": this.add(21.00 * quantity); break;
    		case "P09": this.add(15.65 * quantity); break;
    		case "P10": this.add(11.00 * quantity); break;
            }
            return true;
        },
        voidLastTransaction : function(){
            this.total -= this.lastTransactionAmount;
            this.lastTransactionAmount = 0;
        },
        applyDiscount : function(discount){
    this.total -= this.total*(discount.discountPercent / 100);
    }
        };
    
    cashRegister.scan('P01',1);
    cashRegister.scan('P04',1);
    cashRegister.scan('P10',3);
    
    cashRegister.applyDiscount(discount);
    
    console.log('Your bill is $'+cashRegister.total.toFixed(2));

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    var discount = new Discount("Discount",5);
    var bigDiscount = new Discount("BIGDiscount",10);

    That is not Javascript syntax.

    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 343 Times in 339 Posts
    @Philipp: looks perfectly like JS to me. and object creation via new is still valid JS (though not always the best choice).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by Dormilich View Post
    @Philipp: looks perfectly like JS to me. and object creation via new is still valid JS (though not always the best choice).
    Oh, you tell me something! I guess I have not used object creation with new.

    It would help if the OP indicated the errors he is getting.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 343 Times in 339 Posts
    without error messages there is hardly anything to say.

    besides that, some notes regarding the code:

    - I find it quite odd to use an object for a mere discount value, esp. since you can’t seemingly do more with it than with the bare value.

    - scan() contains a lot of repetitive code. a simple "config" would make that extensible and easier to manage:
    PHP Code:
    var cashRegister = {
      
    // …
      
    prices: {
        
    P0116.50,
        
    P0214.75,
        
    // …
      
    },
      
    scan: function (itemquantity) {
        
    // explicitly naming the Singleton instance saves you from
        // surprises by context mutation ("this" is not as fixed as you may think)
        
    if (!cashRegister.prices.hasOwnProperty(item)) {
          
    // throw new Error("There is no such Item " + item);
          
    return cashRegister.total;
        }
        
    quantity quantity || 0;
        
    cashRegister.add(cashRegister.prices[item] * quantity);
        
    // always returning true doesn’t tell you anything
        
    return cashRegister.total;
      }
    }; 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •