Implement a Java application allowing users to manage their Contact objects. You
ID: 3908909 • Letter: I
Question
Implement a Java application allowing users to manage their Contact objects. Your application should provide the users with the following options: (main menu)
-Add a new contact
-Search for a contact by name, email, or phone number (10 points bonus for wildcard [*] search) [edit option should be included in here too]
-Edit a contact
-Exit
Make sure to apply paging so the users can view a set of contacts per screen
You are free to design sub-menus for each option.
The Contact class is defined in Contact.java file. This is Computer Science II JAVA. Need JAVADOC included.
public class ContactApp {
public static void main(String[] args){
//pre-processing
//an array of Contacts
while(true){
//show main menu
//obtain and validate user's input
if exit option is chosen
break;
}
//handle post processing
}
}
public class Contact {
//constructors
public Contact(String fName, String emailOrPhone){
this.fName = fName;
if(isEmail(emailOrPhone)){ //check if it is an email
this.email = emailOrPhone;
}else {
this.primaryPhone = emailOrPhone;
}
}
public Contact(String fName, String lName, String emailOrPhone){
this.fName = fName;
this.lName = lName;
if(isEmail(emailOrPhone)){ //check if it is an email
this.email = emailOrPhone;
}else {
this.primaryPhone = emailOrPhone;
}
}
public Contact(String fName, String lName, String phone, String email){
this.fName = fName;
this.lName = lName;
this.email = email;
this.primaryPhone = phone;
}
//setter
public void setMobilePhone(String phone){
this.mobilePhone = phone;
}
public void setAddress(String address){
this.address = address;
}
public void setAlias(String alias){
this.alias = alias;
}
////////////////COMPLETE THEM
//helper methods
private boolean isEmail(String data){
if (....) // finish this code
return true;
return false; //it's not an email
}
public String toString(){//display the Contact as a string
}
// need to be able to compare/search for matching
//Put in appropriate getters
//attributes
private String fName, lName, title, alias,
primaryPhone, mobilePhone, email,
address; //address should be represented by an independent class
}
Explanation / Answer
Implemented Java source Code:-
-------------------------------------
package com.koothada;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
class Contact
{
String FirstName;
String LastName;
long PhoneNumber;
public Contact(String FirstName, String LastName,Long PhoneNumber)
{
this.FirstName=FirstName;
this.LastName=LastName;
this.PhoneNumber=PhoneNumber;
}
@Override
public String toString() {
return "Contact [FirstName=" + FirstName + ", LastName=" + LastName
+ ", PhoneNumber=" + PhoneNumber + "]";
}
}
public class ContactApp
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner sc=new Scanner(System.in);
ArrayList<Contact>list=new ArrayList<Contact>();//create a Array list to Store All the Contact Details
File contact_details = new File ("F:\Workspace_Luna\Contactlist.txt");//Reading Contact details from the File
Scanner scanner = new Scanner(contact_details);
String Firstname,Lastname;
long Phone;
Contact contactlist;
while(scanner.hasNextLine())
{
Firstname = scanner.next();//Read First word of line in file as FirstName
Lastname = scanner.next();//Read Second word of line in a File as Second Name
Phone=scanner.nextLong();//Read the Third word of line as Phone Number
contactlist=new Contact(Firstname,Lastname,Phone);//Passing Input parameters through Constructor reading from the File.
//or Otherwise your wish Pass Arguments through the Constructor only
//like contactlist=new Contact("venkanna","Koothada",9949468557);
list.add(contactlist);
}
scanner.close();
System.out.println("The contact Details from the file is");
for(Contact obj:list)
{
System.out.println("FirstName: "+obj.FirstName+" LastName: "+obj.LastName+"PhoneNumber:"+obj.PhoneNumber);
}
//Or Use toString Method to print All the contact information.
//System.out.println(contactlist);
while(true)
{
System.out.println("*** Main Menu ****");
System.out.println("1.Add New Contact");
System.out.println("2.Search Contact");
System.out.println("3.Edit Contact");
System.out.println("4.Exit");
System.out.println("please Select any option");
int option=sc.nextInt();
boolean status = false;
switch(option)
{
case 1:
System.out.println("Please Enter Firstname:");
Firstname=sc.next();
System.out.println("Please enter LastName:");
Lastname=sc.next();
System.out.println("Please Enter MobileNumber");
Phone=sc.nextLong();
contactlist=new Contact(Firstname,Lastname,Phone);
list.add(contactlist);
System.out.println("New Contact Details Are Added Successfully");
break;
case 2:
System.out.println("Please Enter Phone Number");
Phone=sc.nextLong();
for(Contact obj:list)
{
if(Phone==obj.PhoneNumber)
{
status=true;
System.out.println("FirstName: "+obj.FirstName);
System.out.println("LastName: "+obj.LastName);
break;
}
}
if(status==false)
System.out.println("Contact Details are not found");
break;
case 3:
System.out.println("Please Enter Phone Number");
Phone=sc.nextLong();
for(Contact obj:list)
{
if(Phone==obj.PhoneNumber)
{
status=true;
System.out.println("FirstName: "+obj.FirstName);
System.out.println("LastName: "+obj.LastName);
System.out.println("Please Enter New Mobile/Phone Number:");
Phone=sc.nextLong();
obj.PhoneNumber=Phone;
System.out.println("New Phone Number Updated Successfully");
break;
}
}
if(status==false)
System.out.println("Contact Details are not found");
break;
case 4:
System.out.println("Terminated Successfully");
System.exit(0);
default:
System.out.println("Invalid Option please try again");
}
sc.close();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.