<commit_before>package io;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class File_IO {

    public static String addressBook = "contactList";
    public static String contacts = "contacts.txt";

    public static void printMenu() throws IOException{
        System.out.println("1. View contacts\n2. Add a new contact\n3. Search a contact by name.\n4. Delete an existing contact.\n5. Exit.\nEnter an option (1, 2, 3, 4 or 5):");
        menuOptions();
    }
    public static void exitProgram() throws IOException{
        System.out.println("Goodbye!");
        System.exit(0);
    }
    public static void continueMenu() throws IOException{
        System.out.println("Would you like to do something else?");
        String entry = in.nextLine();
        if (entry.equalsIgnoreCase("y") || entry.equalsIgnoreCase("yes")) {
            printMenu();
        }
            exitProgram();
        }

    public static void menuOptions() throws IOException{
        String entry = in.nextLine();

        switch(entry){
            case "1":
                viewAllContacts();
                break;
            case "2":
                addContact();
                break;
            case "3":
                viewContact();
                break;
            case "4":
                deleteContact();
                break;
            case "5":
                exitProgram();
            default:
                exitProgram();
        }
    }

    public static String addContact() throws IOException {
        Path contactsPath = Paths.get(addressBook, contacts);
        List<String> people = Files.readAllLines(contactsPath);

        System.out.println("Add a new contact");
        System.out.println("Enter Contacts Name");
        String nameInput = in.nextLine();
        System.out.println("Enter Contacts Number");
        String phoneNumInput = in.nextLine();
        List<String> newContact = Arrays.asList(nameInput + " | " + phoneNumInput);

        for(String person: people){
            if(person.contains(nameInput) && person.contains(phoneNumInput)){
                System.out.println("There's already a contact named "+nameInput+". Do you want to overwrite it? (Yes/No)");
                String yesNoInput = in.nextLine();
                if(yesNoInput.equalsIgnoreCase("yes") || yesNoInput.equalsIgnoreCase("y")){
                    newContact.add(nameInput+phoneNumInput);
                      continue;
                }
                newContact.add(person);
            }
        }
        Files.write(
                Paths.get(addressBook, contacts),
                newContact,
                StandardOpenOption.APPEND
        );
        return nameInput;
    }


    public static void viewContact() throws IOException{
        Path contactsPath = Paths.get(addressBook, contacts);
        List<String> people = Files.readAllLines(contactsPath);
        System.out.println("Enter person's name you are looking for.\n");
        String displayContact = in.nextLine();<commit_msg>This commit has version of add Contact method that does not check if contact exists.<commit_after>package io;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class File_IO {

    public static String addressBook = "contactList";
    public static String contacts = "contacts.txt";

    public static void printMenu() throws IOException{
        System.out.println("1. View contacts\n2. Add a new contact\n3. Search a contact by name.\n4. Delete an existing contact.\n5. Exit.\nEnter an option (1, 2, 3, 4 or 5):");
        menuOptions();
    }
    public static void exitProgram() throws IOException{
        System.out.println("Goodbye!");
        System.exit(0);
    }
    public static void continueMenu() throws IOException{
        System.out.println("Would you like to do something else?");
        String entry = in.nextLine();
        if (entry.equalsIgnoreCase("y") || entry.equalsIgnoreCase("yes")) {
            printMenu();
        }
            exitProgram();
        }

    public static void menuOptions() throws IOException{
        String entry = in.nextLine();

        switch(entry){
            case "1":
                viewAllContacts();
                break;
            case "2":
                addContact();
                break;
            case "3":
                viewContact();
                break;
            case "4":
                deleteContact();
                break;
            case "5":
                exitProgram();
            default:
                exitProgram();
        }
    }

    public static void addContact() throws IOException {
        System.out.println("Add a new contact");
        System.out.println("Enter Contacts Name");
        String nameInput = in.nextLine();
        System.out.println("Enter Contacts Number");
        String phoneNumInput = in.nextLine();
        List<String> newContact = Arrays.asList(nameInput + " | " + phoneNumInput);

        Files.write(
            Paths.get(addressBook, contacts),
            newContact,
            StandardOpenOption.APPEND
            );
        continueMenu();
    }


    public static void viewContact() throws IOException{
        Path contactsPath = Paths.get(addressBook, contacts);
        List<String> people = Files.readAllLines(contactsPath);
        System.out.println("Enter person's name you are looking for.\n");
        String displayContact = in.nextLine();
{'fraction_non_alphanumeric': 0.1075993091537133, 'fraction_numerical': 0.005526770293609672, 'mean_word_length': 2.044689800210305, 'pattern_counts': {'":': 10, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7515749', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1460, 'n_words': 403}