Home > Software engineering >  How to get the sum of integers in multiple objects?
How to get the sum of integers in multiple objects?

Time:10-21

I have two objects they are: Student and Courses:

public Student(string StudentID, string Name, string Status, Enum StudentMajor, Dictionary<Courses, Grade[]> CompletedCourses)

public Courses(string courseName, string courseCode, string passingGrade, int numOfCredits, List<Courses> prerequisites)

Here's the objects I created in my Main class

//Courses
Courses ITEC_120 = new("Introduction to Computer Hardware", "ITEC 120", "C", 3, new List<Courses> {});
Courses ITEC_122 = new("Introduction to Operating Systems", "ITEC 122", "C", 3, new List<Courses> { ITEC_120 });

//Student
Student student1 = new("00069110", "Antony Dos Santos", "Full-time", Majors.Computer_Information_Systems, new Dictionary<Courses, Grade[]>()
{
 { ITEC_120, new[] { Grade.F, Grade.B, Grade.Not_Taken } },
 { ITEC_122, new[] { Grade.A, Grade.Not_Taken, Grade.Not_Taken } 
},
});

As you can see the courses have a variable called credit and each Student object has a dictionary that takes a Course and a Grade

So student1 has two courses and each of the courses has 3 credits each. How would I iterate over the Dictionary to get the total of all the courses in CoursesCompleted in this case it should be 6.

Each object is added to a List

//List for Courses objects
List<Courses> CompulsoryCourses = new List<Courses>();
CompulsoryCourses.Add(ITEC_120);
CompulsoryCourses.Add(ITEC_122);//Adding the two courses to the List

List for Student objects
List<Student> students = new List<Student>();
students.Add(student1);

foreach(var stu in students)
{
var GPA = 0.0;
var CourseCredits = 0;
Console.WriteLine("\nStudent Information");
foreach (KeyValuePair<Courses, Grade[]> item in stu.CompletedCourses)
{
    var TotalCredits = CourseCredits item.Key.numOfCredits;
    Console.WriteLine("\nName: "   item.Key.courseName   ", Credits: "   item.Key.numOfCredits);
    GPA = stu.calGPA(CourseCredits);
    Console.WriteLine("Total Credits: " TotalCredits);
}
Console.WriteLine(stu.Name  ": GPA = "   GPA);

}

In the foreach loop above I'm looping through the students List, and then I use another foreach loop to iterate the Dictionary to get the Key which in this case is numOfCredits like: item.Key.numOfCredits, and add them to each other.

Currently it's only adding the numOfCredits to itself and not to the other course in the dictionary.

CodePudding user response:

You can use LINQ for something like this. If you have a list of cars and want to sum just the price you can do this:

var cars = new List<Car>
{
    new Car{ BrandName = "Lamborghini", Model = "Huracan", Price = 300000M },
    new Car{ BrandName = "Porsche", Model = "Gt3rs", Price = 200000M },
    new Car{ BrandName = "Vauxhall", Model = "Corsa", Price = 20000M }
};

var totalPrice = cars.Sum(car => car.Price);

Console.WriteLine($"Total Price: {totalPrice}");

Where 'Car' looks like this:

public class Car
{
    public string BrandName { get; set; }
    public string Model { get; set; }
    public decimal Price { get; set; }
}

CodePudding user response:

Not sure exactly what you are asking for. But adding the sum of the cars "prices" i assume.

You could create a list, where all cars are added, along with all new cars. Then what you could do it creating a for loop, thats the lengt of that spesific list. And through that loop add all values. Would look something like this:

Class creation

    class Car
    {
        public string name;
        public int price;

        public Car(string name, int price)
        {
            this.name = name;
            this.price = price;
        }
    }

Then for creating the list, and adding some cars.

            Car car1 = new Car("blue-car", 15);
            Car car2 = new Car("red-car", 15);


            List<Car> cars = new List<Car>() { car1, car2 };

And then finnaly we count the sum up.

            int totalprice = 0;
            for (int i = 0; i < cars.Count; i  )
            {
                totalprice = totalprice   cars[i].price;
            }
  • Related