Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- import java.util.Comparator;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.time.LocalDateTime;
- /**
- * Phonebook extends the Hub
- * This will be the phonebook that can add, edit, display, and delete contacts.
- */
- public class PhoneBook extends Hub
- {
- static Scanner user = new Scanner(System.in);
- static ArrayList<String> name = new ArrayList<String>();
- // You should eliminate the parallel array lists and instead create a class called Contact or Entry
- // You can then create/manipulate an array list of Contact objects (private instance variable not a static one) instead
- public static void PhoneBookMenu() {
- /**
- * This is the menu we can select options from.
- */
- String menu = "1. Add a contact.\n2. Edit a contact.\n3. Display contents in alphabetical order.\n4. Terminate.";
- int selection = 0;
- do{
- /**
- * Looping while selection is not 4
- */
- System.out.println("Welcome to your phone book! Select an item from the following options: \n" + menu);
- selection = Integer.parseInt(user.nextLine().trim()); //Accepting user selection
- if(selection == 1)
- {
- while(true)
- {
- /**
- * Input names and numbers to add
- */
- System.out.print("Please enter your name and phone number or enter Done to stop: ");
- name.add(user.nextLine());
- if(name.contains("Done"))
- {
- break;
- //name.remove("Done");
- //How to remove this?
- }
- }
- }
- else if(selection == 2)
- {
- //Edit a contact
- System.out.print("Enter the name and phone number of the contact you want to change: ");
- String input = user.nextLine();
- if(name.contains(input))
- /**
- * Edits the input by removing current one and redoing it
- */
- {
- int index = name.indexOf(input);
- System.out.print("What would you like to change it to? ");
- String newInput = user.nextLine().trim();
- name.set(index, newInput);
- }
- }
- else if(selection == 3)
- {
- /**
- * Displays contacts in alphabetical order
- * Removes the "Done" statements made
- */
- //Display contacts in Alphabetical order
- //Arrays.sort(ArrayList name)
- name.remove("Done");
- Collections.sort(name);
- for(int i = 0; i < name.size(); i++){
- System.out.println(name.get(i));
- }
- }
- else if(selection < 1 || selection > 4)
- {
- /**
- * Gives an error message
- * Then asks for another input
- */
- System.out.print("Invalid selection please try again: ");
- selection = Integer.parseInt(user.nextLine().trim());
- }
- /**
- * A 4 selection terminated this
- */
- }while(selection != 4);
- /**
- * Creates a new call log file
- */
- File callLog = new File("C:\\Users\\Henry\\Desktop\\JavaFiles\\log.txt");
- //Source : https://stackoverflow.com/questions/9961292/write-to-text-file-without-overwriting-in-java
- PrintWriter fileOUT;
- /**
- * Makes a new file
- * @catch IOException catches if a file cannot be made
- */
- try {
- fileOUT = new PrintWriter(new FileWriter(callLog, true));
- for(String print : name){
- fileOUT.println(print);
- }
- fileOUT.close();
- } catch (IOException e)
- {
- System.out.println("File does not exist.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement