I am having issues with the following part of my code.
when “nn” is entered i get invalid code.
when valid code is entered i get invalid code however this only happens once.
program doesn’t seem to work as intended. Please assist.
System.out.println("ENTER CODE (nn to Stop) : ");
ArrayList<Product> list = new ArrayList<Product>();
.
.
.
.
ArrayList<Code> codeList = new ArrayList<Code>();
for (Product product : list) {
System.out.print("CODE : ");
String pcode = scan.next();
if (pcode.equalsIgnoreCase("nn")) {
break;
}
if (!(code.equalsIgnoreCase(product.getCode()))) {
System.out.println("Invalid code, please enter valid code.");
System.out.print("CODE : ");
pcode = scan.next();
}
System.out.print("QUANTITY : ");
int quan = scan.nextInt();
while (quan > 20) {
System.out.println("Purchase of more than 20 items are not allowed, please enter lower amount.");
System.out.print("QUANTITY : ");
quan = scan.nextInt();
}
codeList.add(new Code(pcode, quan));
}
You want
continueinstead ofbreak.Also, you should only call
code = scan.next()once inside the loop; otherwise you’ll skip over some items.Update:
Now you just need to write the methods isExitCode(), isValidCode(), and isValidQuantity().