Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Converting from Groovy to Java (HashMap issue)

  • 12-04-2008 10:18AM
    #1
    Registered Users, Registered Users 2 Posts: 788 ✭✭✭


    Hi there,

    In our first year Computer Science course, we started off learning Groovy and recently have converted to Java. I attempted to convert a Groovy problem to Java, however I think I may not be writing the HashMaps correctly? I will post both my Groovy and Java codes for this problem. Hope somebody can help, really appreciated :)

    Groovy:
    class Checkout
    { 
    
    // Class Variables
    
    		static CUSTOMER = 0 // the number of recently created customers
    
    // Fields
    		
    		def customer // number of customers
    		def groceries = []  // list of groceries bought by customer
    		def food // map, containing barcode, product name and price   
    
    // Constructor
    
    		     def Checkout( )
    		//Create a 'Checkout' object.
    	
        	       {
            		CUSTOMER++
    
    		        this.customer = CUSTOMER
         	
            		food = [5431 : ["Milk 1L", 99], 2480: ["Flour 1KG", 265 ], 4017: ["Eggs 6PK", 180]]
         	       }
    
    
    
    // Methods
    
    		    def scan( barcode )
    		//A method to 'scan' and record the purchase of an item identified by 'barcode'.
    
                   {
     		    this.groceries.add(food [ barcode] ) 
                   }
    
    
     
    		  def printReceipt( )
    		//A method to 'print' a 'receipt' of all scanned items.
          {     
          		 def subTotal  = 0
           		def total = 0
           		def discount = 100
           		def product
           		def price = 0
           		def noDiscount = 000
    
          printf("     Welcome!%n" )
          printf("   Customer #%-4d %n", this.customer)
          printf("  Item        Price%n")
          printf("------------  -----%n")
    
     		for ( item in this.groceries )
    
               { 
                 
                 product = item.get(0)
                 price = item.get(1)
                 subTotal = subTotal + price
    
                  printf( "%-9s     %2d.%02d %n", product, price.intdiv(100), price % 100  )
    
               }
    
          printf("------------  -----%n")
          printf("SUB-TOTAL     %2d.%02d %n", subTotal.intdiv(100), subTotal % 100)
    		    
    	      if ( isFactor( 7, this.customer ) ) 
           {
              printf("Lucky-7      -%2d.%02d %n", discount.intdiv(100), discount % 100) 
      	      if  ( subTotal < discount )
                    total = 0.00
          	else 
                    total = subTotal - discount
           }
    
            else
     
           {
                 printf("Lucky-7      -%2d %02d %n", noDiscount.intdiv(100), noDiscount % 100 )
                    total = subTotal
           }
    
          printf("------------  -----%n")
          printf("T O T A L     %2d.%02d %n", total.intdiv(100), total % 100)
          printf("Thanks for Shopping!%n")
          printf("                    %n")
      
      
      
        }
      
    	        def isFactor( f, n )
    
    		// Is 'f' a factor of 'n'?
    
            {
     
    	        return ( n % f == 0 )
            
            }
    
    
    
    
    
    }
    

    Java:
    import java.util.ArrayList;
    import java.util.HashMap;
    
    class CheckOut
    
    {
                private static int CUSTOMER = 0;
    
    //Fields
    
    			private int customer;
    			private ArrayList<String> groceries = new ArrayList<String>();
    			private HashMap<String, Integer> food = new HashMap<String, Integer>();
    
    //Constructor
    
    				public CheckOut ()
    			{
    
    					CUSTOMER ++;
    				 this.customer = CUSTOMER;
    
    				map.put( 5431, "Milk 1L" 99 );
    				map.put( 2480, "Flour 1KG" 265 );
    				map.put( 4017, "Eggs 6PK" 180 );
    
    			}
    
    //Methods
     		
    			public void scan ( int barcode)
    	         {
      			this.groceries.add(food [ barcode] );
                     }
    
    
    
        		public void printReceipt()
    
    		{
         			  int subTotal  = 0;
         			  double total
         			  int discount = 100;
         			  String product;
         			  int price = 0;
       		          int noDiscount = 000;
    	
          System.out.printf("     Welcome!%n" );
          System.out.printf("   Customer #%-4d %n", this.customer);
          System.out.printf("  Item        Price%n");
          System.out.printf("------------  -----%n");
    
     		      for  ( int item : groceries )
    
    	             {
    
    	               product = item.get(0);
    	               price = item.get(1);
    	               subTotal = subTotal + price;
    
    	    System.out.printf( "%-9s     %2d.%02d %n", product, price / 100, price / 100  );
    
    	             }
    	             
    	   System.out.printf("------------  -----%n");
    	   System.out.printf("SUB-TOTAL     %2d.%02d %n", subTotal / 100, subTotal / 100);
    
    	        	if ( isFactor( 7, this.customer ) )
    	         {
    	
    	
    	System.out.printf("Lucky-7      -%2d.%02d %n", discount / 100, discount / 100);
    	        
    			        if  ( subTotal < discount )
    		                  total = 0.00;
    	        else
    				  total = subTotal - discount;
    	         }
    	         
    	      else
    	         
    	         {
    	               System.out.printf("Lucky-7      -%2d %02d %n", noDiscount / 100, noDiscount / 100 );
    	         		      total = subTotal;
    	         }
    
    	 
    	 System.out.printf("------------  -----%n");
    	 System.out.printf("T O T A L     %2d.%02d %n", total / 100, total / 100);
    	 System.out.printf("Thanks for Shopping!%n");
    	 System.out.printf("                    %n");
    
    	   }
    
    	   
    	   		private boolean isFactor( int f, int n )
    	  // Is 'f' a factor of 'n'?
    
    	          {
    
    	          return ( n % f == 0 );
    
    	          }
    
    
    
    
    
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    No you're not (using the map correctly), you're attempting to store both a product description and a price directly in the map which won't work.

    I'm presuming the 4 digit key is some form of barcode or product ID?

    2 options :

    - Use 2 hashmaps, both keyed on the barcode, the first storing product descriptions, the 2nd storing prices.

    OR

    - Create a simple class called Product which stores both a description and a price : the code then would look roughly like this:
    [PHP]
    private HashMap<String, Product> food = new HashMap<String, Product>();

    ...

    food.put("1111",new Product("Fish", 199));
    food.put("1112",new Product("Dogfood", 49));[/PHP]


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    pH wrote: »
    No you're not (using the map correctly), you're attempt the store both a product description and a price directly in the map which won't work.

    I'm presuming the 4 digit key is some form of barcode or product ID?

    2 options :

    - Use 2 hashmaps, both keyed on the barcode, the first storing product descriptions, the 2nd storing prices.

    OR

    - Create a simple class called Product which stores both a description and a price : the code then would look roughly like this:
    [PHP]
    private HashMap<String, Product> food = new HashMap<String, Product>();

    ...

    food.put("1111",new Product("Fish", 199));
    food.put("1112",new Product("Dogfood", 49));[/PHP]

    Thanks very much i'll give it a go :D


Advertisement