简体   繁体   English

C#:糟糕的类设计(第一个OOP)

[英]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 我的计划: http//pastebin.com/SZP2dS8D

This is my first OOP. 这是我的第一个OOP。 The program works just fine without user input(UI), but the implementation of it renders my design partially ineffective. 该程序在没有用户输入(UI)的情况下工作得很好,但是它的实现使我的设计部分无效。 I am not using a List collection because of assignment restrictions. 由于赋值限制,我没有使用List集合。 My main goal is to have everything running from the Transcript class. 我的主要目标是让一切都从Transcript类运行。 Here are some issues I am running into: 以下是我遇到的一些问题:

  • Allowing the user to add new course without having to create a new instance of Transcript 允许用户添加新课程而无需创建新的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? 我的问题[S]:我应该保留成绩单课程,还是丢弃它? 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? 使用创建新课程的当前功能,是否可以在使用UI时保持这种方式,或者我是否需要返回粉笔板并重新配置?

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. 如果需要澄清,请询问,我将非常乐意提供更多详细信息。

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. 您只需要一个transcript类的实例。 This will enable you to model n quarters (multiple years) with n courses per quarter. 这将使您能够为每季度n个课程的n个季度(多年)建模。

In your input loop you can add new courses/quarters in response to user input 在输入循环中,您可以添加新的课程/季度以响应用户输入

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
}

you can try this 你可以试试这个

public enum Quarters
    {
        First,
        Second,
        Third,
        Fourth
    }
    class Courses
    {
        private Quarters ThisQuarter { get; private set; }
        private List<Tuple<Quarters, List<Courses>>> SchoolProgram = new List<Tuple<Quarters, List<Courses>>>();

        public int year { get; private set; }
        public string name { get; private set; }

        private Courses()
        {
            //load list from database or xml
            //each tuple has one quarters and a list
            // of associated courses
            //SchoolProgram.Add(new Tuple<Quarters, List<Courses>>(Quarters.First, new List<Courses>(){new Courses(2010,"Math",Quarters.First),
            //                                                                                       new Courses(2010,"English",Quarters.First),
            //                                                                                       new Courses(2010,"Physics",Quarters.First)}));
        }

        public Courses(int year,string name,Quarters q)
        {
            this.year = year;
            this.name = name;
            ThisQuarter = q;

        }

        public Courses GetCourse()
        {
            return SchoolProgram.Find(q => q.Item1 == ThisQuarter).Item2.Single(c => (c.year == this.year && c.name == this.name));
        }
    }

    public class Transcript
    {
        private List<Courses> SchoolProgram = new List<Courses>();

        public Transcript()
        {
            //maybe aditional logic here
        }

        public void AddCourse(int year,string name,Quarters q)
        {
            Courses c = new Courses(year, name, q);
            SchoolProgram.Add(c.GetCourse());
        }
    }

you can add additional logic about the grades and other stuff....best wishes 你可以添加关于成绩和其他东西的额外逻辑....祝福

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM