5 Replies - 6545 Views - Last Post: 21 December 2008 - 01:43 PM Rate Topic: -----

#1 anix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-December 07

how to display arrayList from file using switch?

Posted 19 December 2008 - 03:10 PM

Hi i am designing a console application in visual studio using c# and basically i need to have a switch that controls 4 things.

1. Display Departments
2. Add a department
3. Show employees by department
4. Add new employee

I have designed a switch system but im unsure on how to properly get the switch to display the things i want like the list of departments as im not used to writing console apps.

below is my code that i have up to now but i just dont know how to display my items or add my items via the switch can any1 help please?

using System;
using System.IO;
using System.Collections;
using System.Linq;
using System.Text;

namespace nsSwitch
{
using System;
public class nsSwitch
{
static void Main()
{
bool done = false;
do
{
payaction depts = new payaction(1);
payaction adddept = new payaction(2);
payaction employdept = new payaction(3);
payaction addemploy = new payaction(4);
Console.WriteLine("Select one of the following:");
Console.WriteLine("\t1 - Review Current Departments");
Console.WriteLine("\t2 - Add A Department");
Console.WriteLine("\t3 - List Employees By Department");
Console.WriteLine("\t4 - Add New Employee");
Console.Write("Enter Your selection (0 to exit): ");
string strSelection = Console.ReadLine();
int iSel;
try
{
iSel = int.Parse(strSelection);
}
catch (FormatException)
{
Console.WriteLine("\r\nWhat?\r\n");
continue;
}
Console.WriteLine("You selected " + iSel);
switch (iSel)
{
case 0:
done = true;
break;
case 1:
Console.WriteLine(depts);
break;
case 2:
Console.WriteLine(adddept);
break;
case 3:
Console.WriteLine(employdept);
break;
case 4:
Console.WriteLine(addemploy);
break;
default:
Console.WriteLine("You selected an invalid number: {0}\r\n", iSel);
continue;
}
Console.WriteLine();
} while (!done);

Console.WriteLine("\nGoodbye!");
}
}
class payaction
{
public payaction(int Type)
{
PetType = Type;
}
private int Type;
public int PetType
{
get { return (Type); }
set { Type = value; }
}
public override string ToString()
{
switch (PetType)
{
default:
return ("Unknown item");
case 1:
return ("This is where I want to display departments");
case 2:
return ("This is where I want to add a department");
case 3:
return ("This is where I want to display employees by dept");
case 4:
return ("This is where I want to add employee");
}
}
}
class departments
{
public ArrayList LoadTxtFile(string filename)
{
ArrayList items = new ArrayList();
StreamReader reader = new StreamReader("depts.txt");
int numItems = int.Parse(reader.ReadLine());
for (int i = 0; i < numItems; i++)
{
items.Add(reader.ReadLine());
}
reader.Close();
return items;
}
}
} 


Is This A Good Question/Topic? 0
  • +

Replies To: how to display arrayList from file using switch?

#2 anix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-December 07

Re: how to display arrayList from file using switch?

Posted 19 December 2008 - 04:23 PM

Ok so I have solved one part of my problem by adding this code to the first case which displays my depts:

ArrayList items = new ArrayList();
						string linevalue = "";

						StreamReader filereader = new StreamReader("depts.txt");
						while ((linevalue = filereader.ReadLine()) != null)
						{
							items.Add(linevalue);
						}
						filereader.Close();
						items.Sort();
						//To display the content of array (sorted)
						IEnumerator myEnumerator = items.GetEnumerator();
						while (myEnumerator.MoveNext())
							Console.WriteLine(myEnumerator.Current);


I now do need help on how to add items to my depts file by allowing the user to enter a new dept and then writing that to the file, is there any1 who can help me with this? thanks
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: how to display arrayList from file using switch?

Posted 19 December 2008 - 10:12 PM

I reworked your routine a bit. :)

Try() Catch() blocks can keep an application from aborting without the user knowing why.
Note the commented out Console.ReadLine(); statements.

The ArrayList Class has a built-in indexer in the Item property. No need to work so hard! :)
See ArrayList.Item Property
public ArrayList LoadTxtFile(string filename)
{
	ArrayList items = new ArrayList();
	string linevalue = "";

	try
	{
		StreamReader filereader = new StreamReader("depts.txt");

		while ((linevalue = filereader.ReadLine()) != null)
		{
			items.Add(linevalue);
		}

		filereader.Close();
	}
	catch (Exception ex)
	{
		Console.WriteLine(ex.Message);
		//Console.ReadLine();
	}

	items.Sort();

	//To display the content of array (sorted)
	for (int i = 0; i < items.Count; i++)
		Console.WriteLine(items[i]);

	//Console.ReadLine();

	return items;
}


Quote

I now do need help on how to add items to my depts file by allowing the user to enter a new dept and then writing that to the file.

How are you defining a dept?

This post has been edited by n8wxs: 19 December 2008 - 11:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 anix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-December 07

Re: how to display arrayList from file using switch?

Posted 20 December 2008 - 07:55 AM

I am not sure what you mean by "How are you defining a dept?" as the departments are just in a txt file and are listed as below:

marketing
payroll
sales

Basically I just want to let the user add a deparment and have that department added to the txt file, however i want the program to load the departments in and then let the user make all the changes they want and instead of having the program continually updating the txt file i want it to make the changes locally and then update the txt file on exit of the program, but im not sure how to do this in C# so help will be appreciated thanks.
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: how to display arrayList from file using switch?

Posted 20 December 2008 - 08:16 PM

View Postanix, on 20 Dec, 2008 - 06:55 AM, said:

I am not sure what you mean by "How are you defining a dept?" as the departments are just in a txt file and are listed as below:

marketing
payroll
sales

Basically I just want to let the user add a deparment and have that department added to the txt file, however i want the program to load the departments in and then let the user make all the changes they want and instead of having the program continually updating the txt file i want it to make the changes locally and then update the txt file on exit of the program, but im not sure how to do this in C# so help will be appreciated thanks.

Well, you could write the file contents out in the FormClosing event handler. You'd probably want to set a flag in the routines that change the dept data indicating the file needs to be rewritten. Test the flag in the Form1_FormClosing() method and output to your file as needed.
Was This Post Helpful? 0
  • +
  • -

#6 anix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-December 07

Re: how to display arrayList from file using switch?

Posted 21 December 2008 - 01:43 PM

Hi thanks for all your help i have completed the switch and the can now successfully load in the departments and add them as well and they save correctly. Now comes the difficult for me as im used to working with databases or xml files in actionscript, this is in the console and i need to basicaly set something up where i can load employee names up depending on the department that the user enters, however i dont know how to work this or what type of file would be best to store this data, can some 1 please help me with this? thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1