Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Collections;
- public class topFive extends PhoneBook
- {
- /**
- * Logs all the numbers, contacts, favorite 5, etc
- * @throws IOException In case no file is found for the favorite 5
- */
- public static void FavoriteFive() throws IOException
- {
- ArrayList<String> favContact = new ArrayList<String>();
- ArrayList<String> favNames = new ArrayList<String>();
- ArrayList<String> favNums = new ArrayList<String>();
- ArrayList<String> favEmail = new ArrayList<String>();
- final int fWidth = 3524;
- final int fHeight = 2440;
- FavoriteContactFrame contact4 = new FavoriteContactFrame("C:\\Users\\sword\\OneDrive\\Pictures\\Screenshots\\DatBoi.jpg", fWidth, fHeight, "Dat Boi", "(777) 777-7777"
- + "", "datboi@csulb.edu", "");
- FavoriteContactFrame contact5 = new FavoriteContactFrame("C:\\Users\\sword\\OneDrive\\Pictures\\Screenshots\\spongebobicon.jpg", fWidth, fHeight, "Spongebob", "(562) 584-5488"
- + "", "spongebobsquarepants@csulb.edu", "");
- FavoriteContactFrame contact6 = new FavoriteContactFrame("C:\\Users\\sword\\OneDrive\\Pictures\\Screenshots\\squidwardmeme.jpg", fWidth, fHeight, "Squidward", "(562) 714-8792"
- + "", "squidwardtentacles@ucrb.edu", "");
- favContact.add("Dat Boi 777-777-7777 datboi@csulb.edu");
- favContact.add("Spongebob 562-584-5488 spongebobsquarepants@csulb.edu");
- favContact.add("Squidward 562-714-8792 squidwardtentacles@ucrb.edu");
- String menu = "1. Add a contact.\n2. Remove a contact.\n3. Rearrange a contact.\n4. Display the contacts.";
- int selection = 0;
- do
- {
- System.out.println("Welcome to your favorite contacts! Please select one of the following options: \n" + menu);
- selection = Integer.parseInt(user.nextLine().trim()); //Accepting user selection
- /**
- * If selection = 1, adds a favorite contact
- */
- if(selection == 1)
- {
- while(true)
- {
- System.out.print("Please enter your name, phone number and email or enter Done to stop: ");
- favContact.add(user.nextLine());
- if(favContact.contains("Done"))
- {
- break;
- }
- }
- }
- //------------------------------------------------------------------------------------------------------------------------
- else if(selection == 2)
- /**
- * Removes the selected contact via name
- * Else, declare that the contact does not exist
- */
- {
- //Remove the contact
- //Dat Boi 777-777-7777 datboi@csulb.edu
- //Squidward 562-714-8792 squidwardtentacles@ucrb.edu
- System.out.print("Enter the name of the contact you want to remove: ");
- String remove = user.nextLine();
- if(favContact.contains(remove))
- {
- int removeIndex = favContact.indexOf(remove);
- favContact.remove(removeIndex);
- for(String delete : favContact)
- {
- System.out.println(delete);
- }
- }
- else
- {
- System.out.println("This contact does not exist.");
- }
- }
- //------------------------------------------------------------------------------------------------------------------------
- else if(selection == 3)
- /**
- * Rearrange the order of the contacts
- */
- {
- //Rearrange a contact
- //Spongebob 562-584-5488 spongebobsquarepants@csulb.edu
- System.out.print("Enter the name of the contact you want to rearrange: ");
- String firstSwap = user.nextLine();
- if(favContact.contains(firstSwap))
- {
- int index = favContact.indexOf(firstSwap);
- System.out.print("Enter the name of the contact you want to switch it with: ");
- String secondSwap = user.nextLine();
- if(favContact.contains(secondSwap))
- {
- int indexTwo = favContact.indexOf(secondSwap);
- Collections.swap(favContact, index, indexTwo);
- for(String output : favContact)
- {
- System.out.println(output);
- }
- }
- }
- }
- //------------------------------------------------------------------------------------------------------------------------
- else if(selection == 4)
- {
- while(true)
- {
- System.out.print("Enter the name of your favorite contact or Done to stop: ");
- favNames.add(user.nextLine());
- if(favNames.contains("Done"))
- {
- FavoriteContactFrame contact2 = new FavoriteContactFrame("C:\\Users\\sword\\OneDrive\\Pictures\\Screenshots\\Airalin Wallpaper.png", fWidth, fHeight, favNames.get(0), favNums.get(0)
- + "", favEmail.get(0), "");
- contact2.displayContactFrame();
- FavoriteContactFrame contact3 = new FavoriteContactFrame("C:\\Users\\sword\\OneDrive\\Pictures\\Screenshots\\Knife_Spooky.png", fWidth, fHeight, favNames.get(1), favNums.get(1)
- + "", favEmail.get(1), "");
- contact3.displayContactFrame();
- contact4.displayContactFrame();
- contact5.displayContactFrame();
- contact6.displayContactFrame();
- break;
- }
- System.out.print("Now enter their number: ");
- favNums.add(user.nextLine());
- System.out.print("Finally enter their email: ");
- favEmail.add(user.nextLine());
- }
- }
- //------------------------------------------------------------------------------------------------------------------------
- else if(selection < 1 || selection > 5)
- /**
- * A catch in case an invalid selection is made
- */
- {
- System.out.print("Invalid selection please try again: ");
- selection = Integer.parseInt(user.nextLine().trim());
- }
- /**
- * Program is gone once it hits 5
- */
- }while(selection != 5);
- /**
- * Saves data once this is done
- */
- File callLog = new File("C:\\Users\\Henry\\Desktop\\JavaFiles\\favoriteFive.txt");
- //Source : https://stackoverflow.com/questions/9961292/write-to-text-file-without-overwriting-in-java
- PrintWriter fileOUT = new PrintWriter(new FileWriter(callLog, true));
- for(String print : name){
- fileOUT.println(print);
- }
- fileOUT.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement