Results 1 to 3 of 3
- 03-18-2013, 09:08 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 8
- Rep Power
- 0
Suggestion on how to improve the program
Java Code:/**A Program for a supermarket running a loyalty card scheme.Hard code 5 gifts that the supermarket offer and the points the gifts cost. *Hard code the users for the system. Each will have a pin and points stored in the array. *Be 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. *When the user buy gifts the amount of points must be added on to the user points. *Valadations must be done. * @(#)project2.java * * * @author * @version 1.00 2013/3/16 */ import java.util.Scanner; public class project2 { public static void main (String args[]) { Scanner input = new Scanner (System.in); //declare variables int giftids[]={1,2,3,4,5}; int giftpoints[]={2000,1200,700,200,1000}; String gifts[]={"i-pad","PS3","Bike","i-pod","Laptop"}; int customerpoints[]={500,1200,300}; String names[]={"James","Joe","Danny"}; int pin[]={111,222,333}; int choices=0, sub=0, userpin=0, usergiftids=0, additional_points=0; boolean flag=false; String strlogin, strpin, stroption, strgift, strusergiftids, stradditional_points; //Print out welcome screen System.out.print("\n---Welcome to the Loyalty Card Scheme ---"); System.out.print("\n\nPlease choose an option : \n\n1: Login \n2: Exit\n-->"); strlogin=input.next(); //while loop to check that only 1 or 2 is entered while(!strlogin.matches("[1-2]")){ //beep to alert user that they have made an error java.awt.Toolkit.getDefaultToolkit().beep(); System.out.println("\nError! 1 or 2 only"); System.out.print("\n\nPlease choose an option : \n\n1: Login \n2: Exit\n-->"); strlogin=input.next(); }//end while loop to check to only 1 or 2 is entered choices=Integer.parseInt(strlogin); //do loop to makes sure that the choices are excuted at least once do{ //if statement to check if 1 is entered if(choices==1){ System.out.print("\nPlease enter Pin: "); //take in pin from user strpin=input.next(); // loop to check that only correct pin is entered while(!strpin.matches("\\d+")){ //beep to alert user that they have made an error java.awt.Toolkit.getDefaultToolkit().beep(); System.out.println("Incorrect Pin!"); System.out.print("\nPlease enter Pin: "); strpin=input.next(); }//end while loop to check only correct pin is entered userpin=Integer.parseInt(strpin); }//end if choices ==1 //else statment to exit program else{ System.out.println("\n\n\t***Thank you for using the Loyalty Card Scheme***"); System.out.print("\n\n"); System.exit(0); }//end else to exit program //for loop to go through the array to look for user pin for(int i=0; i<pin.length; i++){ //if statemenr if the pin enter is in the array if(userpin == pin[i]){ //change flag to true flag=true; //holds onto the value of i sub=i; //jump out of loop break; }//end if userpin is true }//end for loop //if flag is flase if(flag==false){ //beep to alert user that they have made an error java.awt.Toolkit.getDefaultToolkit().beep(); System.out.println("Incorrect Pin!"); }//end if flag is false }while(flag ==false); stroption=""; //loop to print out user's name and points and the option menu while(!stroption.equals("4")){ System.out.println("\nWelcome: " + names[sub]); System.out.println("Points : "+ customerpoints[sub]); //print out option menu System.out.print("\n\nPlease choose an option : \n\n1: View Gifts \n2: Buy Gifts \n3: Add Points \n4: Exit\n-->"); //take in the option the user chooses stroption=input.next(); //check that only 1 to 4 is entered while(!stroption.matches("[1-4]")){ //beep to alert user that they have made an error java.awt.Toolkit.getDefaultToolkit().beep(); System.out.println("\nError! 1 to 4 only"); System.out.print("\n\nPlease choose an option : \n\n1: View Gifts \n2: Buy Gifts \n3: Add Points \n4: Exit\n-->"); stroption=input.next(); }//end while to check only 1 to 4 is entered choices=Integer.parseInt(stroption); //if statements if the user selects 1 if(choices==1){ //print out heading for table System.out.println("\n\tId \t Gift \t\t Gift Points:"); System.out.println("\t------------------------------------"); //for loop to print out gifts in table for (int i = 0; i < gifts.length; i++){ System.out.println("\t"+giftids[i] + "\t" + gifts[i] + "\t\t\t" + giftpoints[i]); }//end for loop }//end if choices ==1 //else if statement if the user choose's option 2 else if(choices==2){ System.out.print("\nEnter gift Id that you wish to purchase? \n-->"); //take in the id of the gift that the user wishes to purchase strusergiftids=input.next(); //check that only 1 to 5 can be entered while(!strusergiftids.matches("[1-5]")){ java.awt.Toolkit.getDefaultToolkit().beep(); System.out.println("Incorrect Gift ID!"); System.out.print("\nEnter gift Id that you wish to purchase? \n-->"); strusergiftids=input.next(); }//end while cheeck 1 to 5 only usergiftids=Integer.parseInt(strusergiftids); //for loop to go through the gift ids for(int x=0; x<giftids.length; x++){ //while loop to check to the gift id corresponds to the id the user inputted while(usergiftids == giftids[x]){ //if statment if the customers points are less than the points needed to buy the gift if(customerpoints[sub]<giftpoints[x]){ //beep to alert user that they have made an error java.awt.Toolkit.getDefaultToolkit().beep(); System.out.println("\t***** "+gifts[x]+" "+giftpoints[x]+" Points *****"); //Print out if not got enough points System.out.println("\n\t**** Not enough points ****"); break; }//end if customers points are less that points needed for the gift //else statement if the points are greater than the cost of the gift else{ System.out.println("\t***** "+gifts[x]+" "+giftpoints[x]+" Points *****"); //Print out if the user has enough points System.out.println("\n\t***** " + gifts[x] +" Purchased *****"); //take the cost of the gift from the user points customerpoints[sub]=customerpoints[sub]-giftpoints[x]; //break out of the loop break; }//end else customer has enough points to buy the gift }//end while usergiftid == gift ids }//end for }//end if else choice == 2 //else if statement if user enters choice 3 else if(choices==3){ System.out.print("\nHow many Points do you want to buy? \n-->"); //take in how many points the user wants to add stradditional_points=input.next(); //check that only numbers can be entered while(!stradditional_points.matches("\\d+")){ //beep to alert user that they have made an error java.awt.Toolkit.getDefaultToolkit().beep(); System.out.println("Numbers only!"); System.out.print("\nHow many Points do you want to buy? \n--> "); stradditional_points=input.next(); }//end check that only numbers can be entered additional_points=Integer.parseInt(stradditional_points); //add the points onto the customers existing balance customerpoints[sub] = customerpoints[sub]+additional_points; }//end if else choice ==3 //else statement to end the program else{ System.out.println("\n\n\t***Thank you for using the Loyalty Card Scheme***"); //Get the program to jump down 2 lines System.out.print("\n\n"); System.exit(0); }//end else to exit program }//end while stroption }//end main method }//end class
- 03-19-2013, 03:20 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Re: Suggestion on how to improve the program
The first thing I can suggest is to refactor your code into smaller methods. It so hard to maintain a code where you have such a big method. The whole body of a method should be seen without scrolling the page.
Website: Learn Java by Examples
- 03-19-2013, 04:00 AM #3
Similar Threads
-
Is there any way to improve this code?
By outforlunch in forum New To JavaReplies: 1Last Post: 09-22-2012, 05:12 AM -
suggestion
By Jahnavi Priyadarshini SVH in forum Sun Java Wireless ToolkitReplies: 0Last Post: 07-24-2012, 07:59 PM -
Need suggestion..
By chinna in forum Advanced JavaReplies: 4Last Post: 08-16-2011, 10:18 AM -
Improve my GUI!
By AJArmstron@aol.com in forum New To JavaReplies: 8Last Post: 04-27-2010, 09:17 PM -
how to improve the performance of JWS?
By dinesh kaushik in forum Java AppletsReplies: 0Last Post: 11-21-2007, 08:46 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks