I'm in the process of independently learning Generics as a way to build on a college assignment for Binary Search Tree. I've completed this program without Generics first and now I've converted over but I wanted to get some peer review. It seems to be working well and I've run a few tests already.
Would you do anything differently and why?
My main():
public class Assign3 {
public static void main(String[] args){
String choice = new String();
Scanner in = new Scanner(System.in);
Contact newContact;
BinaryTree<Contact> tree = new BinaryTree<Contact>();
while(!choice.equals("7") ){
System.out.println("\nPlease select option:\n1: Display the Tree\n2: Add to the List\n3: Add from a File\n4: Save to a File\n5: Determine if a Person is in the List\n6: List out who calls whom\n7: To Exit");
choice = in.next(); in.nextLine();
switch(choice){
case "1":
tree.displayInOrder();
break;
case "2":
newContact = new Contact();
if(newContact.addContact(in, false))
tree.insertInTree(newContact);
break;
case "3":
System.out.println("Enter name of file to write to: ");
Scanner f = readFile(in.next());
if(f!=null){
while(f.hasNext()) {
newContact = new Contact();
if(newContact.addContact(f, true))
tree.insertInTree(newContact);
else System.out.println("Unable to add contact from file. Proceeding to next contact..");
}
f.close();
}
break;
case "4":
System.out.println("Specify a filename to save: ");
FileWriter o = writeFile(in.next());
if(o != null)
writeToFile(o, tree.root);
try {
o.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case "5":
newContact = new Contact();
if(newContact.addContact(in, false))
tree.searchTree(newContact);
break;
case "6":
if(tree.root != null)
callWhom(tree.root);
break;
case "7":
System.out.println("Exiting…");
break;
default:
System.out.println("Invalid input... please try again.");
break;
}
}
}
public static Scanner readFile(String fileName){
File file = new File(fileName);
Scanner rF = null;
try {
rF = new Scanner(file);
return rF;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("File does not exist..");
return null;
}
}
public static FileWriter writeFile(String fileName){
FileWriter wF = null;
try {
wF = new FileWriter(fileName);
return wF;
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Unable to write file..");
return null;
}
}
public static void callWhom(BinaryTreeNode<Contact> subRoot){
System.out.print(subRoot.getData().getName() + " calls ");
if(subRoot.getLeft() != null){
System.out.print(subRoot.getLeft().getData().getName());
if(subRoot.getRight() != null)
System.out.println(" and " + subRoot.getRight().getData().getName());
else
System.out.println();
}
else if(subRoot.getRight() != null)
System.out.println(subRoot.getRight().getData().getName());
else
System.out.println("No Contact");
if(subRoot.getLeft() != null)
callWhom(subRoot.getLeft());
if(subRoot.getRight() != null)
callWhom(subRoot.getRight());
}
public static void writeToFile(FileWriter o, BinaryTreeNode<Contact> subRoot){
try {
if(subRoot.getLeft() != null)
writeToFile (o, subRoot.getLeft());
o.append(subRoot.getData().getName() + "\n" + subRoot.getData().getPhone() + "\n");
if(subRoot.getRight() != null)
writeToFile (o, subRoot.getRight());
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
My BT class:
public class BinaryTree<F extends Comparable<F>> {
BinaryTreeNode<F> root = null;
public boolean insertInTree (F data) {
BinaryTreeNode<F> item = new BinaryTreeNode<F>(data);
if (root == null)
root = item;
else
return root.insert(data);
return true;
}
public void displayInOrder () {
System.out.println("Phone List");
if (root == null){
System.out.println("List is Empty");
return;
}
else
displayInOrder (root);
}
private void displayInOrder (BinaryTreeNode<F> subRoot){
if(subRoot.getLeft() != null)
displayInOrder (subRoot.getLeft());
System.out.println(subRoot.getData() + " ");
if(subRoot.getRight() != null)
displayInOrder (subRoot.getRight());
}
public boolean searchTree(F temp){
if (root == null)
return false;
else
return root.search(temp);
}
}
My Node class:
public class BinaryTreeNode<F extends Comparable<F>> {
private F data;
private BinaryTreeNode<F> left, right;
public BinaryTreeNode() {
this(null);
}
public BinaryTreeNode(F data) {
left = null;
right = null;
this.data = data;
}
public F getData() {
return data;
}
public void setValue(F value) {
this.data = value;
}
public BinaryTreeNode<F> getLeft() {
return left;
}
public void setLeft(BinaryTreeNode<F> left) {
this.left = left;
}
public BinaryTreeNode<F> getRight() {
return right;
}
public void setRight(BinaryTreeNode<F> right) {
this.right = right;
}
public boolean insert (F newData) {
if (data.compareTo(newData) < 0) {
if (left == null){
left = new BinaryTreeNode<F>(newData);
return true;
}
else
return left.insert(newData);
} else if (this.data.compareTo(newData) > 0) {
if (this.right == null){
this.right = new BinaryTreeNode<F>(newData);
return true;
}
else
return right.insert(newData);
} else
System.out.println("Duplicate - not adding " + newData);
return false;
}
public boolean search(F findData){
if(data.compareTo(findData) < 0){
if(left == null){
System.out.println("Contact not in List");
}
else
left.search(findData);
}
else if(data.compareTo(findData) > 0){
if(right == null)
System.out.println("Contact not in List");
else
right.search(findData);
}
else{
System.out.println("Contact found in List");
return true;
}
return false;
}
}
My Contact class:
public class Contact implements Comparable<Contact> {
private String phone;
private String name;
public Contact(){
phone = null;
name = null;
}
/**
* Reads a valid date from the input provided
* @param in Specifies the input to retrieve the date from
* @param fromFile Boolean to notify the method if data is being read from a file
* @return <code>true</code> if the method succeeds, <code>false</code> otherwise
*/
public boolean addContact(Scanner in, boolean fromFile){
if(!fromFile)
System.out.print("Enter name of contact: ");
if(in.hasNextLine()){
name = in.nextLine();
}
if(!fromFile)
System.out.print("Enter phone number for contact: ");
if(in.hasNextLine()){
phone = in.nextLine();
while(!Pattern.matches("^(\\d{1})?[\\(-]??\\d{3}\\)?[\\s-]?\\d{3}[\\s-]?\\d{4}$", phone) && !fromFile){
System.out.println("Invalid number, please try again.. Example: 613-333-3333, 1(613)333-3333, 6133333333, (613) 444 5555");
phone = in.nextLine();
}
if(!Pattern.matches("^(\\d{1})?[\\(-]??\\d{3}\\)?[\\s-]?\\d{3}[\\s-]?\\d{4}$", phone) && fromFile){
System.out.println("Invalid telephone number");
return false;
}
phone = phone.replaceAll("[\\D]", "");
if(phone.length() == 10)
phone = phone.replaceFirst("(\\d{3})(\\d{3})(\\d{4})", "1-$1-$2-$3");
else if(phone.length() == 11)
phone = phone.replaceFirst("(\\d{1})(\\d{3})(\\d{3})(\\d{4})", "$1-$2-$3-$4");
}
return true;
}
/**
* Prints the contact name and phone
* @return String that represents the contact stored in this object.
*/
public String toString(){
return name + ": " + phone;
}
/**
* The method compares the Contact name. If the names are the same, the phone numbers are compared.
* @return int value representing the comparison between two object's attribute (==0 means the objects attribute are the same)
*/
@Override
public int compareTo(Contact o) {
// TODO Auto-generated method stub
if(o.name.toUpperCase().compareTo(this.name.toUpperCase()) == 0){
return o.phone.toUpperCase().compareTo(this.phone.toUpperCase());
}
return o.name.toUpperCase().compareTo(this.name.toUpperCase());
}
/**
* The function is used to retrieve the Contact name.
* @return String that represents the Contact name
*/
public String getName(){
return name;
}
/**
* The function is used to retrieve the Contact phone number.
* @return String that represents the Contact phone number
*/
public String getPhone() {
// TODO Auto-generated method stub
return phone;
}
}
I'm a student and welcome all feedback but please give details.