search through an array(instantiated with contact info from the file) and locate a partcular contact. so far i have been able to
create methods that i think will get the job done , honestly i suck at using access modifiers and returns and thus got stuck connecting all
the methods to make the programme work assuming there are no bugs.please review the code provided and try to connect the
methods ( would be glad to learn from your examples on using returns and access modifiers).question : can arrays be returned ? i was aiming at
at returning an array with code like this : return nameArray[250];didn't include the main cus there is nothing on it.
using System;
using System.IO;
namespace contacts_program
{
/// <summary>
/// Description of ManipulateFile.
/// </summary>
public class ManipulateFile
{
public ManipulateFile()
{
}
public void ContactInfo(){
string[] contactArray = new string [5];
string contactName,contactEmail,contactNumber,contactAddress,contactGroup;
Console.WriteLine("Enter name of new contact : ");
contactName = Console.ReadLine();
contactName = contactName.Trim();
Console.WriteLine("Enter email of new contact : ");
contactEmail = Console.ReadLine();
contactEmail = contactEmail.Trim();
Console.WriteLine("Enter number of new contact : ");
contactNumber = Console.ReadLine();
contactNumber = contactNumber.Trim();
Console.WriteLine("Enter address of new contact : ");
contactAddress = Console.ReadLine();
contactAddress =contactAddress.Trim();
Console.WriteLine("choose group of new contact : |friends|family|vip|others|");
contactGroup =Console.ReadLine();
contactGroup = contactGroup.Trim();
short i = 0;
contactArray[i] = contactName;
++i;
contactArray[i] = contactEmail;
++i;
contactArray[i] = contactNumber;
++i;
contactArray[i] = contactAddress;
++i;
contactArray[i] = contactGroup;
}
public string GetGroup(string []array){
string file = "";
string fileName;
string group =array[4];
bool again = true;
while (again == true){
switch (group){
case ("friends") :
file ="friends";
again = false;
break;
case ("family") :
file = "family";
again = false;
break;
case ("vip") :
file = "vip";
again = false;
break;
case ("others"):
file ="others";
again = false;
break;
default:
{
Console.WriteLine("Invalid group : enter a valid group");
again = true;
break;
}
}
}
fileName = file+"_file";
return fileName;
;
}
public void InfoSaved(string fileName,string []array){
bool itExist = false;//would have to work on the detection of file with same name
//cus this wouldn't work, itExist is always falsewhen method is called
if(itExist == true){
FileStream contactFile = new FileStream(fileName,FileMode.Open);
StreamWriter writer = new StreamWriter(contactFile);
for (int i = 0;i<=4;++i){
writer.WriteLine(array[i]);
}
itExist =true;
writer.Close();
contactFile.Close();
}
else
{
FileStream contactFile = new FileStream(fileName, FileMode.Create);
StreamWriter writer = new StreamWriter(contactFile);
for (int i = 0;i<=4;++i){
writer.WriteLine(array[i]);
}
itExist =true;
writer.Close();
contactFile.Close();
}
Console.WriteLine("contact saved in file : {0}",fileName);
Array.Clear(array,0,array.Length);
}
public string SearchInfo(){
string searchName = "";
string file = "";
string group ="";
string concatReturn;
bool again = true;
while(again == true){
Console.WriteLine("Enter name of contact to be searched for");
searchName = Console.ReadLine();
searchName = searchName.Trim();
Console.WriteLine("Choose group to search for contact");
Console.WriteLine("|friends|family|vip|others|");
group = Console.ReadLine();
group = group.Trim();
switch (group){
case ("friends") :
file = "friends_file";
again = false;
break;
case ("family") :
file = "family_file";
again = false;
break;
case ("vip") :
file = "vip_file";
again = false;
break;
case ("others"):
file ="others_file";
again = false;
break;
default:
{
Console.WriteLine("Invalid group : enter a valid group");
again = true;
break;
}
}
}
concatReturn = searchName+" "+file;
return concatReturn;
}
public string LoadSearchFile(string concatReturn){
string[] components;
string fileName,searchName;
components = concatReturn.Split(' ');
searchName = components[0];
fileName = components[1];
FileStream openFile = new FileStream(fileName,FileMode.Open);
StreamReader reader = new StreamReader(openFile);
string[] nameArray = new string [250];
short i = -1;
while(reader.ReadLine() == string.Empty){
nameArray[++i] = reader.ReadLine();
reader.ReadLine();
reader.ReadLine();
reader.ReadLine();
reader.ReadLine();
}
reader.Close();
openFile.Close();
return nameArray[250];//trying to return an array
}
void SearchMechanism(string[] array,string concatReturn){
string[] nameArray = new string[250];
nameArray[250] = LoadSearchFile( concatReturn);
string[] components;
string fileName ="";
string searchName;
string[] searchArray = new string [1250];
short j = -1;
FileStream openFile = new FileStream(fileName,FileMode.Open);
StreamReader reader = new StreamReader(openFile);
while(reader.ReadLine() == string.Empty){
array[++j] = reader.ReadLine();
}
reader.Close();
openFile.Close();
components = concatReturn.Split(' ');
searchName = components[0];
fileName = components[1];
int index = System.Array.BinarySearch(array,searchName);
Console.WriteLine("index is {0}",index);
}

New Topic/Question
Reply




MultiQuote




|