Question

I'm struggling to figure out how to do this problem in Java, any help would be...

I'm struggling to figure out how to do this problem in Java, any help would be appreciated:

Create an application that uses a class to store and display contact information.

Console:

Welcome to the Contact List application

Enter first name: Mike

Enter last name: Murach

Enter email:      [email protected]

Enter phone:      800-221-5528

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

---- Current Contact -----------------------

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

Name:           Mike Murach

Email Address: [email protected]

Phone Number:   800-221-5528

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

Continue? (y/n): n

Specifications

  • Use a class named Contact to store the data for each contact. This class should include these methods:

public void setFirstName(String name)

public String getFirstName()

public void setLastName(String name)

public String getLastName()

public void setEmail(String email)

public String getEmail()

public void setPhone(String phone)

public String getPhone()

public String displayContact()

  • Use the Console class presented in chapter 7 or an enhanced version of it to get and validate the user’s entries.
  • The application should continue only if the user enters “y” or “Y” to continue.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/***************************************Contact.java**************************************/


public class Contact {

   private String firstName;
   private String lastName;
   private String email;
   private String phone;

   public Contact(String firstName, String lastName, String email, String phone) {
       super();
       this.firstName = firstName;
       this.lastName = lastName;
       this.email = email;
       this.phone = phone;
   }

   public String getFirstName() {
       return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   public String getEmail() {
       return email;
   }

   public void setEmail(String email) {
       this.email = email;
   }

   public String getPhone() {
       return phone;
   }

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

   public String displayContact() {

       return "Name:\t" + firstName + " " + lastName + "\n" + "Email Address:\t" + email + "\n" + "Phone Number:\t"
               + phone;
   }
}

/*******************************Console.java***********************************/

import java.util.Scanner;

public class Console {

   private static Scanner sc = new Scanner(System.in);

   public static String getLine(String prompt)

   {

       System.out.print(prompt);

       String s = sc.nextLine(); // read the whole line

       return s;

   }

   public static String getString(String prompt)

   {

       System.out.print(prompt);

       String s = sc.next(); // read the first string on the line

       sc.nextLine(); // discard the rest of the line

       return s;

   }

   public static int getInt(String prompt)

   {

       int i = 0;

       boolean isValid = false;

       while (!isValid)

       {

           System.out.print(prompt);

           if (sc.hasNextInt())

           {

               i = sc.nextInt();

               isValid = true;

           }

           else

           {

               System.out.println("Error! Invalid integer value. Try again.");

           }

           sc.nextLine(); // discard any other data entered on the line

       }

       return i;

   }

   public static int getInt(String prompt, int min, int max)

   {

       int i = 0;

       boolean isValid = false;

       while (!isValid)

       {

           i = getInt(prompt);

           if (i <= min)

               System.out.println(

                       "Error! Number must be greater than " + min + ".");

           else if (i >= max)

               System.out.println(

                       "Error! Number must be less than " + max + ".");

           else

               isValid = true;

       }

       return i;

   }

}

/******************************ContactDriver.java**************************************/

public class ContactDriver {

   public static void main(String[] args) {

       char option = ' ';

       System.out.println("Welcome to the Contact List application");
       do {

           String firstName = Console.getLine("Enter first name: ");
           String lastName = Console.getLine("Enter last name: ");
           String email = Console.getLine("Enter email: ");
           String phone = Console.getLine("Enter phone: ");

           Contact contact = new Contact(firstName, lastName, email, phone);
           System.out.println("----------------------------------------\n"
                   + "---------Current Contact-----------------\n" + "-----------------------------------------");
           System.out.println(contact.displayContact());
           System.out.println("-----------------------------------");
           option = Console.getLine("Continue?(y/n): ").toLowerCase().charAt(0);

       } while (option != 'n');

   }
}
/*****************************************output**********************************/

Please let me know if you have nay doubt or modify the answer, Thanks :)

Add a comment
Know the answer?
Add Answer to:
I'm struggling to figure out how to do this problem in Java, any help would be...
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
  • USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot...

    USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot be viewed from outside of the private class. //only the class functions within the class can access private members. private: string name; string email; string phone; //the public class is accessible from anywhere outside of the class //however can only be within the program. public: string getName() const { return name; } void setName(string name) { this->name = name; } string getEmail() const {...

  • "Function does not take 0 arguments". I keep getting this error for my last line of...

    "Function does not take 0 arguments". I keep getting this error for my last line of code: cout << "First Name: " << employee1.getfirstName() << endl; I do not understand why. I am trying to put the name "Mike" into the firstName string. Why is my constructor not working? In main it should be set to string firstName, string lastName, int salary. Yet nothing is being put into the string. #include<iostream> #include<string> using namespace std; class Employee {    int...

  • Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string...

    Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string +Contact()                     //default constructor sets all attribute strings to “unknown”; no other constructor +setName(string name) : void +setPhone(string pn) : void +setEmail(string em) : void +getName() : string +getPhone() : string +getEmail() : string +printContact() : void         //print out the name, phone numbers and email address Create New Project Name your project: CIS247_Lab7_YourLastName. For this exercise, you may use a header file or one file...

  • Complete the Person class: For setEmail, setFirstName, and setSurname: if the method is passed an empty...

    Complete the Person class: For setEmail, setFirstName, and setSurname: if the method is passed an empty string, it should do nothing; but otherwise it should set the appropriate field. For setMobile: if the method is passed a valid mobile phone number, it should set the appropriate field; otherwise it should do nothing. A string is a valid mobile phone number if every character in it is a digit from 0 to 9. Hints: To convert a string so you can...

  • This is for Java Programming Please use comments Customer and Employee data (15 pts) Problem Description:...

    This is for Java Programming Please use comments Customer and Employee data (15 pts) Problem Description: Write a program that uses inheritance features of object-oriented programming, including method overriding and polymorphism. Console Welcome to the Person Tester application Create customer or employee? (c/e): c Enter first name: Frank Enter last name: Jones Enter email address: frank44@ hot mail. com Customer number: M10293 You entered: Name: Frank Jones Email: frank44@hot mail . com Customer number: M10293 Continue? (y/n): y Create customer...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • 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...

  • using C++ language!! please help me out with this homework In this exercise, you will have...

    using C++ language!! please help me out with this homework In this exercise, you will have to create from scratch and utilize a class called Student1430. Student 1430 has 4 private member attributes: lastName (string) firstName (string) exams (an integer array of fixed size of 4) average (double) Student1430 also has the following member functions: 1. A default constructor, which sets firstName and lastName to empty strings, and all exams and average to 0. 2. A constructor with 3 parameters:...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

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