HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Address book program which saves contacts to text file

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fileaddressbookcontactstextprogramwhichsaves

Problem

```
package com.company;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import static com.company.Person.id;

public class Main {
private static Scanner in = new Scanner(System.in);
private static File file = new File("Addresses.txt");
static List people = new ArrayList<>();

public static void main(String[] args) throws IOException {
readPeopleFromFile();
showMainMenu();
}

private static void findPerson() throws IOException {
System.out.println("1. Find with name");
System.out.println("2. Find with surname");

String choice;
do {
choice = in.nextLine();
switch (choice) {
case "1":
findByName();
break;
case "2":
findBySurname();
break;
default:
System.out.print("Choose 1 or 2: ");
}
} while (!choice.equals("1") && !choice.equals("2"));
System.out.println();
showMainMenu();
}

private static void findBySurname() {
System.out.print("Enter surname: ");
String surnameToFind = in.nextLine();
int matches = 0;
for(Person person : people) {
if(person.getSurname().equals(surnameToFind)) {
System.out.println(person);
matches++;
}
}
if(matches<=0) {
System.out.println("There is no person with this surname");
}
}

private static void findByName() {
System.out.print("Enter name: ");
String nameToFind = in.nextLine();
int matches = 0;
for(Person person : people) {
if(person.getName().equals(nameToFind)) {
System.out.println(person);
matches++;
}
}
if(matches<=0) {
System.out.println("T

Solution

Advice 1

private static boolean readPeopleFromFile() { ... }


In case you or someone else is to maintain your code, it would be nicer if readPeopleFromFile would take a File object as input and return a List:

private static List readPeopleListFromFile(File file) { ... }


Advice 2

private static void addToFile(Person person) { ... }


would be better as

private static void addPersonToFile(Person person, File file) { ... }


Advice 3

private static void showMainMenu() { ... }


I would do exactly what was implied by the name of the method: show the menu; plus, I would added a distinct method for asking the user for input.

Advice 4

choice = in.nextLine();


if your write

choice = in.nextLine().trim(); // trim() removes all whitespace from the beginning and end of the string returned by in.nextLine().


you will make your input handling a little bit more robust.

Advice 5

static int id = Main.people.size();


This is suffient in your program, but in software industry it would be more reasonable to manage ID computation in the business logic (Main.java in your case).

Hope that helps.

Code Snippets

private static boolean readPeopleFromFile() { ... }
private static List<Person> readPeopleListFromFile(File file) { ... }
private static void addToFile(Person person) { ... }
private static void addPersonToFile(Person person, File file) { ... }
private static void showMainMenu() { ... }

Context

StackExchange Code Review Q#152357, answer score: 2

Revisions (0)

No revisions yet.