Looping with various conditions

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1007 Views - Last Post: 14 February 2012 - 08:23 PM Rate Topic: -----

#16 xiphias  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Looping with various conditions

Posted 14 February 2012 - 03:20 PM

View PostSheph, on 14 February 2012 - 02:08 PM, said:

public class ProductList extends ArrayList<Product> {
    public boolean codeExist(int code) {
        for(Product p : this) {
            if(p.getCode() == code)
                return true;
        }
        return false;
    }
}
Then you can create a new ProductList() wherever you need it, and call productList.codeExists(code) wherever you want to check that a code already exists in the list.


I didn't want to create new class altogether, i wanted to include the methods in my Item class along with the setter/ getter methods. I'm a beginner, i think i'll put the methods in main.

What i'm don't quite understand is

boolean codeExist(int code, [b]List<Product> prods)[/b] {
   for(Product p: prods) {

            if(p.getCode() == code)

                   return true;

     }

     return false;



Are we creating a new list prods specifically to test codes?
Was This Post Helpful? 0
  • +
  • -

#17 xiphias  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Looping with various conditions

Posted 14 February 2012 - 06:24 PM

View PostSheph, on 14 February 2012 - 02:08 PM, said:

public class ProductList extends ArrayList<Product> {
    public boolean codeExist(int code) {
        for(Product p : this) {
            if(p.getCode() == code)
                return true;
        }
        return false;
    }
}
Then you can create a new ProductList() wherever you need it, and call productList.codeExists(code) wherever you want to check that a code already exists in the list.


I'm almost there, sorry for asking dumb questions.
Is it ok to do
 if (p.getCode().equalsIgnoreCase(code))


Also i seem to always be receiving "invalid Code"
here is my code

import java.util.ArrayList;



public class ProductList extends ArrayList<Product> {
	
    public boolean isValidCode(String code) {
        for(Product prod : this) {
            if(prod.getCode().equalsIgnoreCase(code))
                return true;
        }
        return false;
    }
    
    boolean isExitCode( String code){
    	boolean exit = false;
    	if (code.equalsIgnoreCase("xx")){
    		exit = true;
    	}
    	return exit;
    }
}



ProductList productList = new ProductList();  
		        while (true) {
		        	System.out.print("CODE : ");
		            final String code = scan.next();
		            if (productList.isExitCode(code)) {
		                break;
		            }
		            if (!(productList.isValidCode(code))) {
		                System.out.println("Invalid code");
		                continue;
		            }

Was This Post Helpful? 0
  • +
  • -

#18 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,186
  • Joined: 05-May 05

Re: Looping with various conditions

Posted 14 February 2012 - 06:30 PM

Quote

Also i seem to always be receiving "invalid Code"
here is my code


What you've posted is not enough to tell why. That code looks good. How do we know what kind of input your feeding it? Are you Product instances correct? Post a runnable program with your console input listed.
Was This Post Helpful? 0
  • +
  • -

#19 xiphias  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Looping with various conditions

Posted 14 February 2012 - 07:13 PM

import java.util.*;

public class PurchaseItems {

    public static void main(String[] args)
    {

        //1. Creates list of items for customer and displays items available for purchase
        ArrayList<Item> list = new ArrayList<Item>();        
        list.add(new Item("D24", "Wood glue, clear, 1 litre", 2100, 5.5f));          
        list.add(new Item("M95", "Sandpaper, rough grade, 100 sheets", 245, 8.5f));
        list.add(new Item("M94", "Sandpaper, fine grade, 100 sheets", 200, 14.75f));
        list.add(new Item("M93", "Sandpaper, medium grade, 100 sheets", 200, 10.25f));
        list.add(new Item("N70", "Nails, steel, 70mm, pack of 50", 1500, 12.5f));
        list.add(new Item("K16", "Wood screws, brass, 20mm, pack of 50", 525, 7.75f));
        list.add(new Item("B12", "Hex Bolts, steel, 125mm, pack of 10", 1675, 20.55f));
        list.add(new Item("S25", "Hex Screws, steel, 25mm, pack of 50", 1425, 10.75f));        
        list.add(new Item("W25", "Wall Plugs, plastic, 25mm, pack of 50", 675, 11.75f));
        
        .
        .//Display Items
        .
        .
        
        boolean start = true;	
	        while (start){
			Scanner scan = new Scanner(System.in);	        
			System.out.println("PLACE YOUR ORDER!");
			System.out.print("Name : ");
			String name = scan.nextLine();
			System.out.print("ADDRESS-1 : ");
			String addr1 = scan.nextLine();
			System.out.print("ADDRESS-2 : ");
			String addr2 = scan.nextLine();
			System.out.print("ADDRESS-3 : ");
			String addr3 = scan.nextLine();
			System.out.print("POST CODE : ");
			String post_code = scan.nextLine();
			System.out.println();
			System.out.println("ENTER CODE (XX to Stop) : ");
			ArrayList<Code> cl = new ArrayList<Code>();
			//3. Allows customer to enter code and quantity of items



			ItemList itemList = new ItemList();  
			while (true) {
				System.out.print("CODE : ");
			    final String code = scan.next();
			    if (itemList.isExitCode(code)) {
				break;
			    }
			    if (!(itemList.isValidCode(code))) {
				System.out.println("Invalid code, please enter valid code.");
				continue;
			    }
			    int quan = -1;
			    while (true) {
				System.out.print("QUANTITY : ");
				quan = scan.nextInt();
				if (!(Code.isValidQuantity(quan))) {
				    System.out.println("Purchase of more than 100 items are not allowed, please enter lower amount.");
				    continue;
				}
				break;
			    }



				cl.add(new Code(code, quan));
		}
		.
		.//Print Receipt
		.
		.
		System.out.printf("Would you like to place another order [y/n] :");
					String order = ask.nextLine();
					if (order.equalsIgnoreCase("n")){
						start = false;
					}
        }
		        



Console:
HARDWARE ITEMS

CODE DESCRIPTION UNIT PRICE

D24 Wood glue, clear, 1 litre, 2100g $ 5.50
M95 Sandpaper, rough grade, 100 sheets, 245g $ 8.50
M94 Sandpaper, fine grade, 100 sheets, 200g $14.75
M93 Sandpaper, medium grade, 100 sheets, 200g $10.25
N70 Nails, steel, 70mm, pack of 50, 1500g $12.50
K16 Wood screws, brass, 20mm, pack of 50, 525g $ 7.75
B12 Hex Bolts, steel, 125mm, pack of 10, 1675g $20.55
S25 Hex Screws, steel, 25mm, pack of 50, 1425g $10.75
W25 Wall Plugs, plastic, 25mm, pack of 50, 675g $11.75

PLACE YOUR ORDER!
Name : x
ADDRESS-1 : xx
ADDRESS-2 : xxx
ADDRESS-3 : xxxx
POST CODE : xxxxx

ENTER CODE (XX to Stop) :
CODE : k16
Invalid code, please enter valid code.
CODE : d24
Invalid code, please enter valid code.
CODE : W25
Invalid code, please enter valid code.
CODE : xx

INVOICE FOR ORDER

x
xx
xxx
xxxx POST CODE : xxxxx

TOTAL : $ 0.00
Shipping : 0 items, 0 0.00
TOTAL INCL. SHIPPING: $ 0.00
VAT at 20% $ 0.00
TOTAL TO PAY : $ 0.00
=================================================================================================
Would you like to place another order [y/n] :
Was This Post Helpful? 0
  • +
  • -

#20 xiphias  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Looping with various conditions

Posted 14 February 2012 - 07:29 PM

Sorry for posting such a long post. Can you edit?
Seems i am unable to.
Was This Post Helpful? 0
  • +
  • -

#21 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,186
  • Joined: 05-May 05

Re: Looping with various conditions

Posted 14 February 2012 - 07:31 PM

Can post a SSCCE? Please remove all prompt stuff other than the entering of the product codes. Add only one or two items to the list. Remove all code in the while loop that doesn't pertain to checking a product code. This is what I mean.

public static void main(String[] args) {

        ArrayList<Item> list = new ArrayList<Item>();
        list.add(new Item("D24", "Wood glue, clear, 1 litre", 2100, 5.5f));
        list.add(new Item("M95", "Sandpaper, rough grade, 100 sheets", 245,
                8.5f));

        System.out.println("ENTER CODE (XX to Stop) : ");
        ItemList itemList = new ItemList();
        while (true) {
            System.out.print("CODE : ");
            final String code = scan.next();
            if (itemList.isExitCode(code)) {
                break;
            }
            if (!(itemList.isValidCode(code))) {
                System.out.println("Invalid code, please enter valid code.");
                continue;
            }

        }

    }



And include whatever classes are needed to make it compile. Then we can easily find your error.

Now that I acutally look at your code. Where have you even added anything to the item list? I see items being added here

 ArrayList<Item> list = new ArrayList<Item>();        
        list.add(new Item("D24", "Wood glue, clear, 1 litre", 2100, 5.5f));          



but that's not an item list. And I see you've created an item list here:

ItemList itemList = new ItemList(); 


Yet, you haven't added anything. Then you call isValidCode on it.

  if (!(itemList.isValidCode(code))) {


This post has been edited by blackcompe: 14 February 2012 - 07:32 PM

Was This Post Helpful? 1
  • +
  • -

#22 Sheph  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 427
  • View blog
  • Posts: 998
  • Joined: 12-October 11

Re: Looping with various conditions

Posted 14 February 2012 - 07:32 PM

Your ItemList and your ArrayList<Item> are 2 different things. First you put everything into ArrayList<Item> at the top. Then you create a new ItemList() that has nothing in it. Then you check the codes based on the itemList with nothing in it.
Was This Post Helpful? 2
  • +
  • -

#23 xiphias  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Looping with various conditions

Posted 14 February 2012 - 07:55 PM

haaaaaa that was dumb of me. I've been messing around with objects names for the purpose of posting.. Right now i'm kind of worried that my entire assignment is on the web. Are you guys able to do some form of editing for me. That way the teacher won't say i copied it from the web..lol.

@blackcompe & @Sheph

I thank you guys for your help.
Was This Post Helpful? 0
  • +
  • -

#24 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,186
  • Joined: 05-May 05

Re: Looping with various conditions

Posted 14 February 2012 - 08:01 PM

Quote

That way the teacher won't say i copied it from the web..lol.


I highly doubt your teaching is perusing DIC looking assignments. Even if (s)he did, you could just show him you have access to the account that made the posts. There are tons of assignments on here.

You can edit your posts. There's a button right below your post.

This post has been edited by blackcompe: 14 February 2012 - 08:04 PM

Was This Post Helpful? 0
  • +
  • -

#25 xiphias  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Looping with various conditions

Posted 14 February 2012 - 08:18 PM

I can't edit
New D.I.C. User Rules

But it's all good man, thanks.
Was This Post Helpful? 1
  • +
  • -

#26 Sheph  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 427
  • View blog
  • Posts: 998
  • Joined: 12-October 11

Re: Looping with various conditions

Posted 14 February 2012 - 08:23 PM

Wow someone actually read the rules?! Amazing!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2