Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: head is fried

  1. #1
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default head is fried

    this is problem ive been given
    Problem
    Using a structure chart design a program that essentially implements the login
    piece of your project, i.e.:
    CIT MODULE REGISTRATION - Login
    ================================================== =========================
    ID Number: S1234
    Password: apples
    Implement the solution in Java - your solution should use void and/or value
    returning methods as appropriate. The program should use a set of parallel
    arrays to store the details corresponding to each user and another set of parallel
    array to store the details of modules available.
    For example the following arrays could be declared to store:
    • user details:
    String[] userId = {“S1234”, “A1234”, “S456”, “S789”};
    String[] password = {“apple”, “peach”, “banana”, “apricot”};
    String[] firstName = {“Jack”, “Belinda”, “John”, “Mary”};
    String[] lastName = {“Black”, “Blake”, “Smith”, “Murphy”};
    int[] userType = {1, 2, 1, 1};
    String[] choice1 = {“SOFT6017”, null, “SOFT6018”, “SOFT6017”};
    String[] choice2 = {“SOFT6018”, null, “SOFT6017”, “SOFT6018”};
    • module details:
    String[] moduleCode = {“SOFT6018”, “SOFT6017”, “COMH6002”,
    “COMH6003”};
    String[] moduleName = {“PSP1”, “PSP2”, “Computer Architecture”,
    “Computer Hardware”};
    int[] numberRegistered = {3, 3, 0, 0};
    From the above sample data we can that:
    • the user with user ID S1234 has:
    o a password of apple
    o a first name of Jack
    o last name of Black
    o is a student, i.e. a user type of 1 indicates a student while a user type
    of 2 indicates an administrator.
    o chosen SOFT6017 as their first module
    o chosen SOFT6018 as their second module.
    • the module with module code SOFT6018 has:
    o a name of PSP1
    o a 2 students registered
    Please note that, for the moment it is sufficient to auto-initialise the arrays to
    populate them with data but ultimately this data will be read from a file.
    This program should:
    • Enable the user to login, i.e. provided a valid user id and password
    combination is entered, the user should be informed that they have logged in
    successfully and be presented with an appropriate menu depending on the
    type of user they are, i.e.
    3 Department of Computing
    o If the user is an administrator then they should be presented with the
    following menu:
    Belinda B. (Admin) MODULE REGISTRATION - [Main Menu] 12-Mar-2013
    ================================================== =========================
    1 - Add a Student
    2 - List All Students
    3 - Add a Module
    4 - List all Modules
    5 - List Students Registered for a Module
    6 - Exit
    Please choose and option:[1-6]
    o If the user is a student then they should be presented with the following
    menu:
    Jack B. (Student) MODULE REGISTRATION - [Main Menu] 12-Mar-2013
    ================================================== =========================
    1 - List my Current Modules
    2 - Register for a Module
    3 - Deregister for a Module
    4 - List all Modules
    5 - Exit
    Please choose and option:[1-5]
    • Implement administrator menu option 4 and student menu options 1 and 4
    – for the other menu options it is sufficient to create stub methods which
    simply display a message to say that this method must be written.


    this is what i have so far -------now i am stuck any tips/suggestions
    import java.util.Scanner;
    public class Registration{
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) 
    	{
    		String[] userId = {"S1234", "A1234", "S456", "S789"}; 
    		String[] password = {"apple", "peach", "banana", "apricot"}; 
    		String[] firstName = {"Jack", "Belinda", "John", "Mary"}; 
    		String[] lastName = {"Black", "Blake", "Smith", "Murphy"}; 
    		int[] userType = {1, 2, 1, 1}; 
    		String[] choice1 = {"SOFT6017", null, "SOFT6018", "SOFT6017"}; 
    		String[] choice2 = {"SOFT6018", null, "SOFT6017", "SOFT6018"}; 
    		welcome();
     
    		int loggedInIndex = login(userId, password);
    		if(loggedInIndex!=-1)
    		{
    			System.out.println("Welcome " + firstName[loggedInIndex] +" " +  lastName[loggedInIndex]);
     
    		}
    		else
    		{
    			System.out.println("Invailed Details Entered");
    		}
    	}
     
    	private static int login(String[] userId, String[] password) 
    	{
    		Scanner keyboard = new Scanner(System.in);
     
    		String userIdEntered = readString("Please Enter ID Number: ");
    		int index = find(userId, userIdEntered );
     
    		String passwordEntered = readString("Enter Password: ");
    		if(passwordEntered.equals(password[index]))
    		{
    			return index;	
    		}
    		else
    		{
    			return -1;
    		}
     
    	}	
     
    	private static void welcome() 
    	{
    		System.out.println(" CIT MODULE REGISTRATION - Login");
    		System.out.println("================================");
    	}
     
    	private static String readString(String prompt)
    	{
    		// Create a scanner object for input from the console window
    		Scanner keyboard = new Scanner(System.in);
     
    		String value;
     
    		do
    		{
    			System.out.println(prompt);
    			value = keyboard.nextLine();
    			if(value.length()<1)
    			{
    				//error
    				System.out.println("Error");
    			}
    		}while(value.length()<1);
    		return value;		
    	}
    	/**Finds the index of a specified item in an array
    	 * @param x The array to be searched
    	 * @param value The value to be found
    	 * @return The index in the array at which the item is found */
    	public static int find(String[] x, String value) {
    		int index = 0;
    		boolean found = false;
    		while(index < x.length && !found)
    		{
    			if (x[index].equals( value))
    				found = true;
    			else
    				index++;
    		}
    		if (found)
    			return index;
    		else
    			return -1;
    	}
    	 /** The readInteger method reads an integer value from
    	 * the console window and will display an appropriate
    	 * error message if a non-integer value is entered.
    	 * @param prompt A prompt to request the user to enter a value
    	 * @return The integer value entered.
    	 */
    	public static int readInteger(String prompt)
    	{
    		// Create a scanner object for input from the console window
    		Scanner keyboard = new Scanner(System.in);
     
    		// boolean flag which is used to control the 
    		// data validation loop
     
    		boolean numGood = false; // Assume the worst	
    		do
    		{
    			System.out.print(prompt);  // ask for the value
    			if(!keyboard.hasNextInt()) // check if what's in the keyboard buffer is not an integer
    			{
    				System.out.println("You must enter an integer value!"); // display an error message
    				keyboard.nextLine(); // consume the bad value entered
    			}
    			else
    				numGood = true; // value entered is good
    		} while(!numGood);
    		// at this point we know the value in the
    		// keyboard buffer is numeric so we can go ahead and
    		// return it.
    		return keyboard.nextInt();
    	}
     
    	/**
    	 * The readPositiveInteger method reads a positive integer value
    	 * from the console window and will display an appropriate
    	 * error message if a non-positive integer value is entered.
    	 * @param prompt A prompt to request the user to enter a value
    	 * @return The positive integer value entered.
    	 */
    	public static int readPositiveInteger(String prompt)
    	{
    		int value;
    		do
    		{
    			value = readInteger(prompt); // ask for and read an integer value
    			if (value < 0) // check if the value entered is less than 0
    				// display an error message
    				System.out.println("Error - you must enter a positive integer value!");
    		} while (value <0);
    		// at this point we know the value entered is positive
    		// so return it
    		return value;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: head is fried

    i am stuck
    Please explain what you are stuck on. Which requirements have you done and which one are you currently having problems with?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    SOLAS (April 3rd, 2013)

  4. #3
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: head is fried

    Quote Originally Posted by Norm View Post
    Please explain what you are stuck on. Which requirements have you done and which one are you currently having problems with?
    HI NORM
    this is the output i have at the moment
    CIT MODULE REGISTRATION - Login
    ================================
    Please Enter ID Number:
    A1234
    Enter Password:
    peach
    Welcome Belinda Blake


    basically im stuck on everything after this . i know how to create the menus using switch and case but thats about it e.g how do i get usertype of 1 to display as admin with name. not sure how to pass the module name array to the menu selection that lists all modules.
    thanks for taking an interest Norm

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: head is fried

    how do i get usertype of 1 to display as admin with name
    Where does the code get the usertype and use it to control a display?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: head is fried

    Quote Originally Posted by Norm View Post
    Where does the code get the usertype and use it to control a display?
    the display wanted is
    If the user is an administrator then they should be presented with the
    following menu:
    Belinda B. (Admin) MODULE REGISTRATION - [Main Menu] 12-Mar-2013
    ================================================== =========================
    1 - Add a Student
    2 - List All Students
    3 - Add a Module
    4 - List all Modules
    5 - List Students Registered for a Module
    6 - Exit
    Please choose and option:[1-6]



    in the usertype array belinda has a usertype of 1 which is admin

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: head is fried

    Where does the code get the value for the usertype? It needs to get that value so it can decide what to display.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: head is fried

    Quote Originally Posted by Norm View Post
    Where does the code get the value for the usertype? It needs to get that value so it can decide what to display.
    thats one of things im stuck on i know i must pass the values but dont know how to be honest

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: head is fried

    What value is in the variable: loggedInIndex?

    The code has several arrays whose values are in "parallel". Once an index's value is set, that same index can be used to get values from the other arrays. See how it's done in the following statement:
      System.out.println("Welcome " + firstName[loggedInIndex] +" " +  lastName[loggedInIndex]);
    There are two arrays that are indexed with loggedInIndex. That same index can be used to get values from the other arrays, like the userType array.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: head is fried

    Quote Originally Posted by Norm View Post
    What value is in the variable: loggedInIndex?

    The code has several arrays whose values are in "parallel". Once an index's value is set, that same index can be used to get values from the other arrays. See how it's done in the following statement:
      System.out.println("Welcome " + firstName[loggedInIndex] +" " +  lastName[loggedInIndex]);


    There are two arrays that are indexed with loggedInIndex. That same index can be used to get values from the other arrays, like the userType array.

    but wont thta just return the value in the array ie 1 not admin

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: head is fried

    return the value in the array
    Yes it would.
    Read the assignment to see the definitions for usertype values. The code would need an if statement to test the contents of the array to determine if the person was admin or student
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: head is fried

    Quote Originally Posted by Norm View Post
    Yes it would.
    Read the assignment to see the definitions for usertype values. The code would need an if statement to test the contents of the array to determine if the person was admin or student
    i tried this
    if(userType=1)
    but no good

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: head is fried

    Look at how the code printed the first and last names. That is a sample of how to get values from an array.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: head is fried

    Quote Originally Posted by Norm View Post
    Look at how the code printed the first and last names. That is a sample of how to get values from an array.
    will do but must hit the hay its 1.15am here , i have figured out how to get array contents to display through menu.
    thank you for your help ill be at it again tomorrow (wreck ur head again)lol

  15. #14
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: head is fried

    back again i happy with prog now except for one thing below is the code for one of the menus that is displayed
    public static void menuAdmin(String[] moduleName)

    	{
     
     
    	int selects;
    	int select;
     
     
    		do{
     
    			select=readPositiveInteger("Please choose and option:[1-6].\n"+ 
                    "1 - Add a Student.\n"+
                    "2 - List All Students.\n"+
                    "3 - Add a Module.\n"+
                    "4 - List all Modules.\n"+
                    "5 - List Students Registered for a Module.\n"+
                    "6 - Exit.");
     
    		boolean numb=false;
     
    		Scanner keyboard = new Scanner(System.in);
     
    			//System.out.println("Please choose and option:[1-6]");
    			//System.out.println("1 - Add a Student");
    			//System.out.println("2 - List All Students");
    			//System.out.println("3 - Add a Module");
    			//System.out.println("4 - List all Modules");
    			//System.out.println("5 - List Students Registered for a Module");
    			//System.out.println("6 - Exit");
     
    			selects= keyboard.nextInt();
    			switch(selects)
    			{
    			case 1:
    				not();
    				break;
     
    			case 2:
    				not();
    				break;
    			case 3:
    				not();
    				break;
    			case 4:
    				for(String n:moduleName )
    				{
    				    System.out.println(n+" ");
    				}
    				break;
    			case 5:
    				not();
    				break;
    			}
     
    			}while (selects!=6);
    		goodbye();
    	}

    the problem is that you must enter your selection twice before it recognises that selection

    CIT MODULE REGISTRATION - Login
    ================================
    Please Enter ID Number:
    A1234
    Enter Password:
    peach
    Welcome Belinda Blake
    Belinda (ADMIN)MODULE REGISTRATION - [Main Menu] 12-Mar-2013
    ================================================== ================================================
    Please choose and option:[1-6].
    1 - Add a Student.
    2 - List All Students.
    3 - Add a Module.
    4 - List all Modules.
    5 - List Students Registered for a Module.
    6 - Exit.1
    1
    METHOD NOT WRITTEN
    Please choose and option:[1-6].
    1 - Add a Student.
    2 - List All Students.
    3 - Add a Module.
    4 - List all Modules.
    5 - List Students Registered for a Module.
    6 - Exit.6
    6
    Goodbye Thank You For Using The CIT MODULE REGISTRATION
    You Are Logged Out

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: head is fried

    must enter your selection twice before it recognises that selection
    Try debugging the code to see what it is doing: Add a call to a println statement to print out the value of selects immediately after it is read.
    Also add a default: case on the select() statement that prints out a message saying what the value of select was since it was not taken by any of the other case statements.

    What does the readPositiveInteger() method do? What is the value of select after it is called? print it to see
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    SOLAS (April 5th, 2013)

  18. #16
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: head is fried

    NORM you are a genius have it fixed i basically over complicated the whole thing select was storing the value i entered but because i had added selects it needed the value again to carry out the task.

    --- Update ---

    In one of the menu options i must display the students current modules i know how to call all objects from the array ie
    for(String n:moduleName )
    				{
    				    System.out.println(n+" ");
    				}
    what i cant do is get the relevant info depending on the user that is logged in

  19. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: head is fried

    Do you need to search an array for a match and use the index to display data from other arrays?
    Like what the find() method does.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #18
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: head is fried

    so i use the find() method agin but just amend it to suit
    i want to return the vale in one array depending on user ie
    if the user is jack black i want to list his modules through the menu

  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: head is fried

    Find the correct index and then use that index in the other arrays to get the other data for that user.

    want to list his modules
    Where are the modules stored for a user?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #20
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: head is fried

    the modules are stored in
    String[] moduleName = {"PSP1", "PSP2", "Computer Architecture","Computer Hardware"};
    for jack black if he selects list my modules all i want display is PSP1

  23. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: head is fried

    The way parallel arrays work: look up in one array, get a match at some index, and then use that index with the other arrays to get their data.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Re: Trying to wrap my head around Java
    By Sylis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 10th, 2012, 01:54 PM
  2. Trying to wrap my head around Java
    By Sylis in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 10th, 2012, 08:10 AM
  3. Cannot get my head around this :S
    By hillzyy in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 17th, 2012, 02:21 PM
  4. Easy but over my head
    By Dejay79 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 14th, 2012, 10:54 AM
  5. What in pan-fried sea snakes is a "proposal computer"?
    By Blackbird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 8th, 2011, 09:22 AM