Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 131,499 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,811 people online right now. Registration is fast and FREE... Join Now!




Preventing Duplicate values in a list.

 
Closed TopicStart new topic

Preventing Duplicate values in a list., comparing a number just entered to other numbers in a List, to prevent

Onker
post 9 Oct, 2008 - 04:29 PM
Post #1


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 37



Thanked 1 times
My Contributions


Hi guys.
As it says in the description, I'm trying to create a list, it'll have first name, last name, and an ID number. The intent is to prevent the duplication of an ID number before it gets added, and thus allow it to be corrected.

Here's my code so far, everything else works pretty well, I'm just having an issue with this step right now.

CODE

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

namespace RonT_2
{
    class RonT
    {
        public struct Info
        {
            public string firstName;
            public string lastName;
            public int idNum;

            public Info(string firstName, string lastName, int idNum)
            {
                this.firstName = firstName;
                this.lastName = lastName;
                this.idNum = idNum;
            }
        }

        static void Main(string[] args)
        {
            List<Info> students = new List<Info>();

            bool again = true;
            Console.Clear();

            Console.WriteLine("Please input your student information: ");

            while (again)
            {
                Console.Write("\nFirst Name: ");
                string firstName = Console.ReadLine();
                Console.Write("Last Name: ");
                string lastName = Console.ReadLine();
                Console.Write("Id: ");
                string input3 = Console.ReadLine();
                int idNum = Convert.ToInt32(input3);


                
                Console.WriteLine("\n\nMore Students? (y/n)");
                char input4 = Console.ReadKey().KeyChar;

                if (input4 == 'n')
                    again = false;
                else if (input4 == 'y')
                    again = true;
                else
                {
                    Console.WriteLine("You have not entered a valid response. Goodbye.");
                    again = false;
                }
            }

            bool Run = true;
            while (Run)
            {
                Console.Clear();
                Console.WriteLine("Select a method to sort and display the students");
                Console.WriteLine("1. By first name");
                Console.WriteLine("2. By last name");
                Console.WriteLine("3. By student ID number");
                Console.WriteLine("4. All of the above");
                Console.WriteLine("5. Exit");
                char Selection = Console.ReadKey().KeyChar;

                switch (Selection)
                {
                    case '1': students.Sort(new SortByFirstName());
                        Console.WriteLine("Student information sorted with first name");
                        printStudents(students);
                        Console.ReadKey();
                        break;
                    case '2': students.Sort(new SortByLastName());
                        Console.WriteLine("Student information sorted with last name");
                        printStudents(students);
                        Console.ReadKey();
                        break;
                    case '3': students.Sort(new SortByID());
                        Console.WriteLine("Student information sorted with ID number");
                        printStudents(students);

                        Console.ReadKey();
                        break;
                    case '4': students.Sort(new SortByFirstName());
                        Console.WriteLine("Student information sorted with first name");
                        printStudents(students);

                        students.Sort(new SortByLastName());
                        Console.WriteLine("Student information sorted with last name");
                        printStudents(students);

                        students.Sort(new SortByID());
                        Console.WriteLine("Student information sorted with ID number");
                        printStudents(students);

                        Console.ReadKey();
                        break;
                    case '5': Run = false;
                        break;
                }
            }
            Console.ReadKey();

    }
    static void printStudents(List<Info> lst)
    {
        foreach (Info s in lst)
        Console.WriteLine("{0:000000000}\t{1}, {2}", s.idNum, s.lastName, s.firstName);
        Console.WriteLine();
    }
    
    class SortByID : IComparer<Info>
    {
        public int Compare(Info a, Info b)
        {
            return a.idNum.CompareTo(b.idNum);
        }
    }
    
    class SortByLastName : IComparer<Info>
    {
        public int Compare(Info a, Info b)
        {
            return a.lastName.CompareTo(b.lastName);
        }
    }
    
    class SortByFirstName : IComparer<Info>
    {
        public int Compare(Info a, Info b)
        {
            return a.firstName.CompareTo(b.firstName);
        }
    }

    }
}
User is offlineProfile CardPM

Go to the top of the page


n8wxs
post 9 Oct, 2008 - 05:47 PM
Post #2


D.I.C Regular

***
Joined: 6 Jan, 2008
Posts: 299



Thanked 26 times
My Contributions


You are not saving the student information anywhere. Below I have added a new
student info struct. Each input string is copied to the appropriate struct field.
Before the new student struct is added to the students list, the ID number is
compared against the existing ID numbers. If it already exists, the new record is
NOT added to the students list.

csharp

#region one
while (again)
{
bool dupIdNum = false;

Info newStudent = new Info();

Console.Write("\nFirst Name: ");
string firstName = Console.ReadLine();

newStudent.firstName = firstName;

Console.Write("Last Name: ");
string lastName = Console.ReadLine();

newStudent.lastName = lastName;

Console.Write("Id: ");
string input3 = Console.ReadLine();
int idNum = Convert.ToInt32(input3);

newStudent.idNum = idNum;

foreach (Info st in students)
{
if (st.idNum == newStudent.idNum)
{
dupIdNum = true;

Console.WriteLine("Id number already in use:");
Console.WriteLine(st.firstName);
Console.WriteLine(st.lastName);
Console.WriteLine(st.idNum);
Console.WriteLine("Hit any key to continue");
Console.ReadKey();
}
}

if (dupIdNum == false)
students.Add(newStudent);

Console.WriteLine("\n\nMore Students? (y/n)");
char input4 = Console.ReadKey().KeyChar;

if (input4 == 'n')
again = false;
else if (input4 == 'y')
again = true;
else
{
Console.WriteLine("You have not entered a valid response. Goodbye.");
again = false;
}
}
#endregion


HTH smile.gif

Jeff
User is offlineProfile CardPM

Go to the top of the page

Onker
post 9 Oct, 2008 - 05:54 PM
Post #3


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 37



Thanked 1 times
My Contributions


Very nice, thank you very much Jeff. I knew I'd started making it more of a mess than it had to be.

A few more things to work out, hopefully I'll be ok now though. If not, I'll post again soon.
User is offlineProfile CardPM

Go to the top of the page

Onker
post 9 Oct, 2008 - 07:03 PM
Post #4


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 37



Thanked 1 times
My Contributions


Well, I'm down to the last parts now. I've got about 6 things left to build but there are three things that're making me scratch my head. I'd have some coffee and keep at it, if I didn't have to get some sleep for work and all.

Anywho, less chat more details I suppose.

Things left to do:

#1 - Figure out Why it is when I input an ID code on the second student, if I use the same ID code, it doesn't check against the students List. I can't figure it out, although some of that could be the time.

#2 - add fields to the struct Info to give myself the ability to input something like this...

Quote:
Please input your student information:

KSU Id: 1234
First Name: Mary
Last Name: Jane
Midterm: 90
Final: 80
homework: 100
homework: 80
homework: 70
homework: 80
homework:

This is the first time I've ever had to deal with "Struct" and none of my reference materials tells me how malleable it is. Everything seems to indicate it's very simplistic...so do I have the ability to continually populate homework amounts? I can see the way the professor has it set up hitting enter with nothing on the homework line ends the cycle.


#3 - use those numbers to create a grade for the student, Midterm + Final * .4 + totalHomework / number homeworks * .2. I can do the math on that one easy enough..it's just figuring out how to pull out the homework numbers. Well, I could probably figure out how to pull them out, if I could figure out how to put them in.

Beyond that...everything else is gravy and easy. I'm just stumped on those three parts. If anyone can spare me some help, that'd be awesome and appreciated.

Here's the modified code to this point.

CODE

public struct Info
        {
            public string firstName;
            public string lastName;
            public int idNum;
            //public int Midterm;
            //public int Final;

          

            public Info(string firstName, string lastName, int idNum)
            {
                this.firstName = firstName;
                this.lastName = lastName;
                this.idNum = idNum;
            }
        }

        static void Main(string[] args)
        {
            List<Info> students = new List<Info>();

            bool again = true;
            Console.Clear();

            Console.WriteLine("Please input your student information: ");

              while (again)  
             {  
                 bool dupIdNum = false;  
  
                 Info newStudent = new Info();  
  
                 Console.Write("\nFirst Name: ");  
                 string firstName = Console.ReadLine();  
                 newStudent.firstName = firstName;  
  
                 Console.Write("Last Name: ");  
                 string lastName = Console.ReadLine();  
                 newStudent.lastName = lastName;  
  
                 Console.Write("Id: ");  
                 string input3 = Console.ReadLine();  
                 int idNum = Convert.ToInt32(input3);  
                 newStudent.idNum = idNum;  
  
                 foreach (Info st in students)  
                 {
                     if (st.idNum == newStudent.idNum)
                     {
                         dupIdNum = true;

                         Console.Write("KSU Id already exists in the system, please input a new one: ");
                         string newInput = Console.ReadLine();
                         int newIdNum = Convert.ToInt32(newInput);
                         newStudent.idNum = newIdNum;
                         continue;
                     }
                     else
                         continue;
                 }
                
                 if (dupIdNum == false)  
                     students.Add(newStudent);  
  
                 Console.WriteLine("\n\nMore Students? (y/n)");  
                 char input4 = Console.ReadKey().KeyChar;  
  
                 if (input4 == 'n')  
                     again = false;  
                 else if (input4 == 'y')  
                     again = true;  
                 else  
                 {  
                     Console.WriteLine("You have not entered a valid response. Goodbye.");  
                     again = false;  
                 }  
             }  
User is offlineProfile CardPM

Go to the top of the page

Onker
post 10 Oct, 2008 - 05:43 AM
Post #5


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 37



Thanked 1 times
My Contributions


I've been looking everything over, and I'm of the opinion that if I can figure out the Struct portion (how to make it malleable so I can have a varying number of "homework" fields) I should be able to figure the rest of it out.

Can anyone help me figure that part out? Is it something I need to be doing IN the struct?
User is offlineProfile CardPM

Go to the top of the page

Onker
post 10 Oct, 2008 - 06:52 AM
Post #6


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 37



Thanked 1 times
My Contributions


As is frequently the case, my problem was a lot more difficult than it should have been...because of a failure in reading comprehension.

I don't have to store the individual grades to the struct, I have to process them and store the overall grade to the struct...which is one field...which is easily doable with a minor conversion to my existing set up.
/facepalm.

That being said, I can now do everything I need to I believe.

Thanks again to Jeff, for not only pointing out my mistake but giving me a beautifully functional bit of code to play with.

And thanks to those who popped into the thread to see if they could help as well. Not your fault I can't read what my professor wants.
User is offlineProfile CardPM

Go to the top of the page

Closed TopicStart new topic
Time is now: 11/20/08 12:19AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month