How do I write this boolean method?

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.

My question is divided into two related parts (please answer each in a separate paragraph):

  1. How do I write a boolean method userKnowsRandomCharsFromMemorableWord(String)? This method should ask the user for 2 different random characters from the current customer’s memorable word. The customer’s memorable word is the word stored in the memorableWords array, at the same index as the customer’s name in the customer array. The method should return true if both characters given by the user match the memorable word characters asked for, and false otherwise.
  2. It is possible to write the above method with three statements incorporating calls to existing methods in the CustomerVerifier class as given to you, plus one or two further statement(s) to return a value? A single return statement is possible with the right choice of String method.

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:
For this, and many of your other questions, you would most likely get more help if you were to try to show what code you've written yourself already.
0
badwolfffAuthor Commented:
I will do in a few hours. Still writing it (struggling with it). Thanks
0
badwolfffAuthor Commented:
For the question asked above I have managed to write the following code referring to textbooks and asking friends. Could someone please check if this answers the question and if there are any errors in what I have written?

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


thanks
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:
C:\>java CustomerVerifier

Enter customer name: Jarvis
Invalid customer Jarvis
Verify another customer? y
Enter customer name: Rob
Enter PIN: 1234
Incorrect PIN (1234) for customer Rob
Verify another customer? y
Enter customer name: Rob
Enter PIN: 1111
Enter character 6 from your memorable word: n
Enter character 7 from your memorable word: s
Verified customer Rob with pin 1111 and memorable word Mittens

Seems to be ok.
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
badwolfffAuthor Commented:
Thanks
0
krakatoaCommented:
It might help you in the future to say what context your question is in - academic, commercial, recreational, other. EE has a policy which now allows academic questions to be answered in full, but many experts here don't agree with that, so it would be advisable to come out and say something about that at the outset, to allay any concerns they might have, and get the best experts to join in on your questions.

Just a suggestion.
0
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.