Results 1 to 16 of 16
  1. #1
    Mikedp is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Question Bothersome Program

    Hello, I have a program that doesnt want to work

    Specificaly, the EnterPayment method value for payment does not transfer to givechange method.

    I was wondering if someone could repair this program, as I have exhausted my non-internet sources. Please ignore how messy this program is.

    Java Code:
    package cashregister;
    import java.util.Scanner;
    
    /**
       This program tests the CashRegister class.
    */
    public class P411
    {
       public static void main(String[] args)
       {
          Scanner in = new Scanner(System.in);
          //final double NICKEL_VALUE = 0.05;
          //final double DIME_VALUE = 0.1;
         // final double QUARTER_VALUE = 0.25;
          int a = 0;
    
          CashRegister myregister = new CashRegister();
          myregister.recordPurchase(1.2);
          System.out.println("You just made a purchase of $1.2. How are you going to pay for it?");
    
          
             System.out.println("Enter coin name or F to finish");
             String input = in.next();
             
             Coin c = new Coin(input);
             
             //c.getValue();
          //double myChange = myregister.giveChange();
          
    
         
          System.out.println("Your change is " + c.myChange);
         
       }
    
    }



    Java Code:
    package cashregister;
    import java.util.Scanner;
    
    
     public class Coin
     {
    
        boolean done = false;              
       public Coin(String input) //Input = double coinCount, String coinType
       {
            CashRegister myregister = new CashRegister();
    
            Scanner in = new Scanner(System.in);
            if (input.equalsIgnoreCase("f"))
                done = true;
             else
             {
                double value = 0;
                if (input.equals("nickel"))
                   value = NICKEL_VALUE;
                else if (input.equals("dime"))
                   value = DIME_VALUE;
                else if (input.equals("quarter"))
                   value = QUARTER_VALUE;
                else if (input.equals("test"))
                   value = No_VALUE;
    
                if (value != 0)
                {
                   System.out.println("Enter the number of coins");
                   int n = in.nextInt(); // NUMBER OF COINS
                    // N/Value is working
                   System.out.println("");
                   myregister.enterPayment(n,(value));
               }
             }
                 
                  myChange = myregister.giveChange();
    
    
    
          //System.out.println("Your change is " + myChange);
       
       }
       
       public double getValue()
       {
           
         return output;  
       }        
    
     
      public double output = 0;
      public static final double QUARTER_VALUE = 0.25;
      public static final double DIME_VALUE = 0.10;
      public static final double NICKEL_VALUE = 0.05;
      public static final double PENNY_VALUE = 0.01;
      public static final double No_VALUE = 0.00;
      public double myChange;
     }
    Java Code:
    package cashregister;
    
    /**
     A cash register totals up sales and computes change due.
     */
     public class CashRegister
       {
          public double payment2;
          public double purchase;
          public double payment;
          public double change;
     /**
     Constructs a cash register with no money in it.
     */
    
    
     public CashRegister()
     {
     //purchase = 0;
     //payment = 0;
     }
    
     /**
     Records the purchase price of an item.
     @param amount the price of the purchased item
    */
     public void recordPurchase(double amount)
     {
         
        purchase += amount; //WORKING!
    
     }
    
    
     public void enterPayment(double number, double value)
     {
    
     payment = value * number; // WORKING!
     //System.out.println("TEST"+payment);
    
     }
    
     public double giveChange()
     {
    
     change = purchase - payment;
     if (payment == 0)
     {
        System.out.println("Payment IS NOT WORKING: GiveChange Method");
     }
    
     //purchase = 0;
    // payment = 0;
    
    
     return change;
     }
    
     //public void enterPayment(int coinCount)
     //{
    /// change = payment - purchase;
    // purchase = 0;
    // payment = 0;
    // }
    
     //------------------------------------------------------------------------------
      //------------------------------------------------------------------------------
      //------------------------------------------------------------------------------
      //------------------------------------------------------------------------------
      //------------------------------------------------------------------------------
      //------------------------------------------------------------------------------
    
    
    
     public static final double QUARTER_VALUE = 0.25;
     public static final double DIME_VALUE = 0.10;
     public static final double NICKEL_VALUE = 0.05;
     public static final double PENNY_VALUE = 0.01;
     }


    INPUT:

    run:
    You just made a purchase of $1.2. How are you going to pay for it?
    Enter coin name or F to finish
    quarter
    Enter the number of coins
    6

    Your change is -1.5
    BUILD SUCCESSFUL (total time: 12 seconds)

    EXPECTED OUTPUT: 0.25
    Last edited by Mikedp; 02-23-2010 at 03:47 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Repost your code using code tags and a proper indentation, that mess is unreadable.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Mikedp is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    Yes, Sorry, Fixing that asap

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Mikedp View Post
    Hello, I have a program that doesnt want to work
    Specificaly, the EnterPayment method value for payment does not transfer to givechange method.
    I was wondering if someone could repair this program, as I have exhausted my non-internet sources. Please ignore how messy this program is.
    Much better is for you to debug it yourself first by placing println statements throughout to poll the state of variables in various locations of the program in order to isolate the problem yourself. Then if you still can't fix it at least you'll be able to tell us where the problem is and at the same time will have learned some valuable debugging techniques.

    Also, regarding "messy" -- usually the "cleaner" your program, the more who will be willing to read it.

    Much luck!

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You've got two instances of CashRegister, one in P411 that has the amount of 1.2 and one in Coin that doesn't know anything about the purchase. Add a few outputs to see it:
    Java Code:
    public double giveChange() {
    	System.out.println(purchase);
    	System.out.println(payment);
    	change = purchase - payment;
    	System.out.println(change);
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Good pick up PhHein!

    Also on an unrelated note, I'm a little confused as to just what Coin is supposed to do. The way I figure it, it should encapsulate a physical coin and thus shouldn't have a Scanner object. In my way of thinking the cash register should do all the interacting and should perhaps hold coins -- or maybe not.... Could you post the assignment itself?

    Also, if it were my program, I'd get rid of all doubles and deal not in dollars but in cents. Then if I wanted to post a result in dollars, I could divide at the time of displaying the information.

  7. #7
    Mikedp is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Much better is for you to debug it yourself first by placing println statements throughout to poll the state of variables in various locations of the program in order to isolate the problem yourself. Then if you still can't fix it at least you'll be able to tell us where the problem is and at the same time will have learned some valuable debugging techniques.

    Also, regarding "messy" -- usually the "cleaner" your program, the more who will be willing to read it.

    Much luck!
    Yes, I have used Println to narrow the problem down to the GiveChange Method. The Payment value seems to have it's value as zero inside the GiveChange method.

    The assignment was to add coins to the register using a method (Coin Class supposedly was to hold the value of the various coins EX: Quarter Nickel Dime Ect Ect)

    I personaly believe the book I am using is causing more problems than it is helping with, sorry for the confusion.
    Last edited by Mikedp; 02-23-2010 at 05:20 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Mikedp View Post
    Yes, I have used Println to narrow the problem down to the GiveChange Method. The Payment value seems to have it's value as zero inside the GiveChange method.
    Good. As you can see PhHein has found your problem so hopefully you now know how to solve it.

    The assignment was to add coins to the register using a method (Coin Class supposedly was to hold the value of the various coins EX: Quarter Nickel Dime Ect Ect)

    I personaly believe the book I am using is causing more problems than it is helping with, sorry for the confusion.
    Yeah, I thought so. So you really don't want coin objects to interact with the user through Scanner or through any means. Rather all interaction should be in the cash register class.

    Also, if this were my app, I'd use an enum to encapsulate the notion of Coin. Are you allowed to do this?

  9. #9
    Mikedp is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    I can use anything, so yes

    Again, thanks to everyone in your support so far

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Then you might consider creating an enum called Coin that has four members (PENNY, NICKEL,...) and holds an int value (I still strongly believe you should do calculations in cents not dollars) and a String name field, that has a private constructor that takes an int and String for parameters and has getters for the two fields, .... And this should be it for Coin. The cash register object is where all the user interactions will be, where you'll ask the user which coin to use and how many of that coin, etc...

  11. #11
    Mikedp is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    I am a beginner, I really dont know how to make enums.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    They're somewhat similar to classes. The Sun tutorial on them is good, but you're probably right to keep this simple, just use numeric constants inside a Coin class.

  13. #13
    Mikedp is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    Pertaining to the GiveChange method, is there any simple way to rectify the disappearing payment value? I am several days behind on this program, and have little free time.

  14. #14
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Yep, remove the CashRegister instance from Coin and pass the reference from P411 into it's constructor. That's a bit ugly, but the easiest solution for now.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  15. #15
    Mikedp is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    Could you please edit my code with this in mind? I am utterly confused even at that supposedly simple correction (The Big Java book is a Big Annoyance) I could learn more by seeing the correction, as my Java Speak is horrible.
    Last edited by Mikedp; 02-24-2010 at 03:27 PM.

  16. #16
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Oh, I could easily do that, but this doesn't help you to learn. To give you a nudge, this should be the signature of the constructor:
    Java Code:
    public Coin(String input, CashRegister myregister)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  2. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 05:17 PM
  3. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

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