issue with a CashRegister
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: issue with a CashRegister

  1. #1
    Join Date
    Jul 2012
    Location
    USA
    Posts
    2

    issue with a CashRegister

    I am having a problem with getting this to work.
    does anyone have any ideas?

    here is the code:

    Code:
    import java.io.*;
    import java.math.*;
    import java.util.*;
    
    public class CashRegisterTester 
    {
        public static void main(String[] args) 
        {
            CashRegister register = new CashRegister();
            
            register.recordPurchase(0.75);
            register.recordPurchase(1.50);
            register.enterPayment(2,0,5,0,0);
            System.out.print("Change= ");
            System.out.println(register.giveChange());
        }
    
        public class CashRegister 
        {              
            public CashRegister() 
            {
                purchase = 0;
                payment = 0;
            }
            public void recordPurchase(double amount)
            {
                purchase = purchase + amount;
            }
            public void enterPayment(int dollars, int quarters, int dimes, int nickels, int pennies)
            {
                payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE 
                           + nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
            }
            public double giveChange()
            {
                double change = payment - purchase;
                purchase = 0;
                payment = 0;
                return change;
            }
            
            public static final double QUARTER_VALUE = 0.25;
            public static final double DIME_VALUE = 0.1;
            public static final double NICKEL_VALUE = 0.05;
            public static final double PENNY_VALUE = 0.01;
            
            private double purchase;
            private double payment;
        }
    }
    and the output:

    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable this cannot be referenced from a static context
    at CashRegisterTester.main(CashRegisterTester.java:13)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Jul 2012
    Location
    USA
    Posts
    2

    Re: issue with a CashRegister

    Nevermind about the previous code, I managed to figure that one out.
    This is the one i need help with.

    Code:
    import java.io.*;
    import java.math.*;
    import java.util.*;
    
    public class CashRegister 
    {
        public static void main(String[] args) 
        {
            CashRegister register = new CashRegister();
            
            register.recordPurchase(20.37);
            register.enterDollars(20);
            register.enterQuarters(2);
            System.out.print("Change= ");
            System.out.println(register.giveChange());
        }
                
            {
                purchase = 0;
                payment = 0;
            }
            public void recordPurchase(double amount)
            {
                purchase = purchase + amount;
            }
            public void enterPayment(int dollars, int quarters, int dimes, int nickels, int pennies)
            {
                payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE 
                           + nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
            }
            public double giveChange()
            {
                double change = payment - purchase;
                purchase = 0;
                payment = 0;
                return change;
            }
            
            public static final double QUARTER_VALUE = 0.25;
            public static final double DIME_VALUE = 0.1;
            public static final double NICKEL_VALUE = 0.05;
            public static final double PENNY_VALUE = 0.01;
            
            private double purchase;
            private double payment;    
    }

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center