Hello, I don't understand what is the difference between this block of code:
case "-i":
synchronized (allPeople) {
for (int i = 0; i < args.length-1; i++) {
person = allPeople.get(i);
String newFormatBirthdate = sdfOutput.format(person.getBirthDate());
System.out.println(allPeople.get(i).getName() + " " +
(person.getSex().equals("Sex.MALE") ? "m" : "f") + " " +
newFormatBirthdate);
}
}
break;
and this block of code:
case "-i":
synchronized (allPeople) {
for (int i = 1; i < args.length; i++) {
int id = Integer.parseInt(args[i]);
Person person = allPeople.get(id);
System.out.print(person.getName() + " ");
System.out.print(person.getSex().equals(Sex.MALE) ? "m " : "f ");
System.out.println(outputFormat.format(person.getBirthDate()));
}
}
break;
Why is the second one better?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
*/
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 {
// Start here
Person person;
Date birthdate;
SimpleDateFormat sdfOutput = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
SimpleDateFormat sdfInput = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH);
if (args[0]==null || args.length < 1) throw new RuntimeException();
switch (args[0]){
case "-c":
synchronized (allPeople) {
//here we check to see if there are multiple persons to be added
if (args.length > 6) {
for (int i = 1; i < args.length; i += 3) {
birthdate = sdfInput.parse(args[i + 3]);
if (args[i + 1].equals("m")) person = Person.createMale(args[i], birthdate);
else person = Person.createFemale(args[i], birthdate);
allPeople.add(person);
System.out.println(allPeople.indexOf(person));
}
} else {
//here we add only one person if there is only 1 person argument passed
birthdate = sdfInput.parse(args[3]);
if (args[2].equals("m")) person = Person.createMale(args[1], birthdate);
else person = Person.createFemale(args[1], birthdate);
allPeople.add(person);
System.out.println(allPeople.indexOf(person));
}
}
break;
case "-u":
synchronized (allPeople) {
//-u id1 name1 sex1 bd1 - this is how update is passed as argument
for (int i = 1; i < args.length; i = i + 4) {
person = allPeople.get(Integer.parseInt(args[i]));
person.setName(args[i+1]);
person.setSex(args[i+2].equals("m") ? Sex.MALE : Sex.FEMALE);
person.setBirthDate(sdfInput.parse(args[i+3]));
}
}
break;
case "-d":
synchronized (allPeople) {
//-d id1 id2 id3 id4 ...
for (int i = 1; i < args.length; i++) {
int id = Integer.parseInt(args[i]);
allPeople.get(id).setName(null);
allPeople.get(id).setSex(null);
allPeople.get(id).setBirthDate(null);
}
}
break;
case "-i":
synchronized (allPeople) {
for (int i = 0; i < args.length-1; i++) {
person = allPeople.get(i);
String newFormatBirthdate = sdfOutput.format(person.getBirthDate());
System.out.println(allPeople.get(i).getName() + " " +
(person.getSex().equals("Sex.MALE") ? "m" : "f") + " " +
newFormatBirthdate);
}
}
break;
}
}
}