Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Count number of 'coin'p coins in a "machine"

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Count number of 'coin'p coins in a "machine"

    [SOLVED]

    Hello for my recent assignment I've been tasked to create a ticket machine which accepts payments in and allows you to cancel them. A recent extension to the assignment was to count how many of the pennies being added were a certain type of penny, e.g 10p, 20p, 50p.

    I managed to get the program to accurately count the number of 10p within the machine but when I tried with 20's I came to a dead end.

    Heres the code that calculates how many 10/20/50ps are in the machine:

    class ProcessMoney
    {
    int ticket;
    int cost = 0;
    int machine;
    int pence = 0;
    int calc = 0;
     
    public void setTicketPrice( int amount ) {
     
       ticket = amount;
     
    }
     
    public int getTicketPrice() { 
     
       return ticket;
     
        }
     
     
    public void add( int coin ) {
     
        cost = cost + coin;
     
        if ( coin == 10 ){
            calc = calc + 1;
        }
     
     
        } 
     
    public boolean enough() { return true; }
     
    public int getPaidSoFar() { return cost; }
     
      public void cancel() {
     
          cost = 0;
     
    } 
     
    public void bought() {
     
          machine = machine + cost;
          cost = 0;
     
        }
     
      public int moneyInMachine() { 
     
          return machine;
     
    }
     
    public int getCoins( int coin ) {
     
         if ( coin == 10 ){
         pence = machine * 100;
         calc = pence / 1000;
         }
         else if ( coin == 20 ){
             pence = machine * 100;
             calc = pence / 2000;
            }
     
         return calc;
        }
    }

    Heres the code for testing it:

    class Main
    { 
      private static ProcessMoney pm = new ProcessMoney();
     
      public static void main( String args[] )
      {
        int res = 0, expected = 100;
     
        test( "setTicketPrice() & getTicketPrice() ");
     
        pm.setTicketPrice( expected );
        res = pm.getTicketPrice();
        check( res == expected, 
              "Ticket price is %d should be %d", res, expected );
     
        expected = 200;
        pm.setTicketPrice( expected );
        res = pm.getTicketPrice();
        check( res == expected, 
              "Ticket price is %d should be %d", res, expected );
     
        test( "add() & getPaidSoFar()");
     
        pm.add( 10 ); pm.add( 20 ); pm.add( 30 );
        expected = 60;
        res = pm.getPaidSoFar();
        check( res == expected, 
              "Money entered into machine is %d should be %d", res, expected );
        pm.add( 20 ); pm.add( 40 ); pm.add( 40 );
        expected = 160;
        res = pm.getPaidSoFar();
        check( res == expected, 
              "Money entered into machine is %d should be %d", res, expected );
     
        test( "add() & cancel()");
     
        pm.add( 10 ); pm.add( 20 ); pm.add( 30 );
        expected = 0;
        pm.cancel();
        res = pm.getPaidSoFar();
        check( res == expected, 
              "money entered into machine is now %d should be 0", res );
     
        pm.add( 100 ); pm.add( 200 ); pm.add( 300 );
        expected = 0;
        pm.cancel();
        res = pm.getPaidSoFar();
        check( res == expected, 
              "money entered into machine is now %d should be 0", res );
     
        test( "enough()");
     
        pm.setTicketPrice( 200 );
        pm.add( 100 ); pm.add( 100 ); pm.add( 0 );
        expected = 200;
        check( pm.enough(), 
              "Enough money entered into machine 200 for 200 ticket" );
        pm.cancel();
     
        pm.setTicketPrice( 210 );
        pm.add( 100 ); pm.add( 100 ); pm.add( 20 );
        expected = 200;
        check( pm.enough(), 
              "Enough money entered into machine 220 for 210 ticket" );
        pm.cancel();
     
        test( "bought() & moneyInMachine()");
     
        pm.setTicketPrice( 200 );
        pm.add( 100 ); pm.add( 100 ); pm.add( 0 );
        if ( pm.enough() )
        {
          pm.bought();
        }
     
        expected = 200;
        res = pm.moneyInMachine();
        check( expected == res,
              "Total money in machine %d should be %d", res, expected );
        res = pm.getPaidSoFar();
        check( res == 0, 
              "Money for ticket in machine is %d should be 0", res );
        pm.cancel();
     
     
        pm.setTicketPrice( 200 );
        pm.add( 100 ); pm.add( 100 ); pm.add( 10 );
        if ( pm.enough() )
        {
          pm.bought();
        }
     
        expected = 410;
        res = pm.moneyInMachine();
        check( expected == res,
              "Total money in machine %d should be %d", res, expected );
        res = pm.getPaidSoFar();
        check( res == 0, 
              "Money for ticket in machine is %d should be 0", res );
     
        test("Count coins");
        pm = new ProcessMoney();
        checkRecord( 10, 2 );
        checkRecord( 20, 4 );
        checkRecord( 50, 3 );
        checkRecord( 100, 3 );
        checkRecord( 200, 2 );
     
        System.out.println( "Success" );
      }
     
      private static void checkRecord( int coin, int howMany )
      {
         pm.setTicketPrice( howMany * coin );
     
         for ( int i=1; i<=howMany*2; i++ )
         {
           pm.add( coin );
         }
         pm.cancel();
     
         for ( int i=1; i<=howMany; i++ )
         {
           pm.add( coin );
         }
     
         pm.bought();
         int actual = pm.getCoins( coin );
         check( howMany == actual,
                "Expected %d - %dp coins found %d - %dp coins", 
                 howMany, coin, actual, coin  );
      }
     
      private static String what = "";
     
      public static void check( boolean ok, String fmt, Object... params )
      {
        if ( ! ok )
        {
          System.out.println( what );
          System.out.print("ERROR: " );
          System.out.printf( fmt, params );
          System.out.println();
          System.exit(-1);
        }
      }
     
      public static void test( String str )
      {
        what = "Test: "  + str;
      }
     
    }

    For example when I run the program at the moment it runs perfectly for the 10ps however when I run it for 20ps I get these results back:

    Test: Count coins
    ERROR: Expected 4 - 20p coins found 5 - 20p coins
    Last edited by BITmixit; March 21st, 2012 at 08:10 AM. Reason: [SOLVED]


Similar Threads

  1. "\b" not "back spacing"?
    By frozen java in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 24th, 2011, 02:53 PM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM