add phone book assignment from WCC CS 145 Spring 2019.
This commit is contained in:
parent
d738aedc12
commit
63d3de5191
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
*
|
||||
* Matt Jensen
|
||||
* CS 145, Spring 2019
|
||||
* Assignment X
|
||||
* 5/15/19
|
||||
*
|
||||
*/
|
||||
|
||||
// stores telephone nodes
|
||||
import java.util.*;
|
||||
|
||||
public class PhoneBook {
|
||||
|
||||
private String name;
|
||||
private TelephoneNode front;
|
||||
private TelephoneNode back;
|
||||
private int size;
|
||||
|
||||
public PhoneBook( String name ) {
|
||||
this.name = name;
|
||||
this.size = 0;
|
||||
}
|
||||
public void add(TelephoneNode node) {
|
||||
this.add(this.size, node);
|
||||
}
|
||||
public void add( int index, TelephoneNode node ) {
|
||||
if( index == 0 ) {
|
||||
node.next = this.front;
|
||||
this.front = node;
|
||||
} else {
|
||||
TelephoneNode nodeBeforeIndex = this.get( index - 1 );
|
||||
//TelephoneNode nodeAtIndex = this.get( index );
|
||||
//node.next = nodeAtIndex.next;
|
||||
nodeBeforeIndex.next = node;
|
||||
}
|
||||
this.size++;
|
||||
}
|
||||
public TelephoneNode get(int index) {
|
||||
TelephoneNode currentNode = this.front;
|
||||
for( int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode; // change back.next from null to node
|
||||
}
|
||||
public int search(String name) {
|
||||
TelephoneNode currentNode = this.front;
|
||||
name = name.toLowerCase();
|
||||
String nodeName;
|
||||
for( int i = 0; i < this.size; i++) {
|
||||
nodeName = currentNode.getName().toLowerCase();
|
||||
if(nodeName.indexOf(name) >= 0) {
|
||||
return i;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public void transfer(int index, PhoneBook destination){
|
||||
TelephoneNode node = this.get(index);
|
||||
this.remove(index);
|
||||
destination.add(node);
|
||||
}
|
||||
public void remove(int index) {
|
||||
if( index == 0 ) {
|
||||
this.front = this.front.next;
|
||||
} else {
|
||||
TelephoneNode nodeBeforeIndex = this.get( index - 1 );
|
||||
TelephoneNode nodeAtIndex = this.get( index );
|
||||
nodeBeforeIndex.next = nodeAtIndex.next;
|
||||
}
|
||||
}
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
public String toString() {
|
||||
String string = this.getName() + "\n";
|
||||
TelephoneNode currentNode = this.front;
|
||||
while( currentNode != null ) {
|
||||
string += currentNode.toString() + "\n";
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return string;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
*
|
||||
* Matt Jensen
|
||||
* CS 145, Spring 2019
|
||||
* Assignment X
|
||||
* 5/15/19
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.awt.*;
|
||||
import java.io.*;
|
||||
|
||||
public class PhoneBookClient {
|
||||
|
||||
// holds the clients phonebooks
|
||||
private static ArrayList<PhoneBook> phonebooks = new ArrayList<PhoneBook>();
|
||||
|
||||
// controls the command prompt and manages class relationships.
|
||||
public static void main( String[] args ) {
|
||||
//seed();
|
||||
Scanner console = new Scanner( System.in );
|
||||
Prompt.intro();
|
||||
char option = Prompt.forOption(console);
|
||||
String name;
|
||||
PhoneBook phonebook = null;
|
||||
TelephoneNode currentNode = null;
|
||||
String[] data;
|
||||
int index;
|
||||
while(option != 'q') {
|
||||
if( option == 'c') {
|
||||
name = Prompt.forString(console);
|
||||
System.out.println(name);
|
||||
PhoneBook newPhonebook = new PhoneBook( name );
|
||||
phonebooks.add(newPhonebook);
|
||||
} else if( phonebooks.size() < 1 ) {
|
||||
System.out.println("No phonebooks yet.");
|
||||
System.out.println();
|
||||
option = Prompt.forOption(console);
|
||||
continue;
|
||||
}
|
||||
if( option == 'a' ) {
|
||||
phonebook = phonebooks.get(Prompt.forPhoneBookIndexOf(phonebooks, console));
|
||||
data = new String[3];
|
||||
TelephoneNode newNode = new TelephoneNode(Prompt.forTelephoneNodeData(data, console));
|
||||
phonebook.add(newNode);
|
||||
}
|
||||
if( option == 'r' ) {
|
||||
index = Prompt.forPhoneBookIndexOf(phonebooks, console);
|
||||
if( index != -1 ) {
|
||||
phonebook = phonebooks.get(index);
|
||||
}
|
||||
index = Prompt.forTelephoneNodeIndexOf(phonebook, console);
|
||||
if( index != -1 ) {
|
||||
phonebook.remove(index);
|
||||
}
|
||||
}
|
||||
if( option == 'm' ) {
|
||||
index = Prompt.forPhoneBookIndexOf(phonebooks, console);
|
||||
if( index != -1 ) {
|
||||
phonebook = phonebooks.get(index);
|
||||
index = Prompt.forTelephoneNodeIndexOf(phonebook, console);
|
||||
if( index != -1 ) {
|
||||
currentNode = phonebook.get(index);
|
||||
data = new String[3];
|
||||
data = Prompt.forTelephoneNodeData(data, console);
|
||||
currentNode.setAll(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
if( option == 'p' ) {
|
||||
index = Prompt.forPhoneBookIndexOf(phonebooks, console);
|
||||
if( index != -1 ) {
|
||||
phonebook = phonebooks.get(index);
|
||||
}
|
||||
System.out.println("PhoneBook:");
|
||||
System.out.println(phonebook);
|
||||
}
|
||||
if( option == 's' ) {
|
||||
index = Prompt.forPhoneBookIndexOf(phonebooks, console);
|
||||
if( index != -1 ) {
|
||||
phonebook = phonebooks.get(index);
|
||||
index = Prompt.forTelephoneNodeIndexOf(phonebook, console);
|
||||
if( index != -1 ) {
|
||||
currentNode = phonebook.get(index);
|
||||
System.out.println("Entry Found:");
|
||||
System.out.println(currentNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
if( option == 't' ) {
|
||||
index = Prompt.forPhoneBookIndexOf(phonebooks, console);
|
||||
if( index != -1 ) {
|
||||
PhoneBook fromPhoneBook = phonebooks.get(index);
|
||||
index = Prompt.forTelephoneNodeIndexOf(fromPhoneBook, console);
|
||||
if( index != -1 ) {
|
||||
int nodeIndex = index;
|
||||
currentNode = fromPhoneBook.get(index);
|
||||
System.out.println("Destination:");
|
||||
index = Prompt.forPhoneBookIndexOf(phonebooks, console);
|
||||
PhoneBook toPhoneBook = phonebooks.get(index);
|
||||
fromPhoneBook.transfer(nodeIndex, toPhoneBook);
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
option = Prompt.forOption(console);
|
||||
}
|
||||
System.out.println("Goodbye!");
|
||||
return;
|
||||
}
|
||||
private static void seed() {
|
||||
PhoneBook book = new PhoneBook("Seattle");
|
||||
|
||||
String[] phoneName = new String[]{"Matt", "555555555", "1234 High St."};
|
||||
book.add(new TelephoneNode(phoneName));
|
||||
|
||||
phoneName = new String[]{"Other", "2525252525", "5555 Main"};
|
||||
book.add(new TelephoneNode(phoneName));
|
||||
|
||||
phoneName = new String[]{"And Another", "111111111", "5555 Main"};
|
||||
book.add(new TelephoneNode(phoneName));
|
||||
|
||||
phonebooks.add(book);
|
||||
|
||||
book = new PhoneBook("Bellingham");
|
||||
phoneName = new String[]{"Another", "000000000", "5555 Main"};
|
||||
book.add(new TelephoneNode(phoneName));
|
||||
|
||||
phonebooks.add(book);
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
*
|
||||
* Matt Jensen
|
||||
* CS 145, Spring 2019
|
||||
* Assignment X
|
||||
* 5/15/19
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.awt.*;
|
||||
import java.io.*;
|
||||
|
||||
public class Prompt {
|
||||
|
||||
private static String[] options = {"Create Directory", "Add Telephone to Directory", "Remove Telephone from Directory", "Modify a Telephone entry", "Tranfer a Telephone to New Directory", "Search a Directory", "Print Directory", "Quit"};
|
||||
private static String[] values = {"c", "a", "r", "m", "t", "s", "p", "q"};
|
||||
private static boolean DEBUG = false;
|
||||
|
||||
// print the introduction to the program on start up.
|
||||
public static void intro() {
|
||||
System.out.println("Welcome to my word search generator");
|
||||
System.out.println("This program allows you to generate your own word search puzzle");
|
||||
}
|
||||
public static TelephoneNode forTelephoneNodeInPhoneBook(String name, PhoneBook phonebook, Scanner console) {
|
||||
return phonebook.get(0);
|
||||
}
|
||||
|
||||
|
||||
// prompts from option input
|
||||
// rejects options outside of class variable
|
||||
public static char forOption( Scanner console ) {
|
||||
String question = getOptionPrompt();
|
||||
String input = prompt(question, console);
|
||||
while( ! isOption(input) ) {
|
||||
System.out.println("Unrecognized input.");
|
||||
input = prompt(question, console);
|
||||
}
|
||||
return getOption(input);
|
||||
}
|
||||
// prompts for string
|
||||
public static String forString( Scanner console ) {
|
||||
String input = prompt("City?", console);
|
||||
return getString(input);
|
||||
}
|
||||
// prompts for telephonenode data
|
||||
public static String[] forTelephoneNodeData( String[] data, Scanner console ) {
|
||||
data[0] = getString(prompt("Name?", console));
|
||||
data[1] = getString(prompt("Phone?", console));
|
||||
data[2] = getString(prompt("Address?", console));
|
||||
return data;
|
||||
}
|
||||
// prompts for telephonenode data
|
||||
public static int forTelephoneNodeIndexOf( PhoneBook phonebook, Scanner console ) {
|
||||
String input = prompt("Which Telephone Entry?", console);
|
||||
return phonebook.search(input);
|
||||
}
|
||||
|
||||
// prompts for PhoneBook Name
|
||||
public static int forPhoneBookIndexOf(ArrayList<PhoneBook> phonebooks, Scanner console ) {
|
||||
String input = prompt("Which Phonebook?", console);
|
||||
return getPhoneBookIndex(phonebooks, input);
|
||||
}
|
||||
|
||||
public static boolean hasPhoneBookNamed(ArrayList<PhoneBook> phonebooks, String name) {
|
||||
int index = 0;
|
||||
PhoneBook current = phonebooks.get(index);
|
||||
for(PhoneBook phonebook : phonebooks) {
|
||||
if(index != 0) {
|
||||
current = phonebooks.get(index);
|
||||
}
|
||||
if(current.getName().indexOf(name.toLowerCase()) >= 0) {
|
||||
return true;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static int getPhoneBookIndex(ArrayList<PhoneBook> phonebooks, String name) {
|
||||
name = name.toLowerCase();
|
||||
int index = 0;
|
||||
String currentName = "";
|
||||
PhoneBook current = phonebooks.get(index);
|
||||
for(PhoneBook phonebook : phonebooks) {
|
||||
if(index != 0) {
|
||||
current = phonebooks.get(index);
|
||||
}
|
||||
currentName = current.getName().toLowerCase();
|
||||
if(currentName.indexOf(name) >= 0) {
|
||||
return index;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// validates if character is an option
|
||||
private static boolean isOption(String input) {
|
||||
for(int i = 0; i < values.length; i++) {
|
||||
if( input.startsWith(values[i]) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static String getString(String input) {
|
||||
return input;
|
||||
}
|
||||
// return first character of input for options.
|
||||
private static char getOption(String input) {
|
||||
return input.charAt(0);
|
||||
}
|
||||
private static String prompt(String prompt, Scanner console) {
|
||||
System.out.println(prompt);
|
||||
String response = console.nextLine();
|
||||
System.out.println();
|
||||
return response;
|
||||
}
|
||||
|
||||
// returns all the possible options
|
||||
private static String getOptionPrompt() {
|
||||
String prompt = "Please select an option below:\n";
|
||||
for( int i = 0; i < options.length; i++ ) {
|
||||
prompt += options[i] + " (" + values[i] + ")\n";
|
||||
}
|
||||
return prompt;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
# Phonebook
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
*
|
||||
* Matt Jensen
|
||||
* CS 145, Spring 2019
|
||||
* Assignment X
|
||||
* 5/15/19
|
||||
*
|
||||
*/
|
||||
|
||||
// an individual telephone in a phone book.
|
||||
public class TelephoneNode {
|
||||
|
||||
private String name;
|
||||
private String number;
|
||||
private String address;
|
||||
|
||||
public TelephoneNode next;
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public TelephoneNode() {
|
||||
this(null, null);
|
||||
}
|
||||
public void setAll(String[] data){
|
||||
this.setData(data);
|
||||
}
|
||||
|
||||
public TelephoneNode(String[] data, TelephoneNode next) {
|
||||
if( data != null ) {
|
||||
this.setData(data);
|
||||
}
|
||||
this.next = next;
|
||||
}
|
||||
public TelephoneNode(String[] data) {
|
||||
this(data, null);
|
||||
}
|
||||
private void setData(String[] data) {
|
||||
this.name = data[0];
|
||||
this.number = data[1];
|
||||
this.address = data[2];
|
||||
}
|
||||
public String toString() {
|
||||
return this.name + " " + this.address + " " + this.number;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue