Results 1 to 8 of 8
  1. #1
    crowledg is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Project work help with loops and flags

    So doing a project for college and am not the best at java. the question we have to do is

    Create a system for a supermarket running a loyalty card scheme. Hard code 5 gifts that the supermarket offer and the points the gifts cost. These must be stored in arrays.
    Hard code the users for the system. Each user will have a name, pin and points. These must be stored in arrays.
    The user will be asked to log in. Once they log in they will have a menu with options to View Gifts, Buy Gifts, Add points and Exit. The menu should loop until the user chooses to Exit.
    When the user buys a gift the amount of points the gift cost must be deducted from the users points. The user will not be allowed to purchase a gift if the gift costs more points than they have. The user points held in the array must be updated. When the user adds points the points in the array must also be updated. Validation must be done on all inputs.

    So far i have done the login menu and enter password,, but came across a problem that when the password is entered correctly it does print the name and the points they have but also re print the login menu,, any one know what i'm doing wrong?? i thought the break would take me out of the loop

    import java.util.Scanner;
    public class newProject {

    public static void main(String args[]) {
    Scanner input = new Scanner (System.in);

    int giftpoints[]={2000,1200,700,200,1000};
    String gifts[]={"i-pad","beat headphones","bike","mp3 player","xbox 360"};
    int customerpoints[]={500,1200,300};
    String names[]={"James","Joe","Danny"};
    int pin[]={111,222,333};
    int choices=0;
    boolean flag=false;
    int sub=0, userpin;

    System.out.print("---- Welcome ----");

    while(choices !=2){
    System.out.println("\n\nPlease choose an option :\n\n1: Login \n2: Exit\n>");
    choices=input.nextInt();

    if (choices ==1){
    while(flag == false){
    //Take pin from the user
    System.out.print("Please enter Pin: ");
    userpin= input.nextInt();

    //go through the array to look for the user pin
    for(int i=0; i<pin.length; i++){
    if(userpin ==pin[i]){
    //hold onto the value of i
    flag=true;
    sub=i;
    break;

    }//end if user pin
    }//end for array

    if(flag ==false){
    System.out.print("Incorrect pin");
    }//end if


    }//end while flag is false

    System.out.println("Welcome : " + names[sub]);
    System.out.println("Points : " + customerpoints[sub]);


    }//end while choices



    }//end main method

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Project work help with loops and flags

    Hi crowledg, welcome.

    The problem you have is that within the while loop the condition is checked prior to executing the code. Try using a do-while loop instead.

    Also, before any of the mods start jumping up and down, please enclose all code within code tags.

    Guide For New Members

    Regards.

  3. #3
    crowledg is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Project work help with loops and flags

    Hey

    Not too sure how to do the do while, which while loop should i be replaceing

    Thanks

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Project work help with loops and flags

    The following tutorial should provide you with the information you need.
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Regfards.

  5. #5
    crowledg is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Project work help with loops and flags

    NNot sure if i'm doing the do while correctly, more than likely not but it wont let me choose option 1 when i run the program
    Java Code:
    import java.util.Scanner;
    public class projectdo {
    
        public static void main(String args[]) {
        	Scanner input = new Scanner (System.in);
    
        	int giftpoints[]={2000,1200,700,200,1000};
        	String gifts[]={"i-pad","beat headphones","bike","mp3 player","xbox 360"};
        	int customerpoints[]={500,1200,300};
        	String names[]={"James","Joe","Danny"};
        	int pin[]={111,222,333};
        	int choices=0;
        	boolean flag=false;
        	int sub=0, userpin;
    
        	System.out.print("---- Welcome ----");
    
        	do{
        		System.out.println("\n\nPlease choose an option :\n\n1: Login \n2: Exit\n>");
        		choices=input.nextInt();
        	}
        	while(choices ==1);
    
        	System.out.print("Please enter Pin: ");
        				userpin= input.nextInt();
    
        				for(int i=0; i<pin.length; i++){
        					if(userpin ==pin[i]){
        						//hold onto the value of i
        						flag=true;
        						sub=i;
        						break;
        					}//end if user
        				}//end for
    
        					if(flag ==false){
        					System.out.print("Incorrect pin");
        				}//end if
    
        	System.out.println("Welcome : " + names[sub]);
        	System.out.println("Points : " + customerpoints[sub]);
    
        }//end main
    
    
    }//end class

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Project work help with loops and flags

    Java Code:
    while(choices ==1);
    You've told it to keep looping as long as the user enters a '1'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    crowledg is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Project work help with loops and flags

    if(choices ==1){
    do i need this in the code and if yes where should it finish
    Java Code:
    import java.util.Scanner;
    public class projectdo {
    
        public static void main(String args[]) {
        	Scanner input = new Scanner (System.in);
    
        	int giftpoints[]={2000,1200,700,200,1000};
        	String gifts[]={"i-pad","beat headphones","bike","mp3 player","xbox 360"};
        	int customerpoints[]={500,1200,300};
        	String names[]={"James","Joe","Danny"};
        	int pin[]={111,222,333};
        	int choices=0;
        	boolean flag=false;
        	int sub=0, userpin;
    
        	System.out.print("---- Welcome ----");
    
        	do{
        		System.out.println("\n\nPlease choose an option :\n\n1: Login \n2: Exit\n>");
        		choices=input.nextInt();
        	}
        	while(choices !=1&& choices !=2);
    
        	if(choices ==1){
    
        	System.out.print("Please enter Pin: ");
        				userpin= input.nextInt();
    
        				for(int i=0; i<pin.length; i++){
        					if(userpin ==pin[i]){
        						//hold onto the value of i
        						flag=true;
        						sub=i;
        						break;
        					}//end if user
        				}//end for
    
        					if(flag ==false){
        					System.out.print("Incorrect pin");
    
        				}//end if
        	
    
        	System.out.println("Welcome : " + names[sub]);
        	System.out.println("Points : " + customerpoints[sub]);
    
        }//end main
    
    
    }//end class

  8. #8
    crowledg is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Project work help with loops and flags

    Its all good. figured it out finally :)

Similar Threads

  1. Flags?
    By radgator in forum New To Java
    Replies: 3
    Last Post: 05-25-2011, 01:00 AM
  2. project runs in netbeans but .jar wont work
    By Zeo7 in forum New To Java
    Replies: 3
    Last Post: 12-16-2010, 05:21 PM
  3. ImageIcon doesnt work after exporting the project
    By random7 in forum New To Java
    Replies: 8
    Last Post: 07-27-2010, 10:00 PM
  4. Returning flags from enums
    By willemien in forum New To Java
    Replies: 5
    Last Post: 05-26-2010, 08:37 AM
  5. Looking for a project to work on - I am a noob tho
    By Natrix in forum Jobs Discussion
    Replies: 4
    Last Post: 07-16-2009, 04:24 AM

Posting Permissions

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