Click here to Skip to main content
15,888,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Kindly, if you could, repair this program in those methods: search and delete.
I have tried and tried could not do it


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

namespace ClassLibrary1
{
    public class Class1
    {
        public class Record
        {
            public int ssn; // Primary Key
            public String name;
            public double pay;
            public Record(int newSSN, String newName, double newPay)
            {
                ssn = newSSN;
                name = newName;
                pay = newPay;
            }
        }
        //------------------------------
        public class RecordDB
        {
            private static int maxRecords = 200;
            private int numRecords = 0;
            private Record[] recordList = new Record[maxRecords];
            //--------------------------------------------------------------------------------
            public void insert(Record rec)
            {
                if (numRecords == maxRecords - 1)
                {
                    return;
                }

                //recodList[numRecords].ssn    = rec.ssn;
                //recodList[numRecords].name   = rec.name;
                //recodList[numRecords].newPay = rec.newPay;

                //numRecords++;
                printAll();
            }
            //--------------------------------------------------------------------------------
            public void delete(int newSSN)
            {
                printAll();
            }
            //--------------------------------------------------------------------------------
            //public Record search(int newSSN)
            public void search(int newSSN)//,Record rec)//<--- should return record
            {int i;
                int first = 0, last = numRecords - 1;
                // Print All records
                printAll();
                //for (i = 0; i < numRecords; i++)
                //{
                //    if(newSSN==
                //}
                //Record recx = new Record(30, "JohnX", 5000);
                //return recx;
            } // end of search function
            //-------------------------------------------------------------------------------
            private void printAll() {
                Console.Write("---------------------------------");
                for (int i = 0; i < numRecords; i++)
                {
                    Console.Write("" + i + " " + recordList[i].ssn +
                    "  " + recordList[i].name + " " + recordList[i].pay);
                }
            }
        }//class1
        //--------------------------------------------------------------------------------

        public static void main(String[] args)
        {
        RecordDB recDB = new RecordDB();

        recDB.insert(new Record(3, "John", 500));
        recDB.insert(new Record(22, "Mike", 2500));
        recDB.insert(new Record(13, "Mark", 1760));
        recDB.insert(new Record(19, "Bob", 500));
        recDB.insert(new Record(7, "Cathy", 500));

        //Record r = recDB.search(22,recDB);<-----------how can i use search method?
        //if (r == null)
        //Console.Write("Not found");
        //else
        //Console.Write("Found");
        //r = recDB.search(34);
        //if (r == null)
        //Console.Write("Not found");
        //else
        //Console.Write("Found");
        //recDB.delete(19);
        //recDB.delete(7);
        //recDB.delete(3);
        //recDB.delete(13);
        ////recDB.delete(21);
        //recDB.delete(22);
        }
    }
}
Posted
Updated 19-Mar-11 1:19am
v2
Comments
Dalek Dave 19-Mar-11 7:19am    
Edited for Grammar and Readability.

1. modify your search method to return a Record object. Make sure that the object you return is instantiated.
2. make sure you supply the parameters of the method with the correct types. In your method, the second parameter(which is commented) is of type Record. but you are passing a RecordDB object (recDb is not of type record) to it.
3. Its better to split those classes to separate ones. Never meddle with nested classes if you know it will just be a pain on the rear for you. :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Mar-11 23:36pm    
What do you mean "never meddle with nested classes"? Do they present any extra complexity? Nah...
--SA
walterhevedeich 20-Mar-11 15:10pm    
Probably for some. As you can see, he was confused on passing the wrong types of parameters to his methods. Perhaps he needs to separate them if he wants it to be more readable.
Sergey Alexandrovich Kryukov 21-Mar-11 0:41am    
I mean complexity of things does not grow if your put some type definitions as nested, only reduced due to removing redundant readability. Yes, OP is confused, so how to help it? Anyway, generally your advice is good, so I voted 5.
--SA
walterhevedeich 21-Mar-11 14:59pm    
Thanks. Your point makes sense as well. :)
Sergey Alexandrovich Kryukov 21-Mar-11 0:14am    
[misplaced]
Do not search manually. You want to search by one field? Put all records in System.Collection.Generic.Dictionary<Ssn, Record> indexed by your field. You search will be very fast, use Dictionary<Ssn, Record>.TryGetValue.

Alternatively or additionally, learn to use LINQ: http://msdn.microsoft.com/en-us/magazine/cc700332.aspx[^], http://msdn.microsoft.com/en-us/library/system.linq.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Khalid Sabtan 18-Mar-11 23:54pm    
i real never done some think like this but i will read on hpp://msdn.microsoft/en-......
if some one could provide the quick answer will be great help for me.
Sergey Alexandrovich Kryukov 19-Mar-11 1:58am    
There is nothing else to help. Just do it, it's 5 lines of code or so.
--SA
Khalid Sabtan 19-Mar-11 2:05am    
i could not.Those are the errors.Error 1 Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier.

To declare a fixed size buffer field, use the fixed keyword before the field type.

Error2Array size cannot be specified in a variable declaration (try initializing with a 'new' expression).the program enervate my time!,could u do those 5 lines
Sergey Alexandrovich Kryukov 19-Mar-11 2:59am    
I have no idea what are you talking about. Use "improve question" and add the code you try.
--SA
i have repaired almost all the program i only need help on delete method
i would change the question now to
HOW CAN I DELETE ONE RECORD my delete method is empty ?
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace main
{
    class Program
    {
        public class Record
        {
            public int ssn;	// Primary Key
            public String name;
            public double pay;
            public Record(int newSSN, String newName, double newPay)
            {
                ssn = newSSN;
                name = newName;
                pay = newPay;
            }
        }
        //------------------------------
        public class RecordDB
        {
            private static int maxRecords = 200;
            private int numRecords = 0;
            private Record[] recordList = new Record[maxRecords];
            //--------------------------------------------------------------------------------
            public RecordDB()
            {
                int i;
                for (i = 0; i < maxRecords; i++)
                    recordList[i] = new Record(0, "", 0.0);
            }
            //----------------------------------
            public void insert(Record rec)
            {
                if (numRecords == maxRecords - 1)
                {
                    return;
                }
                recordList[numRecords].ssn = rec.ssn;
                recordList[numRecords].name = rec.name;
                recordList[numRecords].pay = rec.pay;
                numRecords++;
                Console.Write("---------------------------------\n");
                Console.Write("We are in insert\n");
                //Console.Write(rec.ssn.ToString() + " " + rec.name + "  " + rec.pay.ToString() + "\n");
                printAll();
            }

//--------------------------------------------------------------------------------
C#
public void delete(int newSSN)
{   int i,j;
    Console.Write("---------------------------------\n");
    Console.Write("We are in delete newSSN=" + newSSN.ToString() + "  numRecords=" + numRecords.ToString() + "\n");
    for(i=0;i<numRecords;i++)
    {
        if(newSSN == recordList[i].ssn)
        {
            for (j = 0; j< numRecords-1; j++)
            {
                recordList[j].ssn = recordList[j + 1].ssn;
                recordList[j].name = recordList[j + 1].name;
                recordList[j].pay = recordList[j + 1].pay;
            }
            numRecords--;
            goto outy;
        }//
    }
    outy:
    printAll();
}


//--------------------------------------------------------------------------------
public Record search(int newSSN)
           {
               int i;
               int first = 0, last = numRecords - 1;
               Record Temporary=new Record(0,"",0.0);
               Temporary.ssn = newSSN;
               Temporary.name = "";
               Temporary.pay = 0.0;
               Console.Write("---------------------------------\n");
               Console.Write("We are search     we will search for newSSn=" + newSSN.ToString() + "\n");
               //printAll();
               for (i = 0; i < numRecords; i++)
               {
                   //Console.Write("(i)=" + i.ToString()+" ssn="+recordList[i].ssn.ToString()+" name="+recordList[i].name+" pay="+recordList[i].pay.ToString() + "\n");
                   if (newSSN == recordList[i].ssn)
                   {
                       Console.Write("FOUND @i="+i.ToString()+"  inside search      inside loop(i) " + "i=" + i.ToString() + " " + recordList[i].ssn.ToString() +
                       "  " + recordList[i].name + " " + recordList[i].pay.ToString() + "\n");
                       return recordList[i];
                   }
               }
               Temporary.ssn = -1;
               return Temporary;
           }// end of search function

//-------------------------------------------------------------------------------
private void printAll()
            {
                Console.Write("Hellow printAll\n");
                for (int i = 0; i < numRecords; i++)
                {
                    Console.Write(i +" "+recordList[i].ssn.ToString() +
                    "  "+recordList[i].name+" "+recordList[i].pay.ToString()+"\n");
                }
            }
        }
//RecordDB
//---------------------------------------------------
public static int Main(String[] args)
        {
            RecordDB recDB = new RecordDB();
            recDB.insert(new Record(34, "John", 500));
            recDB.insert(new Record(22, "Mike", 2500));
            recDB.insert(new Record(13, "Mark", 1760));
            recDB.insert(new Record(19, "Bob", 500));
            recDB.insert(new Record(7, "Cathy", 500));
            Record r = recDB.search(22);
            if (r.ssn == -1) Console.Write("Not found\n"); else Console.Write("Found\n");
            r = recDB.search(34);
            if (r.ssn == -1) Console.Write("Not found\n"); else Console.Write("Found\n");
            r = recDB.search(123456);
            if (r.ssn == -1) Console.Write("Not found\n"); else Console.Write("Found\n");
            recDB.delete(34);
            recDB.delete(22);
            recDB.delete(13);
            recDB.delete(19);
            recDB.delete(7);
            return 0;
        }//Main
    }//Program
}//namespace
 
Share this answer
 
v5
Comments
Dalek Dave 19-Mar-11 7:21am    
Edited for Code Blocks.

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