Write a Java program that functions as an address book. It should have entries c
ID: 3677966 • Letter: W
Question
Write a Java program that functions as an address book. It should have entries containing at least the following information: first and last name, phone number, date of birth, address, and email address. You should be able to add entries and remove entries, as well as printing out any given entry. It should save entries to a file, and be able to read in entries from a file as well.
The address book must be able to sort by at least one field (preferably last name). You may use any sort for this that you like.
The address book will almost certainly contain a fixed limit on the total number of entries. However, it should be possible to increase this limit by simply changing a single line in your program and then recompiling with no other modifications.
An excellent program will be able to sort the entries by any field (first name, last name, DOB, phone number, email address, etc).
An excellent program will be able to display only entries matching a certain criteria (only last names beginning with the letter 'M', for example).
An excellent program easily and intelligently handles the use of multiple address books (multiple files).
A spectacular program features the ability to move an entry from one address book to another.
Just a recommendation: use a variable to keep track of the number of elements currently in the address book. This value will increase when you add new entries and decrease when you remove entries. This is the variable you will use in all your for loops for iterating through the array of records.
Once this variable reaches the capacity of the array (arr.length), the address book is "full" and no new entries can be added.
Bonus: when loading a file containing an address book, the program automatically checks if there is a friend's birthday within 3 days and prints on the screen all the close birthdays (birthdays that are today, then the ones that are tomorrow and lastly the ones that are the day after tomorrow).
Bonus 2: Use encryption and passwords for the address book files in the following way:
XOR has a property very useful in encryption: A XOR B=C; C XOR B=A, thus A is your unencrypted file, B is your password and C is the encrypted file.
You will pick a password, say "computing" and you should perform XOR between each ASCII code of the password with each byte of the file: first character of the password with first byte of the file, second character in pwd with second character in file, etc. At the end of the PWD you go to the begining character of the password.
Explanation / Answer
package Address;
import java.io.Console;
import java.util.Scanner;
import java.util.Vector;
/**
*
* AddressBook object with variables firstname, lastName, Birthday, City, State,
* PhoneNumber, ZipCode, Month, Day, Year.
*
*/
public class Address {
String address;
private String firstName;
private String lastName;
private String birthday;
private String city;
private String state;
private String phonenumber;
private String zipcode;
private String month;
private String day;
private String year;
Address(String firstName, String lastName, String birthday, String city, String state, String phonenumber,
String zipcode, String month, String day, String year){
this.firstName = "";
this.lastName = "";
this.birthday = "";
this.city = "";
this.state = "";
this.phonenumber = "";
this.zipcode = "";
this.month = "";
this. day = "";
this.year = "";
}
/**
*
* return the first name
*/
public String getfirstName() {
return this.firstName;
}
/**
*
* Set first name
*/
public void setfirstName(String newfirstName) {
this.firstName = newfirstName;
}
/**
*
* return the last name
*/
public String getlastName() {
return this.lastName;
}
/**
*
* set last name
*/
public void setlastName(String newlastName) {
this.lastName = newlastName;
}
/**
*
* return the birthday
*/
public String getBirthday() {
return this.birthday;
}
/**
*
* Set Birthday
*/
public void setBirthday(String newBirthday) {
this.birthday = newBirthday;
}
/**
*
* return the City
*/
public String getCity() {
return this.city;
}
/**
*
* Set the city
*/
public void setCity(String newCity) {
this.city = newCity;
}
/**
*
* return the state
*/
public String getState() {
return this.state;
}
/**
*
* set the state
*/
public void setState(String newState) {
this.state = newState;
}
/**
*
* return the zipcode
*/
public String getZipCode() {
return this.zipcode;
}
/**
*
* set the zipcode
*/
public void setZipCode(String newZipCode) {
this.zipcode = newZipCode;
}
/**
*
* return the phone number
*/
public String getPhoneNumber() {
return this.phonenumber;
}
/**
*
* Set Phone Number
*/
public void setPhoneNumber(String newPhoneNumber) {
this.phonenumber = newPhoneNumber;
}
/**
*
* return the Month
*/
public String getMonth() {
return this.month;
}
/**
*
* set the Month
*/
public void setMonth(String newMonth) {
this.month = newMonth;
}
/**
*
* return the day
*/
public String getDay() {
return this.day;
}
/**
*
* @param newName An updated name for the song
*/
public void setDay(String newDay) {
this.day = newDay;
}
/**
*
* return the Year
*/
public String getYear(){
return this.year;
}
/**
*
* Set the year
*/
public void setYear(String newYear) {
this.year = newYear;
}
/**
*
* toString all of the objects
*/
public String toString() {
return "fisrtName=" + firstName + " " + "lastName=" + lastName + " " + "birthday=" + birthday + " " + "city="
+ city + " " + "State=" + state + " " + "zipcode=" + zipcode + " " + "phonenumber=" + phonenumber +
" " + "month=" + month + " " + "day=" + day + " " + "year=" + year;
}
{
for( int i = 0; i<number; i++){
System.out.println();
System.out.println("Enter contact "+(i+1)+ " first name:");
firstName[i] = console.next();
outputStream.println();
outputStream.println("First name: "+first[i]);
System.out.println("Enter contact "+(i+1)+ " last name:");
last[i] = console.next();
outputStream.println("Last name: "+last[i]);
System.out.println("Enter contact " +(i+1)+ " home address.");
home[i] = Console.next();
outputStream.println("Home Address: "+home[i]);
System.out.println("Enter contact " +(i+1)+ " home phone");
phone[i] = console.next();
outputStream.println("Home Number: "+phone[i]);
System.out.println("Enter contact "+(i+1)+" Birthday");
cell[i] = Console.next();
outputStream.println("Birthday: "+birthday[i]);
System.out.println();
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Vector <String> namelist = new Vector <String>();
Vector <String> bubble = new Vector <String>();
Vector <String> select = new Vector <String>();
//sequential search
System.out.println("Sequential Search");
getData(namelist);
System.out.println("Enter a name to search for: ");
String seqTerm= console.next();
int result=seqSearch(namelist, seqTerm);
if (result==-1)
System.out.println("The name you searched for, "+seqTerm+", was not found.");
else
System.out.println("The name you searched for, "+seqTerm+", was found at location " + (result+1)+".");
//bubble sort
System.out.println("Bubble Sort");
getData(bubble);
bubbleSort(bubble);
System.out.println(bubble);
//selection sort
System.out.println("Selection Sort");
getData(select);
selectionSort(select);
System.out.println(select);
//binary search
System.out.println("Binary Search");
System.out.println("Enter a name to search for: ");
String bin=console.next();
int blah=binarySearch(select, bin);
if(blah==-1)
System.out.println("Your search item was not found.");
else
System.out.println("Your search item was found at location "+(blah+1)+".");
}
public static void getData(Vector <String> list)
{
Scanner console = new Scanner(System.in);
String str ="0.0";
while (!str.equals("-1"))
{
System.out.println("Enter a last name, or enter -1 to quit: ");
str=console.next();
if (str.compareTo("-1")!=0)
list.addElement(str);
}
}
public static int seqSearch(Vector <String> namelist , String searchItem)
{
int loc;
boolean found = false;
for (loc = 0; loc < namelist.size()-1; loc++)
if (namelist.elementAt(loc).equals(searchItem))
{
found = true;
break;
}
if (found)
return loc;
else
return -1;
}
public static void bubbleSort(Vector <String> bubble)
{
String temp;
int counter, index;
for (counter = 0; counter < bubble.size()-1; counter++)
{
for (index = 0; index < (bubble.size()-1 - counter); index++)
if (bubble.elementAt(index).compareTo(bubble.elementAt(index+1))>0)
{
temp = bubble.elementAt(index);
bubble.setElementAt(bubble.elementAt(index + 1),index);
bubble.setElementAt(temp,index+1);
}
}
}
public static void selectionSort(Vector <String> select)
{
int index;
int smallestIndex;
int minIndex;
String temp;
for (index = 0; index < select.size() - 1; index++)
{
smallestIndex = index;
for (minIndex = index + 1; minIndex < select.size(); minIndex++)
if (select.elementAt(minIndex).compareTo(select.elementAt(smallestIndex))<0)
smallestIndex = minIndex;
temp = select.elementAt(smallestIndex);
select.setElementAt(select.elementAt(index),smallestIndex);
select.setElementAt(temp,index);
}
}
public static int binarySearch(Vector <String> binary,String searchItem)
{
int first = 0;
int last = binary.size() - 1;
int mid=0;
boolean found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (binary.elementAt(mid).compareTo(searchItem)==0)
found = true;
else
if (binary.elementAt(mid).compareTo(searchItem) > 0)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
} //end binarySearch
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.