Results 1 to 8 of 8
- 02-28-2013, 12:08 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 8
- Rep Power
- 0
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
- 02-28-2013, 12:42 AM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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.
- 02-28-2013, 02:06 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 8
- Rep Power
- 0
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
- 02-28-2013, 10:23 AM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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.
- 02-28-2013, 04:50 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 8
- Rep Power
- 0
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
- 02-28-2013, 06:04 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Project work help with loops and flags
You've told it to keep looping as long as the user enters a '1'.Java Code:while(choices ==1);
Please do not ask for code as refusal often offends.
- 02-28-2013, 09:08 PM #7
Member
- Join Date
- Feb 2013
- Posts
- 8
- Rep Power
- 0
Re: Project work help with loops and flags
do i need this in the code and if yes where should it finishif(choices ==1){
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
- 02-28-2013, 10:59 PM #8
Member
- Join Date
- Feb 2013
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Flags?
By radgator in forum New To JavaReplies: 3Last Post: 05-25-2011, 12:00 AM -
project runs in netbeans but .jar wont work
By Zeo7 in forum New To JavaReplies: 3Last Post: 12-16-2010, 04:21 PM -
ImageIcon doesnt work after exporting the project
By random7 in forum New To JavaReplies: 8Last Post: 07-27-2010, 09:00 PM -
Returning flags from enums
By willemien in forum New To JavaReplies: 5Last Post: 05-26-2010, 07:37 AM -
Looking for a project to work on - I am a noob tho
By Natrix in forum Jobs DiscussionReplies: 4Last Post: 07-16-2009, 03:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks