Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
2.50/5 (4 votes)
See more:
Guys , I need your valuable help so I can complete my programming assignment.
I now that I have to use methods and functions but I dont know how to use them in the program....

Assignment: http://pastebin.com/G7QNiPAc[^] (Here are the instructions of what I have to do)

practest5: http://pastebin.com/SGT4uh4m[^] (This is the program that I have to make changes to)

and this is what I've done so far :
C#
namespace Ass1
{
  public  class Arrays
    {

      static void Main(string[] args)
      {
           const int MAX_PEOPLE = 50;

            string[] people = new string[MAX_PEOPLE];

            string[] full_name = new string[MAX_PEOPLE];

            string[] adress = new string[MAX_PEOPLE];

            string[] street_number = new string[MAX_PEOPLE];

            string[] state = new string[MAX_PEOPLE];

            string[] phone_number = new string[MAX_PEOPLE];

            string[] post_code = new string[MAX_PEOPLE];

            float[] hours_worked = new float[MAX_PEOPLE];


            int current_people = 0;
            int total_people = 0;
            string tval = "";
            int c = 0;
            char choise = ' ';


            string vtemp = "";
            bool parseAttempt = true;
            string response = "";



            //Menu displayed
            while (response != "x" && response != "X")
            {
                Console.WriteLine("---------------What do you want to do ?---------------");
                Console.WriteLine("");
                Console.WriteLine("Add a person (a).");
                Console.WriteLine("Find a person(s).");
                Console.WriteLine("Display current person (p).");
                Console.WriteLine("Delete current person (d)");
                Console.WriteLine("Calculate and display pay slip for current person (c)");
                Console.WriteLine("");
                Console.WriteLine("Press 'x' to Exit. ");

                response = Console.ReadLine();

                switch (response)
                {

                    case "a":
                    case "A":

                        do
                        {
                            if (total_people < MAX_PEOPLE)
                            {
                                Console.Write("Enter person's full name >");
                                tval = Console.ReadLine();
                                full_name[current_people] = tval;

                                Console.Write("Enter person's Street number  >");
                                tval = Console.ReadLine();
                                street_number[current_people] = tval;

                                Console.Write("Enter person's Street adress  >");
                                tval = Console.ReadLine();
                                adress[current_people] = tval;

                                Console.Write("Enter person's State  >");
                                tval = Console.ReadLine();
                                state[current_people] = tval;

                                Console.Write("Enter person's Post code  >");
                                tval = Console.ReadLine();
                                post_code[current_people] = tval;

                                Console.Write("Enter person's phone number  >");
                                tval = Console.ReadLine();
                                phone_number[current_people] = tval;

                                Console.Write("Enter person's worked hours >");
                                tval = Console.ReadLine();
                                hours_worked[current_people] = float.Parse(tval);
                            }
                            else
                                Console.WriteLine("Person max {0} reached unable to store any more people", MAX_PEOPLE);

                            //Ask user to continue
                            Console.Write("Press 's'to continue or 'x' to exit  ");
                            response = string.Parse(Console.ReadLine());

                        } while (response == "s");
                        Console.Write("Enter the person to find >");
                        tval = Console.ReadLine();
                        for (c = 0; c < total_people; c++)
                        {
                            if (people[c] == tval)
                            {
                                current_people = c;
                                break;
                            }
                        }
Posted
Updated 26-Sep-12 8:04am
v4
Comments
pradiprenushe 11-Sep-12 5:43am    
Have you understand requirement? If yes then tell us where you stuck?
malakas1821 11-Sep-12 5:45am    
I dont know how to continue from what I've already done... I'm supposed to use arrays to store data and methods to modularize code. i'm confused about how I have to use methods
[no name] 11-Sep-12 10:51am    
"I dont know how to use them", you did not read your textbook? Did you ask your teacher for help? Without knowing what it is that "confuses" you, how would you expect anyone else to unconfuse you?

Without reading your assignment (and it's your homework, I'm certainly not going to do it) there are a couple of things you can do to your code which will modularise it and make it easier to read.

Firstly, your code is 3 basic chunks.
1) Init
2) Loop
2.1) Menu write and input
2.2) Obey commands
3) Dunno - your code just ends in mid method.

So I would make it three chunks:
C#
static void Main(string[] args)
   {
   Initialize();
   bool exit = false;
   while (!exit)
      {
      string response = GetUserInput();
      exit = ProcessUserInput(response);
      }
   }
Now you Main method is small, and easy to read - and it does nothing complex.
Note that I have moved your string arrays and other variables outside Main - since they will be referred to by other aspect of teh program, it makes sense that they are class level, not method local variables.

GetUserInput is (at present) simple:
C#
private static string GetUserInput()
   {
   Console.WriteLine("---------------What do you want to do ?---------------");
   Console.WriteLine("");
   Console.WriteLine("Add a person (a).");
   Console.WriteLine("Find a person(s).");
   Console.WriteLine("Display current person (p).");
   Console.WriteLine("Delete current person (d)");
   Console.WriteLine("Calculate and display pay slip for current person (c)");
   Console.WriteLine("");
   Console.WriteLine("Press 'x' to Exit. ");
   return Console.ReadLine();
   }
Again, this is now simple, and clear.
The complex one is your ProcessUserInput:
C#
private static bool ProcessUserInput(string input)
   {
   bool result = false;
   switch (input.ToLower())
      {
      default:
          UnknownInput(input);
          break;
      case "x":
          result = true;
          break;
      case 'a':
          AddPersons();
          break;
      ...
      }
   return result;
   }
Again, you can see what is happening at each stage, and it is all simple.
You move the complexity of adding people to a self-contained method and let it worry about it.

At each stage, you are making the program less complex and easier to read.

Over to you to give it a try, and fill in the methods you need. Don't forget that they can be modularised as well!

Good luck!
 
Share this answer
 
class, methods, function all are code-blocks, by using them we can manage our code in way it can be more easy to understand.

Methods - accept input parameter ,performs task
Function - accept input parameter ,performs task, returns output parameter
Input parameters are optional, if you want then declare

example:
1. you want to Fill data in controls while form load
then create Method 'FillData', and call it from load event (you can divide more if 'filldata' have lot of lines in code, like FillTreeView, FillComboes, FillTextBox... )

2. you want to Save data in database, it will need status that tell you data is stored or not?
then create Function 'SaveData' that return true/false value as status, and call it from Save buttons click, then check value if function returns true, show message "Saved Successfully" and Clear all Controls... else ...

it is an example, you can manage same thing in many different ways

In short manage code-blocks in way that improve performance, re-usability, code-maintenance and also easily-understandable.

Happy Coding!
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900