Hey folks!! I just passed the case "-u" requirement but I can't quite understand why I had to create another "person" object while it wasn't necessary in task 10 below:
else if (args[0].equals("-u")) {
Date date2 = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH).parse(args[4]);
for (int i = 0; i < allPeople.size(); i++) {
if (i == Integer.parseInt(args[1])) {
allPeople.get(i).setName(args[2]);
allPeople.get(i).setBirthDate(date2);
if (args[2].equals("m")) {
allPeople.get(i).setSex(Sex.MALE);
} else {
allPeople.get(i).setSex(Sex.FEMALE);
}
}
}
package com.codegym.task.task17.task1711;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/*
CRUD 2
The birth date format is Apr 15 1990.
All the people should be stored in allPeople.
The order in which data is displayed corresponds to the order in which is input.
Be sure the program is thread safe (works correctly with multiple threads without corrupting the data).
Use Locale.ENGLISH as the second argument for SimpleDateFormat.
Example output for the -i argument with two ids:
Washington m Apr 15 1990
Ross f Apr 25 1997
Requirements:
1. The Solution class must contain a public volatile List<Person> field called allPeople.
2. The Solution class must have a static block where two people are added to the allPeople list.
3. With the -c argument, the program must add all people with the specified arguments to the end of the allPeople list, and display the id of each of them.
4. With the -u argument, the program must update the data of the people with the specified ids in the allPeople list.
5. With the -d argument, the program must perform the logical deletion of the people with the specified ids in the allPeople list.
6. With the -i argument, the program should display data about all the people with the specified ids according to the format specified in the task.
7. The Solution class's main method must contain a switch statement based on args[0].
8. Each case label in the switch statement must have a synchronization block for allPeople.
*/
public class Solution {
public static volatile List<Person> allPeople = new ArrayList<>();
static {
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) throws ParseException {
switch (args[0]) {
case "-c":
synchronized (allPeople) {
int i = 1; // keeping track of arguments as it has unknown quantity
while (i < args.length) {
Date date1 = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH).parse(args[i+2]);
if (args[i+1].equals("m")) {
allPeople.add(Person.createMale(args[i], date1));
System.out.println(allPeople.size() - 1);
} else if (args[2].equals("f")) {
allPeople.add(Person.createFemale(args[i], date1));
System.out.println(allPeople.size() - 1);
}
i += 3 ;
}
}
break;
case "-u":
synchronized (allPeople) {
for (int i = 1; i < args.length; i = i + 4) {
final Person person = allPeople.get(Integer.parseInt(args[i])); // setting a new Person object to the values of the arg
String name = args[i+1];
String sex = args[i+2];
String bd = args[i+3];
Date date2 = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH).parse(bd);
person.setName(name);
person.setBirthDate(date2);
if (sex.equals("m")) {
person.setSex(Sex.MALE);
} else {
person.setSex(Sex.FEMALE);
}
}
}
// int y=0;
// for (Person p: allPeople) {
// System.out.println(allPeople.get(y).getName());
// y++;
break;
case "-d":
for (int i = 0; i < allPeople.size(); i++) {
if (i == Integer.parseInt(args[1])) {
allPeople.get(i).setSex(null);
allPeople.get(i).setBirthDate(null);
allPeople.get(i).setName(null);
}
}
break;
case "-i":
for (int i = 0; i < allPeople.size(); i++) {
if (i == Integer.parseInt(args[1])) {
System.out.print(allPeople.get(i).getName() + " ");
if (allPeople.get(i).getSex().equals(Sex.MALE)) {
System.out.print("m" + " ");
} else {
System.out.print("f" + " ");
}
SimpleDateFormat date3 = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
System.out.print(date3.format(allPeople.get(i).getBirthDate()));
}
}
}
}
}