Required to create a phone book. One of the options that baffles me:

When the user selects "H", the application requires entering the name, phone and city (optional - that is, you can not enter). The application automatically generates an ID for the new entry. Duplicate entries by name and number are not allowed. Java console application.

Please give ideas in implementation.

Closed due to the fact that the question is too general for the participants zRrr , aleksandr barakin , user194374, Kromster , enzo 13 Jun '16 at 15:54 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Well, where doubles are not allowed immediately remember about Set and Map with their implementation of HashSet and HashMap. The phone book can be organized as a set of phone numbers or a map, whose keys will be phone numbers, and the values ​​will be mappings with contact data. - DimXenon
  • So what's the problem? Can't count, doesn't parse / search? - Chubatiy
  • The Problem of Generating IDs in CSV - Alexander Dermenzhi
  • Isn't the phone number unique? Why else by name? In addition, one person has the right to have several rooms. Also, the phone number can be used as an ID, you just need to bring all the numbers to a common format. - Igor Kudryashov

1 answer 1

I decided this way:

private static int getLastColumnValue() { try { FileReader fr = new FileReader("myPhoneBook.csv"); LineNumberReader lnr = new LineNumberReader(fr); int linenumber = 0; while (lnr.readLine() != null) { linenumber++; } lnr.close(); return linenumber++; } catch (Exception e) { System.out.println(e.getMessage()); } return Integer.parseInt(null); } 

Further I deliver it csvOutput.write(new String(String.valueOf(getLastColumnValue()))); Used OpenCSV library