Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Delete method not deleting

c#

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 josh909

josh909

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 04 November 2014 - 12:29 PM

I'm creating a program that holds three arrays: one for the person's last name, one for the points scored and one for the player number.Now, I got all the arrays and everything done when I call my ProcessDelete method into my DeletePlayer method it isn't removing the items from the list when you select list from the menu and I can't figure out why

 

Any help or guidance in the right direction would really help

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlayerSystem6
{
    class Program
    {
        static void Main(string[] args)
        {
            //The MAXPLAYERS constant is the physical table size
            const Int32 MAXPLAYERS = 23;

            //Declare the player tables
            Int32[] playerNumbers = new Int32[MAXPLAYERS];
            String[] playerLastNames = new String[MAXPLAYERS];
            Int32[] playerPoints = new Int32[MAXPLAYERS];

            //Keep track of the actual number of players (i.e. logical table size)
            Int32 playerCount = 0;

            //Main Driver
            char menuItem;
            Console.WriteLine("Welcome to the player system...\n");
            menuItem = GetMenuItem();
            while (menuItem != 'X')
            {
                ProcessMenuItem(menuItem, playerNumbers, playerLastNames, playerPoints, ref playerCount, MAXPLAYERS);
                menuItem = GetMenuItem();
            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }

        //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            while (menuItem != 'C'
                && menuItem != 'L' && menuItem != 'X' && menuItem != 'R' && menuItem != 'U' && menuItem != 'D')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
            Console.WriteLine("\nPlease pick an item:");
            Console.WriteLine("C - Create Player");
            Console.WriteLine("R - Retrive Player");
            Console.WriteLine("U - Update Player");
            Console.WriteLine("D - Delete Player");
            Console.WriteLine("L - List Players");
            Console.WriteLine("X - Exit");
        }

        //Routes to the appropriate process routine based on the user menu choice
        static void ProcessMenuItem(Char menuItem, Int32[] playerNumbers, String[] playerLastNames,
            Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS)
        {
            switch (menuItem)
            {
                case 'C':
                    ProcessCreate(playerNumbers, playerLastNames, playerPoints, ref playerCount, MAXPLAYERS);
                    break;
                case 'L':
                    ProcessList(playerNumbers, playerLastNames, playerPoints, playerCount);
                    break;
                case 'R':
                    ProcessRetrive(playerNumbers, playerLastNames, playerPoints,  playerCount, MAXPLAYERS);
                    break;
                    
                case 'U':
                    ProcessUpdate(playerNumbers, playerLastNames, playerPoints, playerCount, MAXPLAYERS);
                    break;
                case 'D':
                    DeletePlayer(playerNumbers, playerLastNames, playerPoints, ref playerCount, MAXPLAYERS);
                       break;

            }
        }

        //Creates a player in the tables if the array is not already full and the name is not a duplicate
        static void ProcessCreate(Int32[] playerNumbers, String[] playerLastNames,
            Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS)
        {
            Int32 number, points;
            String lastName;
            if (playerCount < MAXPLAYERS)
            {
                number = GetPositiveInteger("\nCreate Player: please enter the player's number");
                if (GetPlayerIndex(number, playerNumbers, playerCount) == -1)
                {
                    lastName = GetString("\nCreate Player: please enter the player's last name");
                    points = GetPositiveInteger("\nCreate Player: please enter the player's points");
                    InsertPlayer(number, lastName, points, playerNumbers, playerLastNames, playerPoints, ref playerCount);
                    Console.WriteLine("\nCreate Player: Number - {0}, Name - {1}, Points - {2}, created successfully", number, lastName, points);
                    Console.WriteLine();
                }
                else
                    Console.WriteLine("\nCreate Player: the player number already exists");
            }
            else
                Console.WriteLine("\nCreate Player: the player roster is already full");

        }

        //Inserts the player at the correct location in the tables based on order of 
        //ascending player number. Unless the insert location is at the end, this 
        //requires shifting existing players down in order to make room 
        static void InsertPlayer(Int32 number, String lastName, Int32 points,
            Int32[] playerNumbers, String[] playerLastNames, Int32[] playerPoints,
            ref Int32 playerCount)
        {
            Int32 insertIndex, shiftCount;
            insertIndex = GetInsertIndex(number, playerNumbers, playerCount);
            for (shiftCount = playerCount; shiftCount > insertIndex; shiftCount--)
            {
                playerNumbers[shiftCount] = playerNumbers[shiftCount - 1];
                playerLastNames[shiftCount] = playerLastNames[shiftCount - 1];
                playerPoints[shiftCount] = playerPoints[shiftCount - 1];
            }
            playerNumbers[insertIndex] = number;
            playerLastNames[insertIndex] = lastName;
            playerPoints[insertIndex] = points;
            playerCount++;
        }

        //Returns the index of the first player number in the table that is greater
        //than the player number to be inserted
        static Int32 GetInsertIndex(Int32 playerNumber, Int32[] playerNumbers,
            Int32 playerCount)
        {
            Int32 index = 0;
            bool found = false;
            while (index < playerCount && found == false)
                if (playerNumbers[index] > playerNumber)
                    found = true;
                else
                    index++;
            return index;
        }

        //Returns the index of the player number in the table 
        //or -1 if the number is not found
        static Int32 GetPlayerIndex(Int32 playerNumber,
            Int32[] playerNumbers, Int32 playerCount)
        {
            Int32 index = 0;
            bool found = false;
            while (index < playerCount && found == false)
                if (playerNumbers[index] == playerNumber)
                    found = true;
                else
                    index++;
            if (found == false)
                index = -1;
            return index;
        }

        //Lists the players in the tables
        static void ProcessList(Int32[] playerNumbers, String[] playerLastNames,
            Int32[] playerPoints, Int32 playerCount)
        {

            if (playerCount > 0)
            {
                Console.WriteLine("\n{0,7}   {1,-25}{2,6}\n", "Number", "Last Name", "Points");
                for (Int32 player = 0; player < playerCount; player++)
                    Console.WriteLine("{0,7}   {1,-25}{2,6}", playerNumbers[player], playerLastNames[player], playerPoints[player]);
            }
            else
                Console.WriteLine("\nList Players: the roster is empty");
        }

        //Returns a positive integer
        static Int32 GetPositiveInteger(String prompt)
        {
            Int32 n;
            Console.WriteLine(prompt);
            n = Int32.Parse(Console.ReadLine());
            while (n < 0)
            {
                Console.WriteLine("\nError: enter positive value");
                Console.WriteLine(prompt);
                n = Int32.Parse(Console.ReadLine());
            }
            return n;
        }
        //Returns a non-empty string
        static String GetString(String prompt)
        {
            String returnString;
            Console.WriteLine(prompt);
            returnString = Console.ReadLine();
            while (returnString == "")
            {
                Console.WriteLine("\nError: must enter keyboard data");
                Console.WriteLine(prompt);
                returnString = Console.ReadLine();
            }
            return returnString;
        }
        static void ProcessRetrive(Int32[] playerNumbers, String[] playerLastName, Int32[] playerPoints, Int32 playerCount, Int32 MAXPLAYERS)
        {
             int player;// Player number to find
            int playerindex;//index of the player number in Array
            if (playerCount < MAXPLAYERS)
            {
                player = GetPositiveInteger("\nRetrieve Player: please enter the player's number");
                playerindex = GetPlayerIndex(player, playerNumbers, playerCount);
                if (playerindex != -1)
                {
                    //  if (playerLastNames[playerindex].ToString().Length!= 0)
                    {
                        //Console.WriteLine("{0,7}{1,-25}{2,6}", playerNumbers[playerindex], playerLastNames[playerindex], playerPoints[playerindex]);
                        Console.WriteLine("\nRetrive Player: Number - {0}, Name - {1}, Points - {2}", playerNumbers[playerindex], playerLastName[playerindex], playerPoints[playerindex]);
                        Console.WriteLine();
                    }
                }
                else
                    Console.WriteLine("\nRetrieve Player: player not found");
            }
            else
                Console.WriteLine("\nRetrieve Player: the roster is empty");
        }

        static void ProcessUpdate(Int32[] playerNumbers, String[] playerLastName, Int32[] playerPoints, Int32 playerCount, Int32 MAXPLAYERS)
        {
            int player;// Player number to find
            int playerindex;//index of the player number in Array

            String lastName;
            int points;

            if (playerCount < MAXPLAYERS|| playerCount == MAXPLAYERS)
            {
                player = GetPositiveInteger("\nUpdate Player: please enter the player's number");
                playerindex = GetPlayerIndex(player, playerNumbers, playerCount);
                if (playerindex != -1)
                {
                    lastName = GetString("\nUpdate Player: please enter the player's updated last name");
                    points = GetPositiveInteger("\nUpdate Player: please enter the player's updated points");

                    playerLastName[playerindex] = lastName;
                    playerPoints[playerindex] = points;

                }
                else
                    Console.WriteLine("\nUpdate Player: the player number does not exists");
            }
            else
                Console.WriteLine("\nUpdate Player: the player does not exist in the roster");
        }
        static  Int32[] ProcessDelete(Int32[] playerNumbers, ref Int32 playerCount, String[] playerLastName, Int32[] playerPoints )
        {
            Int32[] newArray = new Int32[playerNumbers.Length - 1]; String[] newArray2 = new String[playerLastName.Length - 1]; Int32[] newArray3 = new Int32[playerPoints.Length - 1];

            int index = 0;
            int index2 = 0;
            int index3 = 0;
            int r = 0;
            int j = 0;
            int t = 0;
            while (index < playerNumbers.Length && index2 < playerLastName.Length && index3 < playerPoints.Length)
            {
                if (index != playerCount)
                {
                    newArray[r] = playerNumbers[index];
                    r++;
                }

                index++;
                if (index2 != playerCount)
                {
                    newArray2[j] = playerLastName[index2];
                    j++;
                }

                index2++;

               if (index3 != playerCount)
                {
                    newArray3[t] = playerPoints[index3];
                    t++;
                }

                index3++;
            }
            return newArray;
           
           
           
        }

        static void DeletePlayer(Int32[] playerNumbers, String[] playerLastName, Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS)
        {
            int player;// Player number to delete
            int playerindex;//index of the player number in Array
            if (playerCount < MAXPLAYERS)
            {
               
                player = GetPositiveInteger("\nDelete Player: please enter the player's number");
                playerindex = GetPlayerIndex(player, playerNumbers, playerCount);
               

               if (playerindex != -1)
                {
                    
                    {
                       
                        Console.WriteLine("\nDelete Player: Number - {0}, Name - {1}, Points - {2}", playerNumbers[playerindex], playerLastName[playerindex], playerPoints[playerindex]);
                        Console.WriteLine("Succesfully Deleted");
                        Console.WriteLine();
                        ProcessDelete(playerNumbers, ref playerCount, playerLastName, playerPoints);
                    }
                }
                else
                    Console.WriteLine("\nDelete Player: player not found");
            }
            else
                Console.WriteLine("\nDelete Player: the roster is empty");
        }
        
    }
}


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 04 November 2014 - 08:08 PM

One thing is that you don't delete, but return a new array, which you don't use.

In other words, you are not deleting from the source array, just making a new one without the elements meant to be deleted.

For doing so, it would be better if you just have the array as global, or pass it as reference, and work directly on it, with the array Index.

On the other hand, if you want to make it good, take a look at Dictionary, it's keyed, and it will make your job a lot easier.



#3 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 05 November 2014 - 10:43 AM

I would encapsulate the workings of player into it's own class.  Then have a manager class that contains the list of these objects List<player> and hides the complexity from the front end.

 

Have the front end only do "front end" things, like UI menu, etc.  and have the class deal with the inner workings without any unnecessary knowledge of the inner workings.  

 

The front end would end up being simplified, by something like :

playerManager.Add( /*your param list here*/ );

and Player Count would be a simple property of the manager:

int playerNum = playerManager.PlayerCount;

my 2 cents


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download