I'm not sure what's wrong with my code, other than it being exceedingly long. I've tried multiple different approaches to the -c and -u arguments, but I can not get my program to validate. From the testing I've done so far, the "Recommendations from my mentor" address problems that don't exist.
Validation #3 - Be sure that all the people are really added to the allPeople list.
Validation #4 - Be sure that the size of the allPeople list does not change when people's data is updated.
Any help would be much appreciated.
Thanks,
Dan
package com.codegym.task.task17.task1711;
import java.text.SimpleDateFormat;
import java.util.*;
/*
CRUD 2
*/
public class Solution {
public static volatile List<Person> allPeople = new ArrayList<>();
static {
synchronized (Solution.class) {
allPeople.add(Person.createMale("Donald Chump", new Date())); // id=0
allPeople.add(Person.createMale("Larry Gates", new Date())); // id=1
}
}
public static void main(String[] args) {
String input = "";
for (String arg : args) {
input += arg + " ";
}
switch (args[0]) {
case "-c":
synchronized (allPeople) {
create(input, args);
break;
}
case "-u":
synchronized (allPeople) {
update(input, args);
break;
}
case "-d":
synchronized (allPeople) {
nullify(input, args);
break;
}
case "-i":
synchronized (allPeople) {
info(input, args);
break;
}
}
}
private static synchronized void create(String input, String args[]) {
//Assign var timesToCreate For each set of name,sex,bd (subtract out the command)
//This tells us to perform the creation operation 1 time for each set
int counter = 0;
for (String arg : args) {
counter++;
}
counter--; // Subtract out the command
int timesToCreate = counter / 5; //A single person contains name, sex, bd(3 arguments)
//
Calendar calendar = new GregorianCalendar();
for (int i = 0; i < timesToCreate; i++) { //This is the creation loop
//the following block creates and initializes name and sex variables based on their arguments
String name = args[1+(5*i)];
String sex = args[2+(5*i)];
//The following block creates a Date object with the info given by the date argument
int month = Integer.parseInt(args[3+(5*i)]);
int day = Integer.parseInt(args[4+(5*i)]);
int year = Integer.parseInt(args[5+(5*i)]);
calendar.set(year, month, day);
Date bdAsDate = calendar.getTime();
//The following block creates a person, adds them to the list, and then lists their ID number
int id = 0;
Person person = null;
if (sex.equals("m")) {
person = Person.createMale(name, bdAsDate);
}
else {
person = Person.createFemale(name, bdAsDate);
}
allPeople.add(person);
//The following block finds a match between the person added, and their index number, and prints it on-screen
for (int j = 0; j < allPeople.size(); j++) {
boolean nameEqual = allPeople.get(j).getName().equals(person.getName());
boolean sexEqual = allPeople.get(j).getSex().equals(person.getSex());
boolean bdEqual = allPeople.get(j).getBirthDate().equals(person.getBirthDate());
if (nameEqual && sexEqual && bdEqual) {
id = j;
}
}
System.out.println(id);
}
}
private static synchronized void update(String input, String args[]) {
//Assign var timesToCreate For each set of id,name,sex,bd (subtract out the command)
//This tells us to perform the creation operation 1 time for each set
int counter = 0;
for (String arg : args) {
counter++;
}
counter--; // Subtract out the command
int timesToCreate = counter / 6; //A single person contains id, name, sex, bd(3 arguments)
//
Calendar calendar = new GregorianCalendar();
for (int i = 0; i < timesToCreate; i++) { //This is the creation loop
//the following block creates and initializes id, name, and sex variables based on their arguments
int idNumber = Integer.parseInt(args[1 + (6 * i)]);
String name = args[2 + (6 * i)];
String sex = args[3 + (6 * i)];
//The following block creates a Date object with the info given by the date argument
int month = Integer.parseInt(args[4+(6*i)]);
int day = Integer.parseInt(args[5+(6*i)]);
int year = Integer.parseInt(args[6+(6*i)]);
calendar.set(year, month, day);
Date bdAsDate = calendar.getTime();
//The following block updates a person, and changes their info in the list
/*
Person person = null;
allPeople.remove(idNumber);
if (sex.equals("m")) {
person = Person.createMale(name, bdAsDate);
}
else {
person = Person.createFemale(name, bdAsDate);
}
allPeople.add(idNumber, person);
*/
//Trying something new here -- Seeing if this will pass the requirement, since the allPeople list will no longer change size
Sex sexAsSexObject = null;
if (sex.equals("m")) {
sexAsSexObject = Sex.MALE;
}
else {
sexAsSexObject = Sex.FEMALE;
}
Person personToEdit = allPeople.get(idNumber);
personToEdit.setName(name);
personToEdit.setSex(sexAsSexObject);
personToEdit.setBirthDate(bdAsDate);
allPeople.set(idNumber, personToEdit);
}
}
private static synchronized void nullify(String input, String args[]) {
//This block counts 1 for each id number which is to be nullified, excepting the first argument which is the command
int counter = 0;
for (String arg : args) {
counter++;
}
counter--;
int timesToDelete = counter;
//This block runs once per id, nullifying the information of each person
// corresponding to their ID number within the allPeople list
for (int i = 1; i < timesToDelete+1; i++) {
int idToBeDeleted = Integer.parseInt(args[i]);
Person personToBeDeleted = allPeople.get(idToBeDeleted);
personToBeDeleted.setName(null);
personToBeDeleted.setSex(null);
personToBeDeleted.setBirthDate(null);
}
}
private static synchronized void info(String input, String args[]) {
//The following line initializes the SimpleDateFormat with the required formatting info, which will be used later
SimpleDateFormat format = new SimpleDateFormat("MMM dd YYYY", Locale.ENGLISH);
//This block counts the number of times to the run the information check
//excepting the first argument, which is the command
int counter = 0;
for (String arg : args) {
counter++;
}
counter--;
int timesToGiveInfo = counter;
//The following block runs according to the counter, creating
//variables which will allow the info of each person to be easily
//displayed according to their corresponding ID number
for (int i = 1; i < timesToGiveInfo+1; i++) {
int idToBeDisplayed = Integer.parseInt(args[i]);
Person personToBeDisplayed = allPeople.get(idToBeDisplayed);
String name = personToBeDisplayed.getName();
String sex = "";
if (personToBeDisplayed.getSex().equals(Sex.MALE)) {
sex = "m";
}
else {
sex = "f";
}
String date = format.format(personToBeDisplayed.getBirthDate());
//The following line displays the information of the requested individual
System.out.println(name + " " + sex + " " + date);
}
}
}