Π― Π·Π½Π°Ρ, ΡΡΠΎ Ρ ΠΌΠ΅Π½Ρ ΠΌΠ½ΠΎΠ³ΠΎ ΡΡΡΠΎΠΊ ΠΊΠΎΠ΄Π°. ΠΠΎ ΠΎΠ½ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΠΎΠ²Π°ΡΡ ΡΡΠ΅Π±ΠΎΠ²Π°Π½ΠΈΡΠΌ.
package com.javarush.task.task17.task1710;
import java.text.SimpleDateFormat;
import java.util.*;
/*
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) {
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
new Solution().startCRUD();
}
private void menu() {
System.out.println("-c - Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΡΠ΅Π»ΠΎΠ²Π΅ΠΊΠ° ΠΈ ΠΎΡΠΎΠ±ΡΠ°Π·ΠΈΡΡ Π΅Π³ΠΎ id");
System.out.println("-r - Π²ΡΠ²ΠΎΠ΄ΠΈΡ Π½Π° ΡΠΊΡΠ°Π½ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΡΠ΅Π»ΠΎΠ²Π΅ΠΊΠ΅ Ρ id");
System.out.println("-u - ΠΎΠ±Π½ΠΎΠ²ΠΈΡΡ Π΄Π°Π½Π½ΡΠ΅ ΡΠ΅Π»ΠΎΠ²Π΅ΠΊΠ° Ρ Π΄Π°Π½Π½ΡΠΌ id");
System.out.println("-d - ΡΠ΄Π°Π»ΠΈΡΡ ΡΠ΅Π»ΠΎΠ²Π΅ΠΊΠ° Ρ id, Π·Π°ΠΌΠ΅Π½ΡΠ΅Ρ Π²ΡΠ΅ Π΅Π³ΠΎ Π΄Π°Π½Π½ΡΠ΅ Π½Π° null");
System.out.println("-e - Π·Π°Π²Π΅ΡΡΠΈΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ");
}
private void startCRUD() {
menu();
Scanner sc = new Scanner(System.in);
String opc;
try {
while ((opc = sc.nextLine()) != null) {
switch (opc) {
case "-c": {
System.out.println("Introduce name sex birthdate (dd/mm/yyyy)");
String name = sc.nextLine();
String sex = sc.nextLine();
String bd = sc.nextLine();
Person person = ControlMenu.createPerson(name, sex, bd);
allPeople.add(person);
System.out.println(allPeople.indexOf(person));
startCRUD();
}
case "-r": {
System.out.println("Introduce index of the person");
int index = Integer.parseInt(sc.nextLine());
Person person = ControlMenu.readPerson(index, allPeople);
if(person!=null)
System.out.println(index + ": " + person.getName() + " " + person.getSex() + " " + ControlMenu.getFormat().format(person.getBirthDate()));
else
System.out.println(index + ": null null null");
startCRUD();
}
case "-u": {
System.out.println("Introduce index of the person");
int index = Integer.parseInt(sc.nextLine());
System.out.println("Introduce name sex birthdate (dd/mm/yyyy)");
String name = sc.nextLine();
String sex = sc.nextLine();
String bd = sc.nextLine();
ControlMenu.updatePerson(index, name, sex, bd, allPeople);
startCRUD();
}
case "-d": {
System.out.println("Introduce index of the person");
int index = Integer.parseInt(sc.nextLine());
ControlMenu.deletePerson(index, allPeople);
startCRUD();
}
case "-e":{
System.exit(0);
}
default:
throw new RuntimeException();
}
}
} catch (RuntimeException e) {
System.out.println("Something went wrong");
e.printStackTrace();
startCRUD();
}
}
}