Question

Phonebook. You want to have a phone book that allows you to manipulate Contacts. A contact...

Phonebook. You want to have a phone book that allows you to manipulate Contacts. A contact is made up of First Name, Last Name, Phone, Address, Birthday Date. (On java)
Options:
1. Set number of contacts to store
2. Enter contact
3. Show entered contacts
4. Sort by last name
5. Sort by birthday day
6. Exit
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ScreenShot

-------------------------------------------

Program

/*This program is used to save details in phone book
* Here ask user for how many customer details you want to enter
* Enter that much customer details
* Then display details
* Sort by last name wise
* Sort by day of date of birth
*/
//Header file for I/o and datecheck
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
//Phone book class
class PhoneBook{
   //Member variables
   String[] FirstName,LastName, Phone, Address, BirthdayDate;
   //Constructor set all arrays length 1
   PhoneBook(){
       FirstName=new String[1];
       LastName=new String[1];
       Phone=new String[1];
       Address=new String[1];
       BirthdayDate=new String[1];
   }
   //set array length user defined size
   public void setNumberOfContacts() {
       int n=0;
       Scanner in=new Scanner(System.in);
       System.out.println("Please enter how many contacts are you going to enter:");
       n=in.nextInt();
       FirstName=new String[n];
       LastName=new String[n];
       Phone=new String[n];
       Address=new String[n];
       BirthdayDate=new String[n];
   }
   //Enter contact details
   public void enterContacts() {
       String fName,lName,ph,addr,bDate;
       Scanner in=new Scanner(System.in);
       //Prompt user to enter details of contacts
       for(int i=0;i<FirstName.length;i++) {
           System.out.println("Please enter the first name:");
           fName=in.nextLine();
           System.out.println("Please enter the last name:");
           lName=in.nextLine();
           System.out.println("Please enter Phone number(10 digits):");
           ph=in.nextLine();
           //Check phone number length10 digits
           while(ph.length()!=10) {
               System.out.println("Please enter Phone number(10 digits):");
               ph=in.nextLine();
           }
           System.out.println("Please enter address(comma Seperated):");
           addr=in.nextLine();
           System.out.println("Please enter birth day(dd/mm/yyyy):");
           bDate=in.nextLine();
           //Date validation
           SimpleDateFormat sdfrmt = new SimpleDateFormat("dd/MM/yyyy");
            try
            {
                Date date = sdfrmt.parse(bDate);
             
            }
            catch (ParseException e)
            {
               System.out.println("Please enter birth day(dd/mm/yyyy):");
               bDate=in.nextLine();
             
            }
          
           //Set array with details
            FirstName[i]=fName;
           LastName[i]=lName;
           Phone[i]=ph;
           Address[i]=addr;
           BirthdayDate[i]=bDate;
       }
          
       }
   //Display contact details
   public void displayContacts() {
       System.out.println("FirstName          LastName          PhoneNumber         Address             BirthDayDate");
       System.out.println("******************************************************************************************\n");
       for(int i=0;i<FirstName.length;i++) {
           System.out.println(" "+FirstName[i]+"          "+LastName[i]+"            "+Phone[i]+"          "+Address[i]+"          "+BirthdayDate[i]);
       }
      
   }
   //Sort the array based on lastname  
   public void sortLastName() {
       int n =FirstName.length;
         for(int i=0; i < n; i++){
                 for(int j=1; j < (n-i); j++){
                          if(LastName[j-1].compareTo(LastName[j])>0){
                                 //swap elements
                                 String temp1 = LastName[j-1];
                                 String temp2 = FirstName[j-1];
                                 String temp3 = Phone[j-1];
                                 String temp4 = Address[j-1];
                                 String temp5 = BirthdayDate[j-1];
                                 LastName[j-1] =LastName[j];
                                 FirstName[j-1] =FirstName[j];
                                 Phone[j-1] =Phone[j];
                                 Address[j-1] =Address[j];
                                 BirthdayDate[j-1] =BirthdayDate[j];
                                 LastName[j] = temp1;
                                 FirstName[j] = temp2;
                                 Phone[j] = temp3;
                                 Address[j]=temp4;
                                 BirthdayDate[j]=temp5;
                         }
                        
                 }
         }
      
   }
   //Sort the array based on day
   public void sortBirthDay() {
       int n =FirstName.length;
       for(int i=0; i < n; i++){
           for(int j=1; j < (n-i); j++){
                   String day1=BirthdayDate[j-1].substring(0,2);
                   String day2=BirthdayDate[j].substring(0,2);
                          if(day1.compareTo(day2)>0){
                                 //swap elements
                                 String temp1 = LastName[j-1];
                                 String temp2 = FirstName[j-1];
                                 String temp3 = Phone[j-1];
                                 String temp4 = Address[j-1];
                                 String temp5 = BirthdayDate[j-1];
                                 LastName[j-1] =LastName[j];
                                 FirstName[j-1] =FirstName[j];
                                 Phone[j-1] =Phone[j];
                                 Address[j-1] =Address[j];
                                 BirthdayDate[j-1] =BirthdayDate[j];
                                 LastName[j] = temp1;
                                 FirstName[j] = temp2;
                                 Phone[j] = temp3;
                                 Address[j]=temp4;
                                 BirthdayDate[j]=temp5;
                       }
                        
                 }
         }
      
   }
}
//Test class(Main class)
public class PhoneBookTest {

   public static void main(String[] args) {
       //Object Creation
       PhoneBook pb=new PhoneBook();
       //Call member function of the class
       pb.setNumberOfContacts();
       pb.enterContacts();
       pb.displayContacts();
       pb.sortLastName();
       System.out.println("\n\nAfter Last Name based Sort:");
       pb.displayContacts();
       pb.sortBirthDay();
       System.out.println("\n\nAfter Day based Sort:");
       pb.displayContacts();

   }

}

---------------------------------------------------------

Output

Please enter how many contacts are you going to enter:
2
Please enter the first name:
Mahesh
Please enter the last name:
Tripadi
Please enter Phone number(10 digits):
9845756112
Please enter address(comma Seperated):
13,Hazar Street,Badhi
Please enter birth day(dd/mm/yyyy):
15/05/1996
Please enter the first name:
Milli
Please enter the last name:
Madav
Please enter Phone number(10 digits):
8954782514
Please enter address(comma Seperated):
13,Hazar Street,Badhi
Please enter birth day(dd/mm/yyyy):
04/11/1983
FirstName          LastName          PhoneNumber         Address             BirthDayDate
******************************************************************************************

Mahesh          Tripadi            9845756112          13,Hazar Street,Badhi          15/05/1996
Milli          Madav            8954782514          13,Hazar Street,Badhi          04/11/1983


After Last Name based Sort:
FirstName          LastName          PhoneNumber         Address             BirthDayDate
******************************************************************************************

Milli          Madav            8954782514          13,Hazar Street,Badhi          04/11/1983
Mahesh          Tripadi            9845756112          13,Hazar Street,Badhi          15/05/1996


After Day based Sort:
FirstName          LastName          PhoneNumber         Address             BirthDayDate
******************************************************************************************

Milli          Madav            8954782514          13,Hazar Street,Badhi          04/11/1983
Mahesh          Tripadi            9845756112          13,Hazar Street,Badhi          15/05/1996

Add a comment
Know the answer?
Add Answer to:
Phonebook. You want to have a phone book that allows you to manipulate Contacts. A contact...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose I want to make a list or contacts that allows me to lookup someone’s phone...

    Suppose I want to make a list or contacts that allows me to lookup someone’s phone number by first name. Suppose this list is Bill 385-353-1234 Rich 385-269-1234 Jane 801-352-1234 (A)Write a snippet of code that will store this information in a dictionary called “Contact” with each name as the key and corresponding phone number (as a string) as the value. You dont have to parse the data, just create a dictionary that has the information in it. (B) write...

  • Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static...

    Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...

  • a. Define the struct node that can store a name and up to 3 phone numbers...

    a. Define the struct node that can store a name and up to 3 phone numbers (use an array for the phone numbers). The node struct will also store the address to the next node. b. Define a class contactList that will define a variable to keep track of the number of contacts in the list, a pointer to the first and last node of the list. c. Add the following methods to the class: i. Add a new contact....

  • C++ In this assignment, you will write a class that implements a contact book entry. For...

    C++ In this assignment, you will write a class that implements a contact book entry. For example, my iPhone (and pretty much any smartphone) has a contacts list app that allows you to store information about your friends, colleagues, and businesses. In later assignments, you will have to implement this type of app (as a command line program, of course.) For now, you will just have to implement the building blocks for this app, namely, the Contact class. Your Contact...

  • Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names...

    Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names and phone numbers. ·         a. Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables. b.Add the following operations to your program: i. Add a new contact. Ask the user to enter the name and up to 3 phone numbers. ii. Delete a contact...

  • Requirements Create an Address Book class in Java for general use with the following behaviors: 1....

    Requirements Create an Address Book class in Java for general use with the following behaviors: 1. Constructor: public Address Book Construct a new address book object. • A contact has four fields: first name, last name, email and phone. (There could be more information for a real contact. But these are sufficient for the assignment.) . The constructor reads from the disk to retrieve previously entered contacts. If previous contacts exist, the address book will be populated with those contacts...

  • C++: Array of contact info Design a contact struct, that takes your phone struct and address...

    C++: Array of contact info Design a contact struct, that takes your phone struct and address struct along with a c string for a name to create a record of contact information. ( C++: Design a Struct Design two structs address and phone Address has the following attributes: street address, city name, state code, zip code. phone has 3 numbers: area code, prefix and suffix test these by writing a program that creates one of each and fills them with...

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • (JAVA) Using classes and inheritance, design an online address book to keep track of the names

    (JAVA)Using classes and inheritance, design an online address book to keep track of the names, addresses, phone numbers and birthdays of family members, friends and business associates. Your program should be able to handle a maximum of 500 entries.Define the following classes: Class Address to store a street name, city, state and zip code. Class Date to store the day, month and year. Class Person to store a person's last name and first name. Class ExtPerson that extends the class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT