Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package classIsInSession;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Facebook{
- ArrayList<FacebookUser> fbArray= new ArrayList<FacebookUser>();
- transient Scanner myScan=new Scanner(System.in);
- transient FacebookUser currentUser= new FacebookUser(null, null);
- Facebook fbObject;
- ArrayList<FacebookUser> hugeFriends=new ArrayList<FacebookUser>(); //for getRecommendations()
- int r=0; //for getRecommendations()
- ArrayList<FacebookUser> groupFriends = new ArrayList<FacebookUser>(); //also for getR...
- FacebookUser split = new FacebookUser(null, null); //must i keep going? (getR...)
- public Facebook() {
- // FacebookUser whatUser=new FacebookUser("auspicious","theOne");
- // hugeFriends.add(whatUser);
- }
- public void quit() {
- serialize(); //stores data
- System.exit(0);
- }
- public void serialize(){ //writes fbArray to a file
- try{
- ObjectOutputStream ObjectOut= new ObjectOutputStream(new FileOutputStream("notFacebookUsers.dat", true));
- ObjectOut.writeObject(fbObject);
- ObjectOut.close();
- }catch(IOException ioe){
- System.out.println("PC: I caught an IOException @serialize method!");
- ioe.printStackTrace();
- }
- }
- public void deSerialize(){ //puts info from file into fbObject
- try {
- ObjectInputStream ObjectIn= new ObjectInputStream(new FileInputStream("notFacebookUsers.dat"));
- this.fbObject = (Facebook) ObjectIn.readObject();
- ObjectIn.close();
- } catch (ClassNotFoundException e1) {
- System.out.print("PC: Class not found.");
- } catch (FileNotFoundException e) {
- System.out.print("PC: File not found.");
- } catch(IOException ioe){
- System.out.println("PC: I caught an IOException @deSerialize()!");
- }
- }
- //ignore some code until driver
- public void listUsers() {
- System.out.println("\tList Of Users");
- System.out.println(fbArray.toString());
- }
- public void addUser() {
- String username;
- String password="";
- String passwordHint="";
- System.out.println("Now adding a new user: ");
- System.out.println("Enter the new username: ");
- username=myScan.nextLine();
- String y = fbArray.toString();
- if (y.contains(username)) {
- System.out.println("User already exists.");
- }
- else {
- System.out.println("Enter the new password: ");
- password=myScan.nextLine();
- System.out.println("Enter the new password hint: ");
- passwordHint=myScan.nextLine();
- FacebookUser user=new FacebookUser(username, password);
- user.setPasswordHint(passwordHint);
- System.out.println("...\nCreated new user: "+user.getUsername()+", Password: "+user.getPassword()+", Hint: "+user.getPasswordHint());
- System.out.println("Please save this information for your future.\n");
- fbArray.add(user);
- }
- }
- public void deleteUser() {
- System.out.println("Enter the username of the account you want to delete: ");
- String username=myScan.nextLine();
- for(int i=0;i<=fbArray.size();i++) {
- String y = fbArray.get(i).getUsername();
- if(!y.equals(username)) { }//do nothing until after for loop
- if(y.equals(username)) {
- System.out.println("Enter your password: ");
- String password=myScan.nextLine();
- Boolean k=fbArray.get(i).checkPassword(password);
- if(k) {
- fbArray.remove(i);
- System.out.println("User successfully removed.");
- Driver.menu();
- }
- }
- }
- System.out.println("That username doesn't exist.");
- }
- public void getPasswordHint() {
- System.out.println("Enter your username for password help: ");
- String username=myScan.nextLine();
- for(int i=0;i<=fbArray.size();i++) { //checks each user name
- String y = fbArray.get(i).getUsername();
- if (y.equals(username)) { //if the username exists
- fbArray.get(i).getPasswordHelp();
- Driver.menu();
- } if (!y.equals(username)) { } //do nothing until after for loop
- }
- System.out.println("That username doesn't exist.");
- }
- ==============================================================================
- DRIVER
- public class Driver {
- static Scanner myScan=new Scanner(System.in);
- static Facebook theOne=new Facebook();
- public static void main(String[] args) {
- theOne.deSerialize();
- menu();
- }
- public static void menu() {
- System.out.println("\nMenu");
- System.out.println("\t1. List Users");
- System.out.println("\t2. Add a user");
- System.out.println("\t3. Delete a user");
- System.out.println("\t4. Get password hint");
- System.out.println("\t5. Friend Someone");
- System.out.println("\t6. De-Friend Somone");
- System.out.println("\t7. List Friends");
- System.out.println("\t8. Recommend New Friends");
- System.out.println("\t9. Quit");
- try {
- int choice=0;
- System.out.println("\nWhat would you like to do?(1-9)");
- choice=myScan.nextInt();
- myScan.nextLine();
- switch (choice){
- case 0: System.out.println("Invalid choice, please try again.\n");
- System.exit(0);
- break;
- case 1:
- theOne.listUsers();
- break;
- case 2:
- theOne.addUser();
- break;
- case 3:
- theOne.deleteUser();
- break;
- case 4:
- theOne.getPasswordHint();
- break;
- case 5:
- theOne.addFriend();
- break;
- case 6:
- theOne.removeFriend();
- break;
- case 7:
- theOne.listFriends();
- break;
- case 8:
- theOne.recommendFriends();
- break;
- case 9:
- System.out.println("Goodbye.");
- myScan.close();
- theOne.quit(); //saves users to file and closes
- default: System.out.println("Invalid choice, please try again.\n");
- break;
- }
- }
- catch(InputMismatchException ex){
- System.out.println("Invalid choice, please enter an integer.\n");
- }
- Driver.menu();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement