Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Bad design of class (first OOP)

This is homework!!! Please do not interpret this as me asking for someone to code for me.

My Program: http://pastebin.com/SZP2dS8D

This is my first OOP. The program works just fine without user input(UI), but the implementation of it renders my design partially ineffective. I am not using a List collection because of assignment restrictions. My main goal is to have everything running from the Transcript class. Here are some issues I am running into:

  • Allowing the user to add new course without having to create a new instance of Transcript
    each time
  • Associating the Courses added to a specific Quarter

Here is some pseudo code to show what I am trying to accomplish. I have been experimenting with it, but have yet to succeed.

Please enter the quarter: (user input)
Would you like to add a course? 

while (true)

Enter Course/Credits/Grade

//new Course information populated with user input 
transcript.AddCourse.to specific Quarter((Fall 2013) new Course("Math 238", 5, 3.9));
transcript.AddCourse.to specific Quarter((Fall 2013) new Course("Phys 223", 5, 3.8));
transcript.AddCourse.to specific Quarter((Fall 2013) new Course("Chem 162", 5, 3.8));

MY QUESTION[S]: Should I keep the Transcript class, or discard it? With the current functionality of creating a new course, is it possible to keep it this way while using UI, or do I need to head back to the chalk board and reconfigure?

Hopefully this is coherent and not too broad. If clarification is needed please ask and I will me more than happy to provide more details.

like image 619
Leif Avatar asked Jun 09 '13 02:06

Leif


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What are the basics of C?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

I would consider the following compositon

public class Transcript
{
  public Quarter[] Quarters{get;set;}
}

public class Quarter
{
  public Course[] Courses{get;set;}
}

You only need one instance of the transcript class. This will enable you to model n quarters (multiple years) with n courses per quarter.

In your input loop you can add new courses/quarters in response to user input

like image 172
TGH Avatar answered Sep 18 '22 02:09

TGH


There are a lot of ways to model this problem and I think you're right to have a transcript class, but instead of thinking that a quarter has a set of courses I would suggest that which quarter a course is offered is a property of the course. For example:

public class Transcript
{
    private List<Course> courses_ = new List<Course>();

    public IEnumerable<Course> Courses {get { return courses_; }

    public IEnumerable<Course> GetCoursesFor(int year, int quarter)
    {
        return courses_.Where(course => course.Year == year && course.Quarter == quarter);
    }

    public void AddCourse(Course course)
    {
        courses_.Add(course);
    }
}


public class Course
{
    public int Year {get; private set;}
    public int Quarter {get; private set;}
    // ... other members
}
like image 21
Dan Busha Avatar answered Sep 18 '22 02:09

Dan Busha