I am creating a simple class that takes in a students first name and last name.
the constructor for this is Student myStudent = new Student (first name, last name). I have to public methods that can be called. They are getName(), which returns the persons full name as a string and getEmailAddress() which returns there e-mail address as a string.
The problem i am having is that we need to keep track of multiple last names to change a number. The e-mail address should turn out something like lastname.1@school.edu and if there are multiple people with the same last name then it would be lastname.2@school.edu.
I can create and ArrayList<String> that adds in the last names. I think what is happening is that the ArrayList is being "recreated" from scratch when I do my myStudent = newStudent(first name, last name).
Anyway, here are the two files i am working with:
What I need is for the ArrayList in the Student.java file to only be initialized once (when the first Student myStudent call is made).
I can do this easy enough in the test driver file but it does not work for what i need to do. I can not pass anything across
the constructor for this is Student myStudent = new Student (first name, last name). I have to public methods that can be called. They are getName(), which returns the persons full name as a string and getEmailAddress() which returns there e-mail address as a string.
The problem i am having is that we need to keep track of multiple last names to change a number. The e-mail address should turn out something like lastname.1@school.edu and if there are multiple people with the same last name then it would be lastname.2@school.edu.
I can create and ArrayList<String> that adds in the last names. I think what is happening is that the ArrayList is being "recreated" from scratch when I do my myStudent = newStudent(first name, last name).
Anyway, here are the two files i am working with:
Code:
import java.util.ArrayList;
public class Student
{
private String firstName="", lastName="";
ArrayList<String> lastNames = new ArrayList<String>();
public Student(String fName, String lName)
{
this.firstName = fName;
this.lastName = lName;
}
private static String createEmailAddress(String firstName, String lastName, ArrayList<String> lastNames)
{
String studentsEmail = "";
int number = 0;
lastName = lastName.toLowerCase();
for(int index=0; index < lastNames.size(); index++)
{
if(lastName.equals(lastNames.get(index)))
{
number++;
}
}
studentsEmail = lastName + "." + (number+1) + "@osu.edu";
return studentsEmail;
}
public String getName()
{
String studentsName = this.firstName + " " + this.lastName;
return studentsName;
}
public String getEmailAddress()
{
String emailAddress = createEmailAddress(this.firstName, this.lastName, lastNames);
return emailAddress;
}
}
Code:
import java.util.ArrayList;
import java.util.Scanner;
public class TestDriver
{
public static void main(String[] args)
{
char keepgoing = 'y', answer; //chars for while loop and user input
Scanner keyboard = new Scanner(System.in); //for getting user input from keyboard
String selection, firstName="", lastName=""; //the string input and the number to be changed
Student studentName = new Student(firstName, lastName);
while(keepgoing != 'q')
{
//Selections for the user to choose from
System.out.println("Please select from the following options: ");
System.out.println(" enter name [e]: ");
System.out.println(" get students full name [n]: ");
System.out.println(" get students e-mail address [a]: ");
System.out.print(" quit [q]: ");
//get input and compare the first character
selection = keyboard.nextLine();
answer = selection.charAt(0);
System.out.println();
if(answer == 'e')
{
//get the natural number as a string so it can be arbitrarily long to start
System.out.print("Please enter the first name: ");
firstName = keyboard.nextLine();
System.out.print("Please enter the last name: ");
lastName = keyboard.nextLine();
System.out.println();
studentName = new Student(firstName, lastName);
}
else if(answer == 'n') //increment
{
//call getName method
System.out.println("The students full name is: " + studentName.getName() + "\n");
}
else if(answer == 'a') //decrement
{
//call getEmailAddress method
System.out.println("The students e-mail address is: " + studentName.getEmailAddress() + "\n");
}
else if(answer == 'q')//quit
{
//set keepgoing so while loop stops
keepgoing = 'q';
//print out exit message
System.out.println("Quitting Program");
}
else //user entered an unknown option
{
//print message to the screen
System.out.println("You selected an unknow option please choose again\n");
//set keepgoing to answer so while loop continues
keepgoing = answer;
}
}
}
}
What I need is for the ArrayList in the Student.java file to only be initialized once (when the first Student myStudent call is made).
I can do this easy enough in the test driver file but it does not work for what i need to do. I can not pass anything across