Thursday, May 2, 2013

GPA Classes

First we did a simple Use Case

Then we worked out these class diagrams. the idea is that we would create a Grade class to store the values and pass it to a list in the GPACalculator class to store and evaluate

Next we added the code. Here is the Grade class

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

namespace GPACalculatorProgram
{
    class Grade
    {
        //this is the shortcut way to declare
        //simple properties
        public double GradePoint { get; set; }
        public int Credits { get; set; }
        public string ClassName { get; set; }

        //we also add two constructors
        //a default constructor (no arguments)
        public Grade()
        {
            GradePoint = 0;
            Credits = 0;
            ClassName = null;
        }

        //an alternate constructor that takes three arguments
        public Grade(double grade, int numberOfCredits,string nameOfClass)
        {
            GradePoint = grade;
            Credits = numberOfCredits;
            ClassName = nameOfClass;
        }
    }
}


Here is the GPACalcualte class

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

namespace GPACalculatorProgram
{
    class GPACalculator
    {
        //a generic list object that stores Grades
        private List gradeList;

        //constructor
        public GPACalculator()
        {
            gradeList= new List();
        }
        
        //method to add grades to list
        public void AddGrade(Grade grade)
        {
            gradeList.Add(grade);
        }

        //calculate GPA
        public double CalculateGPA()
        {
            double gpa = 0;
            double weight=0;
            int totalCredits = 0;
            foreach (Grade g in gradeList)
            {
                weight += g.Credits * g.GradePoint;
                totalCredits += g.Credits;
            }

            gpa = weight / totalCredits;
            return gpa;
        }

        //return the gradelist with the grades
        public List GetGrades()
        {
            return gradeList;
        }
    }
}


Here is the Display class

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

namespace GPACalculatorProgram
{
    class Display
    {
        GPACalculator calc;
        //contructor
        public Display()
        {
            calc = new GPACalculator();
        }
        //do inputs
        //get outputs GPA--List the Contents

        //this loops for as long as a user wants to enter
        //grades. It could be made better with some validation
        public void AddGradeInfo()
        {
            string addMore="yes";
            while (addMore.Equals("yes") )
            {
                Console.WriteLine("Enter the Course Name");
                string course = Console.ReadLine();
                Console.WriteLine("Enter your Grade");
                double g = double.Parse(Console.ReadLine());
                Console.WriteLine("Enter the Credits");
                int c = int.Parse(Console.ReadLine());

                //package the information into a grade object
                Grade gr = new Grade(g,c,course);
                //pass the object to the GPACalculator method
                //that adds it to the list
                calc.AddGrade(gr);

                Console.WriteLine("Do you want to add another Grade? Yes/No");
                addMore = Console.ReadLine().ToLower();

            }
        }

            public void GetGPA()
            {
                //show the gpa
                Console.WriteLine(calc.CalculateGPA().ToString("F2"));
            }

            public void showGrades()
            {
                //list out all the grade
                List gr = calc.GetGrades();
                Console.WriteLine("Course\tGrade\tCredits");
                foreach (Grade g in gr)
                {
                    
                    Console.WriteLine(g.ClassName + "\t" + g.GradePoint + "\t" + g.Credits);
                }
            }
        }
    }



Finally the Program

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

namespace GPACalculatorProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Display d = new Display();
            d.AddGradeInfo();
            d.GetGPA();
            d.showGrades();
            Console.ReadKey();
        }
    }
}

Here are the classes as they look in Visual Studio's Class Diagram

Here is the sequence diagram we generated in visual Studio

No comments:

Post a Comment