How do I write a main method in this class?

I have this java class:

import java.util.Random;
import java.util.Scanner;

public class CustomerVerifier {
    private static int[] pins = new int[]{1234, 1111, 4321, 5555, 7777, 1010, 9876};
    private static String[] customers = new String[]{"Bob", "Rob", "Tim", "Jim", "Sam", "Jon", "Tom"};
    private static String[] memorableWords= new String[]{"fishing", "Mittens", "Arsenal", "6packYeah", "Porsche911", "puppies", "CSI4Ever"};
    private static Scanner scanner = new Scanner(System.in);


    private static boolean askUserToContinue() {
        String input = getUserInput("Verify another customer? ");
        return input.trim().toLowerCase().startsWith("y"); //see the String API for documentation of the trim() method
    }

    private static String getCustomerFromUser() {
        return getUserInput("Enter customer name: ");
    }

    private static int getPinFromUser() {
        String input = getUserInput("Enter PIN: ");
        return Integer.parseInt(input);  //see the subject guide volume 1 section 9.6 for more on the parseInt(String) method
    }

    private static String getUserInput(String msg) {
        System.out.print(msg);
        return scanner.nextLine();
    }

    private static boolean isValidPin(String customer, int pin) {
        int customerIndex = -1;
        for (int i = 0; i < customers.length; i++) {
            if (customer.equals((customers[i]))) {  //see the String API for documentation of the equals(Object) method
                customerIndex = i;
            }
        }
        return pin == pins[customerIndex];
    }

    private static boolean isValidCustomer(String customer) {
        for (int i = 0; i < customers.length; i++) {
            if (customer.equals(customers[i])) {
                return true;
            }
        }
        return false;
    }


    //return random integers that are distinct from each other
    private static int[] getDiscreteRandomInts(int quantity, int bound) {
        Random random = new Random();
        int[] store = new int[quantity];
        int r;
        int i = 0;
        while (i < quantity) {
            r = random.nextInt(bound);
            boolean insert = true;
            for (int j = 0; j < i; j++) {
                if (store[j] == r) {
                    insert = false;
                }
            }
            if (insert) {
                store[i] = r;
                i++;
            }
        }
        return store;
    }

    private static String charsAt(String word, int[] indexes) {
        String result = "";
        for (int i = 0; i < indexes.length; i++) {
            result += word.charAt(indexes[i]);
        }
        return result;
    }

    private static String getMemorableWordCharsFromUser(int[] chars) {
        String result = "";
        //computers start counting characters in a string from 0 but humans start at 1 so we add 1 to every number shown to the user
        for (int i = 0; i < chars.length; i++) {
            result += getUserInput("Enter character " + (chars[i]+1) + " from your memorable word: ");
        }
        return  result;
    }

    private static String getMemorableWord(String customer) {
        for (int i = 0; i < customers.length; i++) {
            if (customer.equals(customers[i])) {
                return memorableWords[i];
            }
        }

        //won't get here if the customer exists
        return "";
    }

    private static void verifiedCustomer(String customer, int pin, String memorableWord) {
        System.out.println("Verified customer " + customer + " with pin " + pin + " and memorable word " + memorableWord);
    }

    private static void incorrectPin(String customer, int pin) {
        System.out.println("Incorrect PIN (" + pin + ") for customer " + customer);
    }

    private static void invalidMemorableWord(String customer) {
        System.out.println("Invalid memorable word for " + customer);
    }

    private static void invalidCustomer(String customer) {
        System.out.println("Invalid customer " + customer);
    }
}

Open in new window


The CustomerVerifier class has methods that are intended to be used in a two-stage verification process. Firstly, the user inputs the customer name, and this is checked against the customer pin. If this is correct, then the class will go on to ask for two characters from the user’s memorable word.

How do I write a main method in this class?:

The main method should run the program. There is no need for any verification of data entered by the user, hence, if the PIN entered by the user cannot be parsed to an int, a run-time exception will be thrown.

I am very new to Java and appreciate all the help I can get. If someone could kindly show me how to do this it would be most educative. Thanks in advance
badwolfffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
Just add this into your class, if all you want to do is add a main method.

public static void main(String[] args){}

Open in new window

0
badwolfffAuthor Commented:
Hi

thanks for your comment. Before I read it, I had already worked the solution out referring to the textbook (maybe) and written the following code:

public static void main(String[] args) {
    	verify(); // calling verify method to check if program if working correctly
    }

Open in new window


I am happy for you to have the points, but can you please tell me if what I have written is ok?
thanks
0
badwolfffAuthor Commented:
by the way the full code is

import java.util.Random;
import java.util.Scanner;

public class CustomerVerifier {
    private static int[] pins = new int[]{1234, 1111, 4321, 5555, 7777, 1010, 9876};
    private static String[] customers = new String[]{"Bob", "Rob", "Tim", "Jim", "Sam", "Jon", "Tom"};
    private static String[] memorableWords= new String[]{"fishing", "Mittens", "Arsenal", "6packYeah", "Porsche911", "puppies", "CSI4Ever"};
    private static Scanner scanner = new Scanner(System.in);


    private static boolean askUserToContinue() {
        String input = getUserInput("Verify another customer? ");
        return input.trim().toLowerCase().startsWith("y"); //see the String API for documentation of the trim() method
    }

    private static String getCustomerFromUser() {
        return getUserInput("Enter customer name: ");
    }

    private static int getPinFromUser() {
        String input = getUserInput("Enter PIN: ");
        return Integer.parseInt(input);  //see the subject guide volume 1 section 9.6 for more on the parseInt(String) method
    }

    private static String getUserInput(String msg) {
        System.out.print(msg);
        return scanner.nextLine();
    }

    private static boolean isValidPin(String customer, int pin) {
        int customerIndex = -1;
        for (int i = 0; i < customers.length; i++) {
            if (customer.equals((customers[i]))) {  //see the String API for documentation of the equals(Object) method
                customerIndex = i;
            }
        }
        return pin == pins[customerIndex];
    }

    private static boolean isValidCustomer(String customer) {
        for (int i = 0; i < customers.length; i++) {
            if (customer.equals(customers[i])) {
                return true;
            }
        }
        return false;
    }


    //return random integers that are distinct from each other
    private static int[] getDiscreteRandomInts(int quantity, int bound) {
        Random random = new Random();
        int[] store = new int[quantity];
        int r;
        int i = 0;
        while (i < quantity) {
            r = random.nextInt(bound);
            boolean insert = true;
            for (int j = 0; j < i; j++) {
                if (store[j] == r) {
                    insert = false;
                }
            }
            if (insert) {
                store[i] = r;
                i++;
            }
        }
        return store;
    }

    private static String charsAt(String word, int[] indexes) {
        String result = "";
        for (int i = 0; i < indexes.length; i++) {
            result += word.charAt(indexes[i]);
        }
        return result;
    }

    private static String getMemorableWordCharsFromUser(int[] chars) {
        String result = "";
        //computers start counting characters in a string from 0 but humans start at 1 so we add 1 to every number shown to the user
        for (int i = 0; i < chars.length; i++) {
            result += getUserInput("Enter character " + (chars[i]+1) + " from your memorable word: ");
        }
        return  result;
    }

    private static String getMemorableWord(String customer) {
        for (int i = 0; i < customers.length; i++) {
            if (customer.equals(customers[i])) {
                return memorableWords[i];
            }
        }

        //won't get here if the customer exists
        return "";
    }

    private static void verifiedCustomer(String customer, int pin, String memorableWord) {
        System.out.println("Verified customer " + customer + " with pin " + pin + " and memorable word " + memorableWord);
    }

    private static void incorrectPin(String customer, int pin) {
        System.out.println("Incorrect PIN (" + pin + ") for customer " + customer);
    }

    private static void invalidMemorableWord(String customer) {
        System.out.println("Invalid memorable word for " + customer);
    }

    private static void invalidCustomer(String customer) {
        System.out.println("Invalid customer " + customer);
    }
    
    // QUESTION 1
    private static boolean userKnowsRandomCharsFromMemorableWord(String memorableWord) {
    	int[] indexes = getDiscreteRandomInts(2, memorableWord.length()); // generating 2 random ints with a maximum of the word's length
    	String momorableWordCharsFromUser = getMemorableWordCharsFromUser(indexes); // getting the input for the user for the 2 random letters in his secret word
    	String realMemorableWordChars = charsAt(memorableWord, indexes);  // getting the real 2 letters for the random ints
    	
    	return momorableWordCharsFromUser.equals(realMemorableWordChars); // checking if user's input is correct
    }
    
    // QUESTION 2
    private static void verify() {
		// initializing variables that we need for printing the final output: "Valid customer ..."
		String customerName = null;
		String memorableWord = null;
		int pin = 0;
		while (true) { // starting an infinite loop
			customerName = getCustomerFromUser(); // STEP 1: asking the customer to enter his name and storing it in a local variable
			boolean isValidCustomer = isValidCustomer(customerName); // STEP 2: checking if customer exists in the costomers array
			if (!isValidCustomer) {
				invalidCustomer(customerName);// customer is NOT valid; is not in the customers array
				boolean userWantsToContinue = askUserToContinue(); // does user want to check another customer?
				if (!userWantsToContinue) {
					System.out.println("Goodbye!"); // he does not want to continue; show a nice message
					break; // end the program
				} else {
					continue; // go back to STEP 1
				}
			}
			pin = getPinFromUser(); // STEP 3: getting the PIN from the user
			boolean isValidPin = isValidPin(customerName, pin); // STEP 4: checking if PIN is valid for this customer
			if (!isValidPin) {
				incorrectPin(customerName, pin);
				boolean userWantsToContinue = askUserToContinue(); // PIN not valid; asking the customer if he wants to check another customer
				if (!userWantsToContinue) {
					System.out.println("Goodbye!");
					break;
				} else {
					continue;
				}
			}
			memorableWord = getMemorableWord(customerName); // get the memorable word for this customer
			boolean userKnoesRandomCharsFromMemorableWord = userKnowsRandomCharsFromMemorableWord(memorableWord); // checking 2 random chars from the customer's memorable word
			if (!userKnoesRandomCharsFromMemorableWord) {
				invalidMemorableWord(customerName); // 2 random chars from the memorable word were invalid
				boolean userWantsToContinue = askUserToContinue();
				if (!userWantsToContinue) {
					System.out.println("Goodbye!");
					break;
				} else {
					continue;
				}
			}
			break;
		}
		verifiedCustomer(customerName, pin, memorableWord); // all is good!
		return;
	}
    
    // QUESTION 3
    public static void main(String[] args) {
    	verify(); // calling verify method to check if program if working correctly
    }
}

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

krakatoaCommented:
can you please tell me if what I have written is ok?

Well, I don't see why it shouldn't function. Have you tried it? It will call your verify() method, which in turn will call 3 others, asking for name, pin and memorable word. Is that what you mean ?
0
badwolfffAuthor Commented:
Yes that is what is required. What I was referring to is whether my final code has all the requirements mentioned in my question (if I have missed out or written something poorly) or upon compile do you get any errors? Basically I need someone who is a java expert to proof read my work. Thanks
0
krakatoaCommented:
Basically I need someone who is a java expert to proof read my work. Thanks

Right. Well I've done that, and your question was as to whether the code is "ok". It will run, that's for sure.

As far as 'proof reading' is concerned, 'proof' resides in the fact that a programme compiles, and then runs without error. Your code should compile, but whether there are runtime errors or it produces incorrect results, that's another question, and one that you will likely have to answer yourself.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.