Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

College of Engineering and Applied Sciences

Instructor: Dima Aburshaid

CSIS 130 – Computer Programming II


Fall 2022

HW 5
Due: Wednesday December 23 2022, 9 PM

HW INSTRUCTIONS

LEARNING OUTCOMES
The purpose of this assignment is (1) to introduce students to the Java Collection interfaces
(Set and Map) and their implementations (LinkedList and HashMap), (2) to get more
practice in I/O operations. We achieve this by implementing a phonebook that allows for
collecting and retrieving contact information (phone numbers, emails, and addresses). It
also provides methods for saving contact information to output streams and loading them
later from input streams.

1. THE Phonebook CLASS


The Phonebook class is an abstraction of a phonebook. The essence of a phonebook is that
it allows you to store contact information about people you know and to retrieve (and
possibly update) this information at a later time. Therefore, a phonebook must maintain a
collection of its contacts information that facilitate information storage and retrieval. This
collection is a Map that maps each username (a String object) to its contact information (an
Info object).

The public methods of a Phonebook are:


• setContactInfo(name, phone, email, address): Associates the name with the
given contact information (phone, email, and address). If the name already exists in
the phonebook, the new information overwrites the old information and the old
information is lost.
• deleteContactInfo(name): Deletes the given name from the phonebook along with
all of its contact information. If the name is not in the phonebook,
deleteContactInfo does nothing.
• getPhone(name): Retrieves the phone number associated with the given name. If
the name is not in the phonebook, getPhone throws a NameNotFoundException.
• getEmail(name): Retrieves the email associatedwith the given name. If the name is
not in the phonebook, getEmail throws a NameNotFoundException.
• getAddress(name): Retrieves the address associated with the given name. If the
name is not in the phonebook, getAddress throws a NameNotFoundException.
• getNamesSortedAlphabetically(): Returns an array of strings containing the
names of all people in the phonebook, sorted alphabetically.
• clear(): Deletes all the names and contact information in the phonebook.

CSIS 130: HW5 Page 1 of 4


• save(OutputStream out): Saves all names and contact information to the given
output stream. The exact format for how to encode the contact information is not
specified in this assignment. You need to decide how contact information must be
saved into the output stream in such a way that the load method (below) knows
how to load it.
• load(InputStream in): Loads all names and contact information from the given
input stream and adds them to the contact information already in the phonebook.
The format of the data in the input stream is not specified in this assignment. It
depends on how the save method (above) works. The method save and load work
together.

CSIS 130: HW5 Page 2 of 4


1 public class Phonebook {

2 private static class Info {...}

3 public static class NameNotFoundException extends Exception {...}

5 private final Map<String, Info> infoMap = new HashMap<String, Info>();

7 public void setContactInfo(String name, String phone, String email, String address) {

8 throw new UnsupportedOperationException();

9 }

10 public void deleteContactInfo(String name) {

11 throw new UnsupportedOperationException();

12 }

13 public String getPhone(String name) throws NameNotFoundException {

14 throw new UnsupportedOperationException();

15 }

16 public String getAddress(String name) throws NameNotFoundException {

17 throw new UnsupportedOperationException();

18 }

19 public String getEmail(String name) throws NameNotFoundException {

20 throw new UnsupportedOperationException();

21 }

22 public String[] getNamesSortedAlphabetically() {

23 throw new UnsupportedOperationException();

24 }

25 public void clear() {

26 throw new UnsupportedOperationException();

27 }

28 public void save(OutputStream output) throws IOException {

29 throw new UnsupportedOperationException();

30 }

31 public void load(InputStream input) throws IOException {

3 throw new UnsupportedOperationException();

4 }

35 }

Figure 1: Skeleton for the Phonebook class. See Figure 2 and Figure 3 for the definitions
of the two inner-classes Info and NameNotFoundException.

CSIS 130: HW5 Page 3 of 4


1 private static class Info {
3 final String phone;
4 final String email;
5 final String address;
6

7 Info(String phone, String email, String address) {


8 this.phone = phone;
9 this.email = email;
10 this.address = address;
11 }
12 }

Figure 2: The Phonebook.Info class: this is an inner-class that should go inside Phonebook
(see Figure 1, line 3)

1 public static class NameNotFoundException extends Exception {


3 private final String name;
4 private NameNotFoundException(String name) {
5 this.name = name;
6 }
7

8 @Override
9 public String toString() {
10 return String.format("NameNotFoundException<%s>", name);
11 }
12 }

Figure 3: The Phonebook.NameNotFoundException class: this is an inner-class that should


go inside Phonebook (see Figure 1, line 5). It is used to for reporting attempts to access
information about people not listed in the phonebook.

CSIS 130: HW5 Page 4 of 4

You might also like