Hi,
I have created Guessing Game where computer is generating random number from 1-100 and then user needs to guess it.
as the second part of the assignment I need to:
1. Restructure the application you created in exercise 5 so that it now carefully controls who has access to play the Guessing Game. Create a separate class called UserValidation which is used to check a supplied password for a given user against a stored password. If the two passwords match then the user should be allowed to proceed, otherwise the user should be asked to re‐enter both username and password. This process should be repeated without end.
a. The UserValidation class should contain an array of user names and corresponding passwords. For simplicity assume there will be no more than 10 valid users.
b. The password array should be populated at the point of object instantiation (i.e. in the class constructor) – using hard‐coded values, with usernames ordered alphabetically.
c. The UserValidation class needs only one public method – called ‘checkPwd()’ which is used to compare the supplied username/password pair with those stored internally using a simple Linear search routine.
d. The UserValidation class should have as many private methods as you deem necessary and appropriate. However, this class should NOT be used to read in the username and passwords – that should be accomplished by one of the methods within your GuessingGame class.
What I did was, I created new class called UserValidation, here you have the code:
Code:
import java.util.*; import java.io.*; //Provides for system input and output through data streams, serialization and the file system. (for example Scanner class) public class UserValidation { public String userNameInput; public String passwordInput; public String password; public String userName; private Scanner scan; public static void main(String[] args) throws IOException { // declares an array of Strings for user name String[] userName = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj"}; String[] password = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj"}; UserValidation hiLo = new UserValidation(); //pointing new object to new method called go() hiLo.go(); } public void go() { scan = new Scanner(System.in) ; // Creates Scanner instance System.out.print("What is your user name? :"); //input point userNameInput = scan.next(); System.out.print("What is your password? :"); //input point passwordInput = scan.next(); //loop for the decision point to start the game if((userNameInput.equals(userName[0])) || (passwordInput.equals(password[0]))) { //program is moving to new method GuessingGame.start(); } else if((userNameInput.equals(userName[1])) || (passwordInput.equals(password[1]))) { //program is moving to new method GuessingGame.start(); } else if((userNameInput.equals(userName[2])) || (passwordInput.equals(password[2]))) { //program is moving to new method GuessingGame.start(); } else if((userNameInput.equals(userName[3])) || (passwordInput.equals(password[3]))) { //program is moving to new method GuessingGame.start(); } else if((userNameInput.equals(userName[4])) || (passwordInput.equals(password[4]))) { //program is moving to new method GuessingGame.start(); } else if((userNameInput.equals(userName[5])) || (passwordInput.equals(password[5]))) { //program is moving to new method GuessingGame.start(); } else if((userNameInput.equals(userName[6])) || (passwordInput.equals(password[6]))) { //program is moving to new method GuessingGame.start(); } else if((userNameInput.equals(userName[7])) || (passwordInput.equals(password[7]))) { //program is moving to new method GuessingGame.start(); } else if((userNameInput.equals(userName[8])) || (passwordInput.equals(password[8]))) { //program is moving to new method GuessingGame.start(); } else if((userNameInput.equals(userName[9])) || (passwordInput.equals(password[9]))) { //program is moving to new method GuessingGame.start(); } //negative response for a loop else { System.out.println("your User Name or Password is wrong"); } } }
I am getting few errors,
1. array required, but java.lang.String found in every if, else if line "if((userNameInput.equals(userName[0])) || (passwordInput.equals(password[0])))"
2. "non-static method start() cannot be referenced from a static context" in GuessingGame.start(); I am assuming here I need to point it to an object not a method right?
or maybe there is any other way to build this new class?
Thanks in advance
Exose