Java programmers a programming excercise

Ngereja

JF-Expert Member
Feb 27, 2007
798
333
Here is a problem to solve, how gona you tackle it?
There are seven classes of the package PhoneBookOscar as follows

1. Company.java
2. Contact.java
3. ContactExtended.java
4. Item.java
5. Keyboard.java
6. Main.java
7. PhoneBook.java

The problem is:
1. Extend the package PhoneBookOscar adding new characteristics (address,email,age) to class Company and Contacts.
2. Extend Item interface in order to have a new method for each new attribute.
3. Search each attribute by its address

The code snippet for the 7 classes are attached below:

1. Company class:

package phonebookoscar;

public class Company implements Item {
private String name;
private String phone;

public Company() {

}

public Company(String name, String phone) {
this.name = name;
this.phone = phone;
}

public String getName() {
return name;
}

public String getPhone() {
return phone;
}

public String getSurname() {
return "";
}
}

2. Contact class:

package phonebookoscar;

public class Contact implements Item {
private String name = null;
private String surname = null;
private String phone = null;

public Contact() {};

public Contact(String name, String surname, String phone) {
this.name = name;
this.surname = surname;
this.phone = phone;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}
}

3. ContactExtended class

package phonebookoscar;

public class ContactExtended extends Contact{
private String email = null;
private int age;

public ContactExtended() {
super();
}

public ContactExtended(String name, String surname, String phone, int age, String email){
super(name, surname, phone);
this.email = email;
this.age = age;
}
}

4. Item class:

package phonebookoscar;

public interface Item {
public String getName();
public String getSurname();
public String getPhone();
}

5. Keyboard class:

ackage phonebookoscar;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Keyboard {
private static final BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));

public static String readString() {
String line = null;

try {
line = keyboard.readLine();
}
catch(IOException e) {
show("Error reading a string of characters.");
}

return line;
}

public static int readInteger() {
int integer = 0;
String line = readString();

if(line != null) integer = Integer.parseInt(line);

return integer;
}

public static float readFloat() {
float real = 0.0f;
String line = readString();

if(line != null) real = Float.parseFloat(line);

return real;
}

public static double readDouble() {
double real = 0.0;
String line = readString();

if(line != null) real = Double.parseDouble(line);

return real;
}

private static void show(String line) {
System.err.println(line);
}
}

6. Main class:

package phonebookoscar;

public class Main {
private static final int EXIT = 0;
private static final int ADD = 1;
private static final int LIST = 2;
private static final int SEARCH = 3;
private static final int ADD_NEW_COMPANY = 4;
private PhoneBook phoneBook = new PhoneBook();

public void menu() {
System.out.println("");
System.out.println("1. Add a new contact to the phone book.");
System.out.println("2. List all contacts in the phone book.");
System.out.println("3. Search a contact for name.");
System.out.println("4. Add a new company");
System.out.println("0. Exit.");
System.out.print("Choose an option:");
}

public void doAdd() {
System.out.print("Name: ");
String name = Keyboard.readString();
System.out.print("Surname: ");
String surname = Keyboard.readString();
System.out.print("Phone number: ");
String phone = Keyboard.readString();
phoneBook.addContact(new Contact(name, surname, phone));
}

public void doAddNewCompany() {
System.out.print("Name: ");
String name = Keyboard.readString();
System.out.print("Phone number: ");
String phone = Keyboard.readString();
phoneBook.addContact(new Company(name, phone));
}

public void doList() {
int nContacts = phoneBook.getNContacts();
Item contact;
for(int i = 0; i < nContacts; i++) {
contact = phoneBook.getContactAt(i);
System.out.println("Name: " + contact.getName());
System.out.println("Surname: " + contact.getSurname());
System.out.println("Phone: " + contact.getPhone());
}
}

public void doSearch() {
System.out.print("Name to search for: ");
String name = Keyboard.readString();
Item contact = phoneBook.searchForName(name);
System.out.println("Name: " + contact.getName());
System.out.println("Surname: " + contact.getSurname());
System.out.println("Phone: " + contact.getPhone());
}

public void doAction(int action) {
switch(action) {
case ADD :
doAdd();
break;
case LIST :
doList();
break;
case SEARCH :
doSearch();
break;
case ADD_NEW_COMPANY:
doAddNewCompany();
break;
case EXIT :
System.out.println("Bye!!");
break;
default :
System.out.println("The option is not valid.");
break;
}
}

public void go() {
int option;
do {
menu();
option = Keyboard.readInteger();
doAction(option);
}while(option != EXIT);
}

public static void main(String args[]) {
Main main = new Main();
main.go();
}
}

7. PhoneBook class:

ackage phonebookoscar;

public class PhoneBook {
private static final int SIZE = 100;
private int nContacts = 0;
private Item contacts[] = new Item[SIZE];

public PhoneBook() {}

public Item getContactAt(int position) {
Item contact = new Contact();
if(position >= 0 && position < nContacts)
contact = contacts[position];
return contact;
}

public int getNContacts() {
return nContacts;
}

public Item searchForName(String name) {
Item contact = null;
int position = 0;
while((contact == null) && position < nContacts) {
if(contacts[position].getName().equals(name))
contact = contacts[position];
else position++;
}
if(contact == null) contact = new Contact();
return contact;
}

public void addContact(Item contact) {
if(nContacts < SIZE)
contacts[nContacts++] = contact;
}
}
 
Back
Top Bottom