I want to convert this JAVA code to C++ Please!! code: File Name: MyContact.java
ID: 3718073 • Letter: I
Question
I want to convert this JAVA code to C++ Please!!
code:
File Name: MyContact.java
//Contact class
public class MyContact
{
//Declare contactName
private String contactName;
//Declare contactPhoneNum
private String contactPhoneNum;
//Declare contactEmail
private String contactEmail;
//Define method to set contact name
public void setName(String contactName)
{
//Set contactName
this.contactName = contactName;
}
//Define method to set the phone number
public void setPhoneNumber(String contactPhoneNum)
{
//Set the contact phone number
this.contactPhoneNum = contactPhoneNum;
}
//Define method to set the contact email
public void setEmail(String contactEmail)
{
//Set the contact email
this.contactEmail = contactEmail;
}
//Define method to get the contact name
public String getName()
{
//Return contact name
return contactName;
}
//Define method to get the contact phone number
public String getMyContactPhoneNumber()
{
//Return the contact phone number
return contactPhoneNum;
}
//Define method to get the contact email
public String getMyContactEmail()
{
//return the contact email
return contactEmail;
}
//Define method to represent contact details as string
public String toString()
{
//Return
return " Name:"+contactName+" Phone Number:"+ contactPhoneNum + " Email:"+contactEmail+" ";
}
}
File Name: MyAddressBook.java
//Import the needed files
import java.util.*;
import java.util.HashMap;
import java.io.*;
//class defines a address book
public class MyAddressBook
{
//main
public static void main(String[] args) throws Exception
{
//Declare fileToSave
String fileToSave = null;
//Define inpScaner
Scanner inpScaner = new Scanner(System.in);
//Decalre cnt and initialize it
int cnt=1;
//Declare map to create address book
HashMap< Integer,MyContact > myContactList = new HashMap< >();
//Call Function
displayProgramMenu();
//Get user choice
int myUserCh = inpScaner.nextInt();
//Loop
while(true)
{
//Switch statement
switch(myUserCh)
{
//if choice is 1, display all contact details
case 1:
//read new line
String m=inpScaner.nextLine();
//Loop to display all contact details
for(int kk=0;kk<cnt-1;kk++)
{
//Print the current contact info
System.out.println(myContactList.get(kk+1));
}
//Break
break;
//If choice is 2, add new contact
case 2:
//read new line
m=inpScaner.nextLine();
//Create new contact
MyContact newContact = new MyContact();
//Prompt for name
System.out.print("Enter name: ");
//Get name
newContact.setName(inpScaner.nextLine());
//Prompt for phone number
System.out.print("Enter phone number(xxx-xxx-xxxx): ");
//Get phone number
newContact.setPhoneNumber(inpScaner.nextLine());
//Prompt for email
System.out.print("Enter email: ");
//Get email address
newContact.setEmail(inpScaner.nextLine());
//Add newContact to address book
myContactList.put(cnt, newContact);
//Increment cnt by 1
cnt++;
//Break
break;
//If choice 3, delete contact detail
case 3:
//read new line
m=inpScaner.nextLine();
//Prompt for name
System.out.print("Enter name to delete from list: ");
//Read the name
String name=inpScaner.nextLine();
//Loop to check the address book
for(int kk=0;kk<cnt-1;kk++)
{
//Get current contact details
MyContact tp =(MyContact)myContactList.get(kk+1);
//Check for condition
if(tp.getName().equals(name))
{
//Remove current contact
for(int aa=kk+1; aa<cnt-1;aa++)
{
System.out.println(aa);
MyContact mp = (MyContact)myContactList.get(aa+1);
myContactList.put(aa, mp);
}
//Decrease cnt by 1
cnt--;
//Break
break;
}
}
//Break
break;
//If choice is 4, save the file
case 4:
//Prompt for file name
System.out.print("Enter file name to save: ");
//Get file name
fileToSave = inpScaner.next();
//Create print writer object
BufferedWriter outContactFile = new BufferedWriter (new FileWriter(fileToSave));
//Loop
for(int kk=0;kk<cnt-1;kk++)
{
//Get current contact details
MyContact tp =(MyContact)myContactList.get(kk+1);
//Write name
outContactFile.write(tp.getName());
//newline
outContactFile.newLine();
//Write phone number
outContactFile.write( tp.getMyContactPhoneNumber());
//newline
outContactFile.newLine();
//Write email
outContactFile.write(tp.getMyContactEmail());
//newline
outContactFile.newLine();
}
//Flush file
outContactFile.flush();
//Close file
outContactFile.close();
//Print message.
System.out.println("Saved");
//Exit
System.exit(0);
//Break
break;
//Default
default:
//Display
System.out.println("Invalid choice");
}
//Call Function
displayProgramMenu();
//Get choice
myUserCh = inpScaner.nextInt();
}
}
//Define method to display memu
public static void displayProgramMenu()
{
//Display
System.out.println("1 Display contact list");
System.out.println("2 Add Contact");
System.out.println("3 Delete contact");
System.out.println("4 Save contact list and Exit");
System.out.print("Your choice: ");
Explanation / Answer
//Hearder file MyContact.h
//This header file contains decleration of MyContact Class and its fileds and methods.
#pragma once
#include <string>
using namespace std;
//Contact class
class MyContact
{
private:
//Declare contactName
string contactName;
//Declare contactPhoneNum
string contactPhoneNum;
//Declare contactEmail
string contactEmail;
public:
//Define method to set contact name
void setName(string contactName);
//Define method to set the phone number
void setPhoneNumber(string contactPhoneNum);
//Define method to set the contact email
void setEmail(string contactEmail);
//Define method to get the contact name
string getName();
//Define method to get the contact phone number
string getMyContactPhoneNumber();
//Define method to get the contact email
string getMyContactEmail();
//Define method to represent contact details as string
string toString();
};
//file MyContact.cpp
//contains defination of MyContact class methods
#include "MyContact.h"
void MyContact::setName(string contactName)
{
//Set contactName
this->contactName = contactName;
}
void MyContact::setPhoneNumber(string contactPhoneNum)
{
//Set the contact phone number
this->contactPhoneNum = contactPhoneNum;
}
void MyContact::setEmail(string contactEmail)
{
//Set the contact email
this->contactEmail = contactEmail;
}
string MyContact::getName()
{
//Return contact name
return contactName;
}
string MyContact::getMyContactPhoneNumber()
{
//Return the contact phone number
return contactPhoneNum;
}
string MyContact::getMyContactEmail()
{
//return the contact email
return contactEmail;
}
string MyContact::toString()
{
//Return
return " Name:" + contactName + " Phone Number:" + contactPhoneNum + " Email:" + contactEmail + " ";
}
//MyAddressBook.cpp
#include "MyContact.h"
#include <string>
#include <unordered_map>
#include <vector>
#include <iostream>
#include<fstream>
static void displayProgramMenu()
{
//Display
cout<<"1 Display contact list"<<endl;
cout << "2 Add Contact" << endl;
cout << "3 Delete contact" << endl;
cout << "4 Save contact list and Exit" << endl;
cout << "Your choice: " << endl;
}
int main(int argc, char *argv[])
{
//Declare fileToSave
string fileToSave;
//Decalre cnt and initialize it
int cnt = 1;
//Declare map to create address book
unordered_map<int, MyContact> myContactList;
//Call Function
displayProgramMenu();
//Get user choice
int myUserCh = 0;
cin >> myUserCh;
//Loop
while (true)
{
//Switch statement
switch (myUserCh)
{
//if choice is 1, display all contact details
case 1:
{
//Loop to display all contact details
for (int kk = 0; kk < cnt - 1; kk++)
{
//Print the current contact info
MyContact contact = myContactList[kk + 1];
cout << contact.getName() <<" ";
cout << contact.getMyContactPhoneNumber() << " ";
cout << contact.getMyContactEmail() << endl;
}
//Break
break;
}
case 2: //If choice is 2, add new contact
{
//Create new contact
MyContact newContact;
//Prompt for name
cout << "Enter name: ";
//Get name
string name;
cin >> name;
newContact.setName(name);
//Prompt for phone number
cout << "Enter phone number(xxx-xxx-xxxx): ";
//Get phone number
string phone;
cin >> phone;
newContact.setPhoneNumber(phone);
//Prompt for email
cout << "Enter email: ";
//Get email addres
string email;
cin >> email;
newContact.setEmail(email);
//Add newContact to address book
myContactList.emplace(cnt, newContact);
//Increment cnt by 1
cnt++;
//Break
break;
//If choice 3, delete contact detail
}
case 3:
{
cout << "Enter name to delete from list: ";
//Read the name
string name;
cin >> name;
//Loop to check the address book
for (int kk = 0; kk < cnt - 1; kk++)
{
//Get current contact details
MyContact tp = myContactList[kk + 1];
//Check for condition
if(tp.getName() == name)
{
//Remove current contact
for (int aa = kk + 1; aa < cnt - 1; aa++)
{
cout << aa << std::endl;
MyContact mp = myContactList[aa + 1];
myContactList[aa] = mp;
}
//Decrease cnt by 1
cnt--;
//Break
break;
}
}
//Break
break;
}
case 4: //If choice is 4, save the file
{
//Prompt for file name
cout << "Enter file name to save: ";
//Get file name
string fileToSave;
cin>>fileToSave;
//open output file in append mode
ofstream tempVar(fileToSave,ios::app);
if (!tempVar.is_open()) {
cout << "Error in opening file";
}
//Loop
for (int kk = 0; kk < cnt - 1; kk++)
{
//Get current contact details
MyContact tp = myContactList[kk + 1];
//Write name
tempVar<<(tp.getName())<<endl;
//Write phone number
tempVar << tp.getMyContactPhoneNumber()<<endl;
//Write email
tempVar << tp.getMyContactEmail()<<endl;
}
//Flush file
tempVar.close();
//Print message.
cout << "Saved" << endl;
//Exit
exit(0);
//Break
break;
}
default: //Default
//Display
cout << "Invalid choice" << endl;
}
//Call Function
displayProgramMenu();
//Get choice
cin >> myUserCh;
}
}
//Note: These files are being build and executed on visual studio 2015
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.