C# chapter 9 question #7 E-mail Address book Create an application with a class
ID: 3553559 • Letter: C
Question
C#
chapter 9 question #7
E-mail Address book
Create an application with a class named PersonEntry. The PersonEntry class should have properties for a person's name, e-mail address, and phone number.Also, create a text file that contains the names, e-mail addresses, and phone numbers for at least five people. When the application starts, it should read the data from the file and create a PersonEntry object for each person's data. The PersonEntry objects should be added to a List, and each person's name should be displayed in a list box on the application's main form.When the user selects a name from the list box, a second form should appear displaying that person's name. e-mail address, and the phone number.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace phonebook
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void searchBtn_Click(object sender, EventArgs e)
{
phonebook search = new phonebook(forenameTxt.Text,phoneNumTxt.Text, emailAddressTxt.Text);
displayTxt.Text = search.findContact(forenameTxt.Text);
}
private void exitBtn_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void addBtn_Click(object sender, EventArgs e)
{
string forename;
string phoneNumber;
string emailAddress;
forename = forenameTxt.Text;
phoneNumber = phoneNumTxt.Text;
emailAddress = emailAddressTxt.Text;
if (forenameTxt.Text != "" && phoneNumTxt.Text != "" && emailAddressTxt.Text != "")
{
person create = new person(forename, phoneNumber, emailAddress);
}
else
{
MessageBox.Show("Error: Not all values added!");
}
}
private void clearBtn_Click(object sender, EventArgs e)
{
forenameTxt.Text = "";
phoneNumTxt.Text = "";
emailAddressTxt.Text = "";
}
private void saveBtn_Click(object sender, EventArgs e)
{
}
}
public class person
{
private string _Forename;
private string _PhoneNumber;
private string _EmailAddress;
public person(string forename, string phoneNumber, string emailAddress)
{
_Forename = forename;
_PhoneNumber = phoneNumber;
_EmailAddress = emailAddress;
phonebook addContact = new phonebook(_Forename, _PhoneNumber, _EmailAddress);
}
public string Forename
{
get
{
return _Forename;
}
set
{
_Forename = value;
}
}
public string phoneNumber
{
get
{
return _PhoneNumber;
}
set
{
_PhoneNumber = value;
}
}
public string emailAddress
{
get
{
return _EmailAddress;
}
set
{
_EmailAddress = value;
}
}
}
public class phonebook
{
private string Forname;
private string PhoneNumber;
private string EmailAddress;
int contactCount = 0;
person[] Contact = new person[100];
public phonebook(string forename, string phoneNumber, string emailAddress)
{
Forname = forename;
PhoneNumber = phoneNumber;
EmailAddress = emailAddress;
}
public void addContact()
{
Contact[this.contactCount] = new person(Forname, PhoneNumber, EmailAddress);
contactCount++;
}
public string findContact(string forename)
{
string searchName = "";
string searchPhone = "";
string searchEmail = "";
for (int i = 0; i < contactCount; i++)
{
if (Contact[i].Forename == forename)
{
searchName = Contact[i].Forename;
searchPhone = Contact[i].phoneNumber;
searchEmail = Contact[i].emailAddress;
}
}
return searchName + searchPhone + searchEmail;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.