Уже все отладил и проверил,но компилятор не хочет пропускать.
Не пойму, как он хочет видеть подачу аргументов
1. args[0] = "-c Пупкин м 10/12/2009";
2. args [0]= "-c";
args [1] = "Пупкин";
args[2] = "м";
args[3]= "10/12/2009";
Я сделал по первому варианту распарсив строку на аргументы и уже потом создаю новых person.
Как я понял из комментария компилятора и некоторых обсуждений компилятор ожидает второй вариант,это верно?
В Idea если написать строку из примера,что в условии,то он ее сам разобъет на аргументы.
package com.javarush.task.task17.task1710;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/*
CRUD
*/
public class Solution {
public static List<Person> allPeople = new ArrayList<Person>();
static {
allPeople.add(Person.createMale("Иванов Иван", new Date())); //сегодня родился id=0
allPeople.add(Person.createMale("Петров Петр", new Date())); //сегодня родился id=1
}
public static void main(String[] args)throws Exception {
args[0]= "-c Петров м 10/12/2000";
args[1]= "-u 0 Васечкин м 06/09/2010";
args[2]= "-i 0";
args[3]= "-d 0";
args[4]= "-i 0";
String sexs= null;
for(int i =0; i<=args.length-1;i++){
if(args[i].startsWith("-c")){
int index1= args[i].indexOf(" ")+1;
int index2= args[i].indexOf(" ", index1);
String name= args[i].substring(index1,index2);
int index3= args[i].indexOf(" ",index2+1);
String pol= args[i].substring(index2+1,index3);
String date= args[i].substring(index3+1);
SimpleDateFormat fromdformat = new SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH);
SimpleDateFormat toformat = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
Date birthday= fromdformat.parse(date);
String printdate= toformat.format(birthday);
if(pol.equals("м")){
allPeople.add(Person.createMale(name,birthday));
System.out.println((allPeople.size()-1));
}
if (pol.equals("ж")){
allPeople.add(Person.createFemale(name,birthday));
System.out.println((allPeople.size()-1));
}
}
if(args[i].startsWith("-u")){
int index1= args[i].indexOf(" ");
int index2= args[i].indexOf(" ", index1+1);
String ids= args[i].substring(index1+1,index2);
int id = Integer.parseInt(ids);
int index3= args[i].indexOf(" ",index2+1);
int index4= args[i].indexOf(" ",index3+1);
String name= args[i].substring(index2+1,index3);
String pol= args[i].substring(index3+1,index4);
String date= args[i].substring(index4+1);
SimpleDateFormat fromdformat = new SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH);
Date birthday= fromdformat.parse(date);
if(pol.equals("м")){
allPeople.set(id, Person.createMale(name,birthday));
}
if (pol.equals("ж")){
allPeople.set(id, Person.createFemale(name,birthday));
}
}
if(args[i].startsWith("-d")){
int index1= args[i].indexOf(" ");
String ids= args[i].substring(index1+1);
int id = Integer.parseInt(ids);
Person current = allPeople.get(id);
current.setName(null);
current.setSex(null);
current.setBirthDate(null);
allPeople.set(id,current);
}
if(args[i].startsWith("-i")){
int index1= args[i].indexOf(" ");
String ids= args[i].substring(index1+1);
int id = Integer.parseInt(ids);
Person person= allPeople.get(id);
String name=person.getName();
SimpleDateFormat toformat = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
Date birthdate = person.getBirthDate();
try {
String printdate = toformat.format(birthdate);
Sex sex = person.getSex();
if (sex == Sex.MALE) {
sexs = "м";
}
if (sex == Sex.FEMALE) {
sexs = "ж";
}
System.out.println(name + " " + sexs + " " + printdate);
}
catch(NullPointerException e) {
String printdate= null;
Sex sex = person.getSex();
if (sex == Sex.MALE) {
sexs = "м";
}
else if (sex == Sex.FEMALE) {
sexs = "ж";
}
else{
sexs= null;
}
System.out.println(name + " " + sexs + " " + printdate);
}
}
}
}
}