0

So I have an issue with Hashmaps and a login feature.

  1. When using addLogin I am required to enter parameters but it makes no sense to do this since I have already done this in the constructor classes. How would I simply just use addLogin and the Surname, Forename and Personal ID number is added to the hashmap?

  2. Upon using Math.round(Math.random()*999+1) to generate a random number between 1-999 how am I supposed to go around adding this to the hashmap with the other student details?

Here is the full code that applies to both questions, apologies for the stupid questions I'm very new to Java! I am very appreciative of any help I recieve. Thanks in advance.

public class TestApplication
{
// hashmap
private HashMap<String, ArrayList <String>> Application = new HashMap<String, ArrayList <String>>();
// hashset
private HashSet<String> loginsIssued = new HashSet<String>();
// An Arry List for storing student information 
private ArrayList<String> Student = new ArrayList<String>();

/**
 * Constructor for objects of class Application
 */
public TestApplication(String Surname, String personalIdNo)
{
    if (isValidpersonalIdNo(personalIdNo) == true)
    {
        Student.add(Surname);
        Application.put(personalIdNo, Student);
        System.out.println("Application number ### " +  "has registered successfully");
    }
    else
    {
        System.out.println("Application has failed, Personal id: " + personalIdNo);
    }
}

/**
 * Create a Student Information
 */
public void TestApplication(String personalIdNo, String Surname, String Forename)
{
    Student.add(Surname);
    Student.add(Forename);
    Student.add (personalIdNo);
}

/** 
 * Add Login
 * Pull First Letter of Forenames
 * Pull First Letter of Surname
 * Generate Random Number
 * Print
 */
public void addLogin(String Surname, String Forename)
{
    String login = "";
    {
        System.out.println (Surname.charAt(0) + "" + " " + Forename.charAt(0) + " " + Math.round(Math.random()*999+1));
        Student.add(login);
        loginsIssued.add(login);

    }
}

/** 
 * CONDITION 1
 * Check whether the ID supplied is only numbers
 */
public boolean isNumeric(String personalIdNo)  
{  
    if (personalIdNo.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {   
        return true;
    } 
    else 
    {  
        return false;
    }  
}

/** 
 * CONDITION 2
 * Check whether the ID supplied has a length of 10
 */
public boolean checkLength(String personalIdNo)
{
    if (String.valueOf(personalIdNo).length()==10) 
    {
        return true;
    }
    else
    {
        return false;
    }
}

/** 
 * CONDITION 3
 * Check whether the ID supplied starts with 1
 */
public boolean checkFirstDigit(String personalIdNo)
{
    if (personalIdNo.startsWith("1"))
    {
        return true;
    }
    else
    {
        return false;
    }
}

/** 
 * Validation Check - Check if it satisfies all conditions. 
 */
public boolean isValidpersonalIdNo(String personalIdNo)
{
    if (isNumeric(personalIdNo) &&  checkLength(personalIdNo) && checkFirstDigit(personalIdNo))
    {
        return true;
    }
    else
    { 
        return false;
    }
}

/** 
 * FORENAME
 * Add Forename
 */
public void addForename(String Forename)
{
    Student.add(Forename);  
}

/** 
 * Return Surname
 */
public String getSurname()
{
    return Student.get(0);
}

}

screamingchimps
  • 161
  • 2
  • 2
  • 7
  • 2
    Nothing to do with your problem but it is a bad idea to give the same name to a method than to a class.(`public void TestApplication`) . You could think it is a constructor. – C.Champagne Dec 04 '13 at 21:47
  • 1
    For 1, what paramaters do you have to put that you think are unnessecary? – hichris123 Dec 04 '13 at 21:47
  • What do you want to achieve exactly? What should your program exactly do? In addition to the question about 1, I have a question about 2: what do you intend to do with your generated number? You only display it for the moment. Do you want to use it to create a personal id ? – C.Champagne Dec 04 '13 at 22:18
  • Basically addLogin that takes no parameter and takes the first character of each forename and the first character of the surname contained in student and adds a random number in the range 1 to 999 to form a login which is then added as the final entry to the student collection. A record is kept of all such logins created in a further collection, accessible from any Application object, called loginsIssued. This is then used to ensure that each login assigned is unique. – screamingchimps Dec 04 '13 at 22:21
  • Another remark about your code: your variable and method names should always begin with a lower case letter. It is not crucial but it makes things a bit clearer. Furthermore, instead of using an ArrayList to represent a student, I would make specific class with surname, forename... as fields. – C.Champagne Dec 04 '13 at 22:29

2 Answers2

0

Concerning to your first question At the initialisation i guess you just want to give the string a value normally you achieve this through writing String login= null; But I'm quiet not sure what you want to achieve with the empty ""

And i dont get why you dont give your login a value before you add it to the arraylist or should be this the login by default public void addLogin(String Surname, String Forename)

String login = null;
{
    System.out.println (Surname.charAt(0) + "" + " " +         Forename.charAt(0) + " " +        Math.round(Math.random()*999+1));
    Student.add(login);
    loginsIssued.add(login);

}

And just as a tip if you return boolean at your equalization methods you dont need to check in the if clauses if true == true because the if clause checks if you do it your way wether true ==true and returns true if it like this do you get my point? you 'll save resources if you dont do this twice :) So just write your method which returns the boolean value in the if braces . I hope i can help you
Pls comment if you need further informations

Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
0

To be honest, I think there should be some rework to fix your code (sorry to not answer directly to you two questions but is impossible as is):

  1. Fix the second constructor, which is declared as a method
  2. Create a Student class: it is harder to retrieve fields by index and the risk is to add twice the same field or to miss adding one field.
  3. Unless I don't fully understand what your code should achieve, the major issue is a design error : your TestApplication class manages the whole set of students and their login thus the Student instance variable is a nonsense as well as the constructors with a single student fields. You should instead create an addStudent method with the student fields as parameter or better a Student instance.
  4. I don't understand the use of "" + " " either.

By consequence, to answer to your questions:

  1. You can keep your addLogin method and if so, yes you have to keep all the info but you can use a Student object (or a Collection as you currently modelise a student) as parameter.
  2. If you update the same Student (same object instance), it is of course updated in your map. If not(you use a copy of Student) then just execute Application.put(personId,Student). Have a look at this answer to get more info
Community
  • 1
  • 1
C.Champagne
  • 5,381
  • 2
  • 23
  • 35