Advertisement
pavelperc

stage 3

Sep 3rd, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.92 KB | None | 0 0
  1. // https://stepik.org/submissions/602014/127212122?unit=178572
  2. package contacts;
  3.  
  4. import java.time.LocalDateTime;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Scanner;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. public class Main {
  12. public static void main(String[] args) {
  13. Scanner scanner = new Scanner(System.in);
  14. List<Contact> contactList = new ArrayList<>();
  15. boolean terminate = false;
  16.  
  17. while (!terminate) {
  18. System.out.print("\nEnter action (add, remove, edit, count, info, exit): > ");
  19. String action = scanner.next();
  20.  
  21. if (Actions.ADD.name().equals(action.toUpperCase())) {
  22. ContactFactory personFactory = new PersonFactory();
  23. ContactFactory organizationFactory = new OrganizationFactory();
  24.  
  25. System.out.print("Enter the type (person, organization): > ");
  26. scanner.nextLine();
  27. String typeName = scanner.nextLine();
  28.  
  29. if ("person".equals(typeName)) {
  30. Person person = (Person) personFactory.makeContact();
  31.  
  32. System.out.print("Enter the name: > ");
  33. person.setName(scanner.nextLine());
  34.  
  35. System.out.print("Enter the surname: > ");
  36. person.setSurname(scanner.nextLine());
  37.  
  38. System.out.print("Enter the birth date: > ");
  39. person.setBirthDate(scanner.nextLine());
  40.  
  41. System.out.print("Enter the gender (M, F): > ");
  42. person.setGender(scanner.nextLine());
  43.  
  44. System.out.print("Enter the number: > ");
  45. person.setNumber(scanner.nextLine());
  46.  
  47. person.setTimeCreated(LocalDateTime.now());
  48. person.setTimeEdited(LocalDateTime.now());
  49. contactList.add(person);
  50. } else if ("organization".equals(typeName)) {
  51. Organization organization = (Organization) organizationFactory.makeContact();
  52.  
  53. System.out.print("Enter the organization name: > ");
  54. organization.setName(scanner.nextLine());
  55.  
  56. System.out.print("Enter the address: > ");
  57. organization.setAddress(scanner.nextLine());
  58.  
  59. System.out.print("Enter the number: > ");
  60. organization.setNumber(scanner.nextLine());
  61.  
  62. organization.setTimeCreated(LocalDateTime.now());
  63. organization.setTimeEdited(LocalDateTime.now());
  64. contactList.add(organization);
  65. }
  66.  
  67. System.out.println("The record added.");
  68. }
  69.  
  70. if (Actions.REMOVE.name().equals(action.toUpperCase())) {
  71. if (contactList.size() == 0) {
  72. System.out.println("No records to remove!");
  73. } else {
  74. printContactList(contactList);
  75.  
  76. System.out.print("Select a record: > ");
  77. int i = scanner.nextInt();
  78. contactList.remove(i - 1);
  79. System.out.println("The record removed!");
  80. }
  81. }
  82.  
  83. if (Actions.EDIT.name().equals(action.toUpperCase())) {
  84. if (contactList.size() == 0) {
  85. System.out.println("No records to edit!");
  86. } else {
  87. printContactList(contactList);
  88.  
  89. System.out.print("Select a record: > ");
  90. int i = scanner.nextInt();
  91. Contact contact = contactList.get(i - 1);
  92.  
  93. if (contact.isPerson()) {
  94. Person person = (Person) contact;
  95. System.out.print("Select a field (name, surname, birth, gender, number): > ");
  96. String field = scanner.next();
  97.  
  98. if ("name".equals(field)) {
  99. System.out.print("Enter name: > ");
  100. person.setName(scanner.next());
  101. System.out.println("The record updated!");
  102. }
  103.  
  104. if ("surname".equals(field)) {
  105. System.out.print("Enter surname: > ");
  106. person.setSurname(scanner.next());
  107. System.out.println("The record updated!");
  108. }
  109.  
  110. if ("birth".equals(field)) {
  111. System.out.print("Enter birth date");
  112. person.setBirthDate(scanner.next());
  113. System.out.println("The record updated!");
  114. }
  115.  
  116. if ("gender".equals(field)) {
  117. System.out.print("Enter gender: > ");
  118. person.setGender(scanner.next());
  119. System.out.println("The record updated!");
  120. }
  121.  
  122. if ("number".equals(field)) {
  123. System.out.print("Enter number: > ");
  124. scanner.nextLine();
  125. String prevNumber = person.getNumber();
  126. person.setNumber(scanner.nextLine());
  127. if (!person.getNumber().equals(prevNumber) && person.hasNumber()) {
  128. System.out.println("The record updated!");
  129. }
  130. }
  131.  
  132. person.setTimeEdited(LocalDateTime.now());
  133. } else {
  134. Organization organization = (Organization) contact;
  135. System.out.print("Select field (name, address, number): > ");
  136. String field = scanner.next();
  137.  
  138. if ("name".equals(field)) {
  139. System.out.print("Enter name: > ");
  140. scanner.nextLine();
  141. organization.setName(scanner.nextLine());
  142. System.out.println("The record updated!");
  143. }
  144.  
  145. if ("address".equals(field)) {
  146. System.out.print("Enter address: > ");
  147. scanner.nextLine();
  148. organization.setAddress(scanner.nextLine());
  149. System.out.println("The record updated!");
  150. }
  151.  
  152. if ("number".equals(field)) {
  153. System.out.println("Enter number: > ");
  154. scanner.nextLine();
  155. String prevNumber = organization.getNumber();
  156. organization.setNumber(scanner.nextLine());
  157. if (!organization.getNumber().equals(prevNumber) && organization.hasNumber()) {
  158. System.out.println("The record updated!");
  159. }
  160. }
  161.  
  162. organization.setTimeEdited(LocalDateTime.now());
  163. }
  164. }
  165. }
  166.  
  167. if (Actions.COUNT.name().equals(action.toUpperCase())) {
  168. System.out.println("The phone book has " + contactList.size() + " records.");
  169. }
  170.  
  171. if (Actions.INFO.name().equals(action.toUpperCase())) {
  172. printContactList(contactList);
  173. System.out.print("Enter index to show info: > ");
  174. int i = scanner.nextInt();
  175. Contact contact = contactList.get(i - 1);
  176.  
  177. if (contact.isPerson()) {
  178. Person person = (Person) contact;
  179. System.out.println(person.toString());
  180. } else {
  181. Organization organization = (Organization) contact;
  182. System.out.println(organization.toString());
  183. }
  184. }
  185.  
  186. if (Actions.EXIT.name().equals(action.toUpperCase())) {
  187. terminate = true;
  188. }
  189. }
  190. }
  191.  
  192. private static void printContactList(List<Contact> contactList) {
  193. for (int i = 0; i < contactList.size(); i++) {
  194. Contact contact = contactList.get(i);
  195. if (contact.getClass() == Person.class) {
  196. Person person = (Person) contact;
  197. System.out.println((i + 1) + ". " + person.getName() + " " + person.getSurname());
  198. } else {
  199. Organization organization = (Organization) contact;
  200. System.out.println((i + 1) + ". " + organization.getName());
  201. }
  202. }
  203. }
  204.  
  205. enum Actions {
  206. ADD, REMOVE, EDIT, COUNT, INFO, EXIT
  207. }
  208. }
  209.  
  210. class Contact {
  211.  
  212. private String number = "";
  213. private LocalDateTime timeCreated;
  214. private LocalDateTime timeEdited;
  215. private boolean isPerson = false;
  216.  
  217. String getNumber() {
  218. return number;
  219. }
  220.  
  221. void setNumber(String number) {
  222. if (!checkNumber(number) && !hasNumber()) {
  223. System.out.println("Wrong number format!");
  224. } else if (checkNumber(number)){
  225. this.number = number;
  226. } else if (!checkNumber(number)) {
  227. this.number = "";
  228. }
  229. }
  230.  
  231. public LocalDateTime getTimeCreated() {
  232. return timeCreated;
  233. }
  234.  
  235. public void setTimeCreated(LocalDateTime timeCreated) {
  236. this.timeCreated = timeCreated;
  237. }
  238.  
  239. public LocalDateTime getTimeEdited() {
  240. return timeEdited;
  241. }
  242.  
  243. public void setTimeEdited(LocalDateTime timeEdited) {
  244. this.timeEdited = timeEdited;
  245. }
  246.  
  247. public boolean isPerson() {
  248. return isPerson;
  249. }
  250.  
  251. public void setPerson(boolean person) {
  252. isPerson = person;
  253. }
  254.  
  255. boolean hasNumber() {
  256. return number.length() != 0;
  257. }
  258.  
  259. private boolean checkNumber(String number) {
  260. Pattern pattern = Pattern.compile("([+?\\w]{2,}(\\s|-)[(]\\w{2,}[)]|\\+?[(]\\w{2,}[)](\\s|-)\\w{2,}|\\+?\\w{2,}|\\+?[(]\\w{2,}[)])((\\s|-)\\w{2,})*");
  261. Matcher matcher = pattern.matcher(number);
  262. return matcher.matches();
  263. }
  264. }
  265.  
  266. class Person extends Contact {
  267. private String name;
  268. private String surname;
  269. private String birthDate;
  270. private String gender;
  271.  
  272. private Pattern pattern;
  273. private Matcher matcher;
  274.  
  275. public String getName() {
  276. return name;
  277. }
  278.  
  279. public void setName(String name) {
  280. this.name = name;
  281. }
  282.  
  283. public String getSurname() {
  284. return surname;
  285. }
  286.  
  287. public void setSurname(String surname) {
  288. this.surname = surname;
  289. }
  290.  
  291. public String getBirthDate() {
  292. return birthDate;
  293. }
  294.  
  295. public void setBirthDate(String birthDate) {
  296. pattern = Pattern.compile("(0[1-9]|1[1-2])[-]((0|[1-2])[1-9]|3[0-1])");
  297. matcher = pattern.matcher(birthDate);
  298. if (!matcher.matches()) {
  299. this.birthDate = "[no data]";
  300. System.out.println("Bad birth date!");
  301. } else {
  302. this.birthDate = birthDate;
  303. }
  304. }
  305.  
  306. public String getGender() {
  307. return gender;
  308. }
  309.  
  310. public void setGender(String gender) {
  311. pattern = Pattern.compile("[FM]");
  312. matcher = pattern.matcher(gender);
  313. if (!matcher.matches()) {
  314. this.gender = "[no data]";
  315. System.out.println("Bad gender!");
  316. } else {
  317. this.gender = gender;
  318. }
  319. }
  320.  
  321. @Override
  322. public String toString() {
  323. return "Name: " + name + '\n' +
  324. "Surname: " + surname + '\n' +
  325. "Birth date: " + birthDate + '\n' +
  326. "Gender: " + gender + '\n' +
  327. "Number: " + getNumber() + '\n' +
  328. "Time created: " + getTimeCreated() + '\n' +
  329. "Time last edit: " + getTimeEdited() + '\n';
  330. }
  331. }
  332.  
  333. class Organization extends Contact {
  334. private String name;
  335. private String address;
  336.  
  337. public String getName() {
  338. return name;
  339. }
  340.  
  341. public void setName(String name) {
  342. this.name = name;
  343. }
  344.  
  345. public String getAddress() {
  346. return address;
  347. }
  348.  
  349. public void setAddress(String address) {
  350. this.address = address;
  351. }
  352.  
  353. @Override
  354. public String toString() {
  355. return "Organization name: " + name + '\n' +
  356. "Address: " + address + '\n' +
  357. "Number: " + getNumber() + '\n' +
  358. "Time created: " + getTimeCreated().withNano(0) + '\n' +
  359. "Time last edit: " + getTimeEdited().withNano(0) + '\n';
  360. }
  361. }
  362.  
  363. interface ContactFactory {
  364. Contact makeContact();
  365. }
  366.  
  367. class PersonFactory implements ContactFactory {
  368. @Override
  369. public Contact makeContact() {
  370. Person person = new Person();
  371. person.setPerson(true);
  372. return person;
  373. }
  374. }
  375.  
  376. class OrganizationFactory implements ContactFactory {
  377. @Override
  378. public Contact makeContact() {
  379. return new Organization();
  380. }
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement