Home > database >  Update details after retrieving the list item by index
Update details after retrieving the list item by index

Time:07-25

I am unable to update the details after retrieving the list item by index(How should I update the details after retrieving a list item?)

try
{
    Console.WriteLine("Updating the student details");
    Console.WriteLine("Enter the index at which to update a student details");
    
    int index = Convert.ToInt32(Console.ReadLine());
    for (int i = 0; i < Student.Count; i  )
    {
        if(i==index)
        {
            Console.WriteLine("Id:{0} \nName:{1} \nMarks:{2}", Student[i].Name, Student[i].ID, Student[i].Marks);
        }
        Console.WriteLine("What details you want to update?");
    }
}                   
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
break;

Expected Output-->The code should except user input in Int form and fetch the details(by using the index) and return the fetched details to user, then user should be able to modify the details and the modified details should be updated to the list of student details.The Student list contains(string Name,int ID,int Marks).

CodePudding user response:

You can use a menu with a key to update individual property of the student.

Here is the student class:

public class Student
{
    public Student(int id, string name, int marks)
    {
        ID = id;
        Name = name;
        Marks = marks;
    }

    public int ID { get; set; }
    public string? Name { get; set; }
    public int Marks { get; set; }

    public override string ToString()
    {
        return $"Id: {ID} \nName:{Name} \nMarks:{Marks}";
    }
}

I have overridden ToString() to format print the student detail.

Insertion of dummy data into studentList,

Random generator = new Random();
List<Student> studentList = new List<Student>();

for (int i = 1; i <= 5; i  )
{
    studentList.Add(
        new Student(i, $"Student {i}", generator.Next(60, 101))
    );
}

Implementation of updating part with index out of bound check,

try
{
    Console.WriteLine("Enter the index at which to update a student details");
    int index = Convert.ToInt32(Console.ReadLine());
    if (index >= studentList.Count)
        throw new Exception("Out of bound student index");

    Console.WriteLine(studentList[index]);

    Console.WriteLine("What details you want to update?");

    Console.WriteLine("1. ID\n2. Name\n3.Marks");

    int choice = Convert.ToInt32(Console.ReadLine());

    switch(choice)
    {
        case 1:
            Console.WriteLine("Enter new ID: ");
            int newId = Convert.ToInt32(Console.ReadLine());
            studentList[index].ID = newId;
            break;
        case 2:
            Console.WriteLine("Enter new Name: ");
            string newName = Console.ReadLine() ?? "";
            studentList[index].Name = newName;
            break;
        case 3:
            Console.WriteLine("Enter new Marks: ");
            int newMarks = Convert.ToInt32(Console.ReadLine());
            studentList[index].Marks = newMarks;
            break;
        default:
            Console.WriteLine("Invalid choice");
            break;
    }

    Console.WriteLine(studentList[index]);
}
catch(Exception e)
{
    Console.WriteLine(e.Message);
}

CodePudding user response:

apologies,I cant find pastebin as I am new to StackOverflow,below is my code,the switch statement 4 is where I have SUB SWITCH included,PLease guide me if I am wrong.

   using System;
   using System.Collections.Generic;

   namespace StudentList
   {
      class Student
   {
    public string Name { get; set; }
    public int ID { get; set; }
    public int Marks { get; set; }

    public Student(string Name, int ID, int Marks)
    {
        this.Name = Name;
        this.ID = ID;
        this.Marks = Marks;
    }
}
class Program
{
    public static void Main()
    {
        List<Student> Student = new List<Student>();
        Console.WriteLine("----Welcome to Student Management System----");
        while (true)
        {
            Console.WriteLine("----Choose a Operation to perform----");
            Console.WriteLine("1-->Create");
            Console.WriteLine("2-->Read");
            Console.WriteLine("3-->Delete");
            Console.WriteLine("4-->Update\n");
            int num = Convert.ToInt32(Console.ReadLine());
            switch (num)
            {
                case 1:
                    try
                    {
                        Console.WriteLine("Enter the Name:");
                        string Name = Convert.ToString(Console.ReadLine());
                        Console.WriteLine("Enter the ID:");
                        int ID = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Enter the total marks:");
                        int Marks = Convert.ToInt32(Console.ReadLine());
                        Student.Add(new Student(Name, ID, Marks));
                        Console.WriteLine();
                        Console.WriteLine("\nAfter adding the Student,the list now contains:");
                        foreach (var std in Student)
                        {
                            Console.WriteLine("Name:"   std.Name);
                            Console.WriteLine("ID:"   std.ID);
                            Console.WriteLine("Marks:"   std.Marks);
                            Console.WriteLine(" ");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;
                case 2:
                    if (Student.Count == 0)
                    {
                        Console.WriteLine(
                            "There are no students in the list,Create Student details using Create option\n"
                        );
                    }
                    else
                    {
                        Console.WriteLine("Displaying the Student Details:");
                        foreach (var std in Student)
                        {
                            Console.WriteLine("Name:"   std.Name);
                            Console.WriteLine("ID:"   std.ID);
                            Console.WriteLine("Marks:"   std.Marks);
                        }
                    }
                    break;
                case 3:
                    try
                    {
                        if (Student.Count == 0)
                        {
                            Console.WriteLine(
                                "There are no students in the list,Create Student details using Create option\n"
                            );
                        }
                        else
                        {
                            Console.WriteLine("Removing the student details");
                            Console.WriteLine("Enter the Index to remove a student details");
                            int remove = Convert.ToInt32(Console.ReadLine());
                            Student.RemoveAt(remove);
                            Console.WriteLine(
                                "After Removing a Student,the list now contains,\n"
                            );
                            if (Student.Count == 0)
                            {
                                Console.WriteLine(
                                    "There are no students in the list,Create Student details using Create option\n"
                                );
                            }
                            foreach (var std in Student)
                            {
                                Console.WriteLine("Name:"   std.Name);
                                Console.WriteLine("ID:"   std.ID);
                                Console.WriteLine("Marks:"   std.Marks);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;
                case 4:
                    try
                    {
                        if (Student.Count == 0)
                        {
                            Console.WriteLine(
                                "There are no students in the list,Create Student details using Create option\n"
                            );
                        }
                        else
                        {
                            Console.WriteLine("Updating the student details");
                            Console.WriteLine(
                                "Enter the index at which to update a student details"
                            );
                            if (Student.Count == 0)
                            {
                                Console.WriteLine(
                                    "There are no students in the list,Create Student details using Create option\n"
                                );
                            }
                            int index = Convert.ToInt32(Console.ReadLine());
                            for (int i = 0; i < Student.Count; i  )
                            {
                                if (i == index)
                                {
                                    Console.WriteLine(
                                        "Id:{0} \nName:{1} \nMarks:{2}",
                                        Student[i].Name,
                                        Student[i].ID,
                                        Student[i].Marks
                                    );
                                }
                                Console.WriteLine("Enter 1 to update");
                                Console.WriteLine(
                                    "Enter any number other than 1 if there is nothing to update"
                                );
                                int n = Convert.ToInt32(Console.ReadLine());
                                Student.RemoveAt(n);
                                switch (n)
                                {
                                    case 1:
                                        Console.WriteLine("Enter the New Name:");
                                        string Name = Convert.ToString(Console.ReadLine());
                                        Console.WriteLine("Enter the New ID:");
                                        int ID = Convert.ToInt32(Console.ReadLine());
                                        Console.WriteLine("Enter the  New Total Marks:");
                                        int Marks = Convert.ToInt32(Console.ReadLine());
                                        Student.Add(new Student(Name, ID, Marks));
                                        Console.WriteLine();
                                        Console.WriteLine("\nUpdated");
                                        Console.WriteLine(" ");
                                        break;
                                    default:
                                        Console.WriteLine("No Updation");
                                        break;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;
                default:
                    Console.WriteLine("Invalid Choice,Please Choose a Number From 1 to 4");
                    break;
            }
        }
    }
}
}
  •  Tags:  
  • c#
  • Related