All pastes #455278 Raw Edit

##java

public text v1 · immutable
·published 2007-04-24 03:06 UTC
rendered paste body
/*
 * Author:        Bradford Fisher
 * Date:          April 11, 2007
 * Project:       CS 241 (Project 1) - AddressBook Class
 * Description:   This class stores and manipulates an array of persons and their addresses.
 */

// -- Import Statements
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class AddressBook {
	// -- Fields
	private ArrayList<Person> addrList;
	
	// -- Constructors
	public AddressBook() {
		addrList = new ArrayList<Person>();
	}
	public AddressBook(String inputFile) throws IOException {
		addrList = new ArrayList<Person>();
		File file = new File(inputFile);
		Person quickPerson;
		Scanner bookFile = new Scanner(file);
		String line;
		String[] splitLine = new String[3];
		
		while (bookFile.hasNext()) {
			line = bookFile.nextLine();
			splitLine = line.split("~");
			quickPerson = new Person();
			quickPerson.setFirstName(splitLine[0]);
			quickPerson.setLastName(splitLine[1]);
			quickPerson.setAddress(splitLine[2]);
			addrList.add(quickPerson);
		}
	}
	
	// -- Accessor Methods
	private int locate(String fName, String lName) {
		boolean exists = false;
		int i = 0;
		
		if (addrList.size() != 0) {
			while (!exists && i < addrList.size()) {
				if (addrList.get(i).getFirstName().equals(fName) && addrList.get(i).getLastName().equals(lName))
					exists = true;
				else {
					exists = false;
					i++;
				}
			}
		}
		if (!exists)
			return -1;
		else
			return i;
	}
	public String findAddress(String fName, String lName) {
		if (locate(fName, lName) == -1)
			return "No address available!";
		else
			return addrList.get(locate(fName, lName)).getAddress();
	}
	public void printBook() {
		int i = 0;
		
		if (addrList.size() != 0) {
			do {
				System.out.println(addrList.get(i).toString());
				i++;
			} while (i < addrList.size());
		}
		else
			System.out.println("No entries available for printing!");
	}
	
	// -- Mutator Methods
	public boolean addPerson(Person per) {
		if (locate(per.getFirstName(), per.getLastName()) == -1) {
			addrList.add(per);
			return true;
		}
		else
			return false;
	}
	public boolean removePerson(String fName, String lName) {
		if (locate(fName, lName) == -1)
			return false;
		else {
			addrList.remove(locate(fName, lName));
			return true;
		}
	}
	public void sortBook() {
		Person tempPer;
		
		for (int i = 0; i < addrList.size()-1; i++) {
			if (addrList.get(i+1).getLastName().compareTo(addrList.get(i).getLastName()) < 0) {
				tempPer = addrList.get(i+1);
				addrList.set(i+1, addrList.get(i));
				addrList.set(i, tempPer);
			}
			if (addrList.get(i+1).getLastName().compareTo(addrList.get(i).getLastName()) == 0) {
				for (int j = 0; j < addrList.size()-1; j++) {
					if (addrList.get(j+1).getFirstName().compareTo(addrList.get(j).getFirstName()) < 0) {
						tempPer = addrList.get(j+1);
						addrList.set(j+1, addrList.get(j));
						addrList.set(j, tempPer);
					}
				}
			}
		}
	}
	
	// -- General Methods
	public void printInfo() {
		System.out.println();
		System.out.println("Author:         Bradford Fisher");
		System.out.println("Date:           April 11, 2007");
		System.out.println("Project:        CS 241 (Project) - AddressBook Class");
		System.out.println("Description:    This class interacts with the Person class to maintain an array");
		System.out.println("                of persons and their associated addresses.");
	}
	public void printMenu() {
		System.out.println();
		System.out.println("Menu (Choose from one of the following options)");
		System.out.println("Usage statements follow each item's description");
		System.out.println("-----------------------------------------------");
		System.out.println("a - add a new person (a firstname lastname address)");
		System.out.println("f - find an address (f firstname lastname)");
		System.out.println("p - print the address book (p)");
		System.out.println("r - remove a person (r firstname lastname)");
		System.out.println("s - sort the address book by name (s)");
		System.out.println("q - quit (q)");
		System.out.println();
		System.out.print("Please enter your choice here: ");
	}
	public static void main(String[] args) throws IOException {
		AddressBook book = new AddressBook();
		char charChoice;
		Person userPer;
		Scanner keyboard = new Scanner(System.in);
		String userChoice;
		String[] splitChoice = new String[4];
		
		book.printInfo();
		do {
			book.printMenu();
			userChoice = keyboard.nextLine();
			splitChoice = userChoice.split(" ", 4);
			charChoice = splitChoice[0].charAt(0);
			switch (charChoice) {
				case 'a':
					userPer = new Person(splitChoice[1], splitChoice[2], splitChoice[3]);
					book.addPerson(userPer);
					break;
				case 'f':
					System.out.println(book.findAddress(splitChoice[1], splitChoice[2]));
					break;
				case 'p':
					book.printBook();
					break;
				case 'r':
					book.removePerson(splitChoice[1], splitChoice[2]);
					break;
				case 's':
					book.sortBook();
					break;
				default:
					System.out.println("The choice you have entered is not available!");
			}
		} while (charChoice != 'q');
	}
}