I'm a student working on a project, and I have a method takes a Person, adds it to the array (Admits[]) and writes the array to file. The method currently does not do this, as the numSoFar value does not increase and the person is not saved to the array. (either that or i made a mistake in my tester class) I put coding of both classes below, and hope someone can point me in the direction of what i am doing wrong. As this is a school project please try not to be too specific actually, just tell me some suggestions as to why my coding may not work. I don't want to pass off someone else's work as my own; i just need a new set of eyes to see what I haven't.
Coding in my Database class
//adds a Person to the Database
public void addPerson(Person admit)
{
for(int i = 0; i < numSoFar; i++)//i = -1 when admit's alpabetical spot in Database has been located
{
if((i == numSoFar-1) || (i == numSoFar))//there is no Person in this index, so this is the end of the database
{
Admits[i] = admit;
numSoFar = numSoFar + 1;
}
else
{
if(Admits[i].getLN().compareTo(admit.getLN()) == -1)//last name comes before last name of Person at index i
{
Person current= Admits[i];
Admits[i] = admit;
while(i <= numSoFar)
{
Person next = Admits[i+1];
Admits[i+1] = current;
current = next;
i++;
}
numSoFar++;
}
else
{
if(Admits[i].getLN().equalsIgnoreCase(admit.getLN()))//admit and Person at index i have the same last name
{
if(Admits[i].getFN().compareTo(admit.getFN()) == -1)
{
Person current= Admits[i];
Admits[i] = admit;
while(i < numSoFar)
{
Person next = Admits[i+1];
Admits[i+1] = current;
current = next;
i++;
}
numSoFar++;
}
else
{
if(Admits[i].getFN().equalsIgnoreCase(admit.getFN())) //admit and Person at index i are the same person
{
Scanner in = new Scanner(System.in);
System.out.println("There is already a person with this name in the database.");
int c = 0;
while(c != 1 || c != 2)
{
System.out.println("If you would like to keep that person, enter '1', and if you would like to replace him/her with the person entered, enter 2.");
if(c == 2)
{
Admits[i] = admit;
}
}
}
}
}
}
}
}
this.writeToFile();
}
This is the coding to add a new person in my tester (i stopped after case 1
Person[] Admits = new Person[5000];
Database dB = new Database(Admits);
dB.fillFromFile();//fills array with info from text file
Scanner in = new Scanner(System.in);
String c = "0";
System.out.println("Hello, and thank you for using the Notre Dame admitted students friend-finder");
while(!c.equals("6")) //Menu for user to traverse, is exited when user enters a 6
{
System.out.println("Please pick the desired action from one of the options below and enter its number.");
System.out.println(" 1: Enter info for a new admitted student and check matches");
System.out.println(" 2: Change info for an admitted student already in the database");
System.out.println(" 3: Delete an admitted student from the database");
System.out.println(" 4: Log in as an admitted student to check matches");
System.out.println(" 5: View contact info for a certain person in the database");
System.out.println(" 6: Exit the program");
c = in.next();
switch(c)
{
case "1": //create new student and check matches
System.out.println("Enter your first name:");
String firstName = in.next();
System.out.println("Enter your last name:");
String lastName = in.next();
System.out.println("Enter your gender:");
String gen = in.next();
Person p = new Person(lastName, firstName, gen);
//String chracteristics
System.out.println("Are you an a. introvert or b. extrovert? (enter a or b):");
String traitIntroExtro = in.next();
while(!(traitIntroExtro.equalsIgnoreCase("a") || traitIntroExtro.equalsIgnoreCase("b")))
{
System.out.println("Invalid choice.Please re-enter your choice:");
traitIntroExtro = in.next();
}
if(traitIntroExtro.equalsIgnoreCase("a"))
{
p.setTraitIntroExtro("Introvert");
}
else
{
p.setTraitIntroExtro("Extrovert");
}
There above coding is basically repeated with different variables, but as there are many i will cut to the end of case 1
//facebook url to contact matches with
System.out.println("Please enter the url of your facebook profile page:");
String url = in.next();
p.setFacebookUrl(url);
dB.addPerson(p);
p.fillMatches(dB);
boolean first = dB.first();
if(first == true)//the database only has one person in it
{
System.out.println("You are currently the only person in the database.");
}
else//the database has atleast one person in it
{
System.out.println("Your top 2 most compatible people currently in the data base are:");
System.out.println(p.getMatches().getHead().getPerson() + ", who can be found at " + p.getMatches().getHead().getPerson().getFacebookUrl());
if(dB.getNumSoFar() == 2)
{
System.out.println("This is the only other person in the database.");
}
else
{
System.out.println(p.getMatches().getHead().getNextNode().getPerson() + ", who can be found at " + p.getMatches().getHead().getNextNode().getPerson().getFacebookUrl());
}
}
break;