Hey friends,
The code is working, I tested it with all possible arguments.
However, I cannot pass the last condition, something is wrong with my switch statement and I cannot get what exactly is wrong.
Additional information is displayed on this error: "Be sure that the main method has a switch statement based on args[0]."
Please help! :)
package com.codegym.task.task17.task1711;
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 Exception {
if (args.length < 2) throw new Exception("Invalid arguments");
switch (args[0]) {
case "-c":
synchronized (allPeople) {
for (int i = 1; i < args.length; i += 3) {
String name = parseName(args[i]);
Sex sex = parseSex(args[i + 1]);
Date date = parseDate(args[i + 2]);
Person person = sex == Sex.MALE
? Person.createMale(name, date)
: Person.createFemale(name, date);
allPeople.add(person);
int id = allPeople.size() - 1;
System.out.println(id);
}
break;
}
case "-u":
synchronized (allPeople) {
for (int i = 1; i < args.length; i += 4) {
int id = parseId(args[i]);
String name = parseName(args[i + 1]);
Sex sex = parseSex(args[i + 2]);
Date date = parseDate(args[i + 3]);
Person person = sex == Sex.MALE
? Person.createMale(name, date)
: Person.createFemale(name, date);
allPeople.set(id, person);
}
break;
}
case "-d":
synchronized (allPeople) {
for (int i = 1; i < args.length; i += 1) {
int id = parseId(args[i]);
Person person = allPeople.get(id);
person.setName(null);
person.setBirthDate(null);
person.setSex(null);
}
break;
}
case "-i":
synchronized (allPeople) {
for (int i = 1; i < args.length; i += 1) {
int id = parseId(args[i]);
Person person = allPeople.get(id);
printPerson(person);
}
break;
}
default:
throw new Exception("wrong operation");
}
}
private static String parseName(String str) throws Exception {
if (str.isEmpty()) throw new Exception("Wrong name");
return str;
}
private static Date parseDate(String str) throws Exception {
String[] arr = str.split(" ");
int month = Integer.parseInt(arr[0]) - 1; // Accommodates zero-based month
int day = Integer.parseInt(arr[1]);
int year = Integer.parseInt(arr[2]) - 1900;
if (month > 12 || month < 1 || day > 31 || day < 1 || year > 9999 || year < 1) {
throw new Exception("wrong date");
}
Date date = new Date(year, month, day);
return date;
}
private static Sex parseSex(String str) throws Exception {
if (str.equals("m")) return Sex.MALE;
if (str.equals("f")) return Sex.FEMALE;
throw new Exception("wrong sex");
}
private static int parseId(String str) throws Exception {
int number = Integer.parseInt(str);
if (number < 0 || number >= allPeople.size()) throw new Exception("wrong id");
return number;
}
private static void printPerson(Person person) {
String name = person.getName();
String sex = person.getSex().toString().toLowerCase().substring(0, 1);
Date date = person.getBirthDate();
String pattern = "MMM dd yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.ENGLISH);
String dateString = simpleDateFormat.format(date);
System.out.println(name + " " + sex + " " + dateString);
}
}