列出班级时间表

时间:2015-04-10 03:57:44

标签: c#

我完成了我的学生日程安排课程,并且我的教授回复了我的工作,宣布"日程安排是一个列表课程,而不仅仅是studentntid和crn"

现在我把列表放到了问题中,但我得到的错误很大。 我该如何解决这个问题?

我的教授要求: 属性:一个Sections和一个Count的数组。构造函数:只需要一个默认构造函数。行为:添加(部分s)和显示()。 drop()函数是可选的。要测试Schedule类,请实例化一个计划并将2个部分添加到Schedule中,然后显示()Schedule。

我知道这是我测试它的方式:

sch1 = new Schedule(); //create an empty schedule
sch1.add(new Section(30999, “CIST 9999”, “MW6-9pm”, “F1150”, 6));
sch1.add(new Section(30777, “CIST 9998”, “MW1-3pm”, “F1150”, 6));
sch1.display();  // you should see a list of 2 sections in this schedule

时间表:

using System;
using System.Collections.Generic;
using System.Text;

namespace Schedule 
{
    class Schedule : Section
    {
         private int studentID;
         private int cRN;
    private List<Sections> SectionList;



    public Schedule() {
        this.studentID = 0;
         this.cRN = 0;
    }

        //++++++++++++++++  DATABASE Data Elements +++++++++++++++++
        public System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter;
        public System.Data.OleDb.OleDbCommand OleDbSelectCommand;
        public System.Data.OleDb.OleDbCommand OleDbInsertCommand;
        public System.Data.OleDb.OleDbCommand OleDbUpdateCommand;
        public System.Data.OleDb.OleDbCommand OleDbDeleteCommand;
        public System.Data.OleDb.OleDbConnection OleDbConnection;
        public string cmd;

        public void DBSetup(){
        // +++++++++++++++++++++++++++  DBSetup function +++++++++++++++++++++++++++
        // This DBSetup() method instantiates all the DB objects needed to access a DB, 
        // including OleDbDataAdapter, which contains 4 other objects(OlsDbSelectCommand, 
        // oleDbInsertCommand, oleDbUpdateCommand, oleDbDeleteCommand.) And each
        // Command object contains a Connection object and an SQL string object.
            OleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
            OleDbSelectCommand = new System.Data.OleDb.OleDbCommand();
            OleDbInsertCommand = new System.Data.OleDb.OleDbCommand();
            OleDbUpdateCommand = new System.Data.OleDb.OleDbCommand();
            OleDbDeleteCommand = new System.Data.OleDb.OleDbCommand();
            OleDbConnection = new System.Data.OleDb.OleDbConnection();


            OleDbDataAdapter.DeleteCommand = OleDbDeleteCommand;
            OleDbDataAdapter.InsertCommand = OleDbInsertCommand;
            OleDbDataAdapter.SelectCommand = OleDbSelectCommand;
            OleDbDataAdapter.UpdateCommand = OleDbUpdateCommand;


OleDbConnection.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Reg"+
"istry Path=;Jet OLEDB:Database L" + 
"ocking Mode=1;Data Source=C:\Users\Tina\Desktop\RegistrationMDB.accdb;J" + 
"et OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System datab" + 
"ase=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=S" + 
"hare Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet " + 
"OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repai" + 
"r=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";

        }



        public void SelectDB(int studentID) 
        { //++++++++++++++++++++++++++  SELECT +++++++++++++++++++++++++
            DBSetup();
            cmd = "Select * from Courses where StudentID = " + studentID;
            OleDbDataAdapter.SelectCommand.CommandText = cmd;
            OleDbDataAdapter.SelectCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  {
                    OleDbConnection.Open();
                    System.Data.OleDb.OleDbDataReader dr;
                    dr = OleDbDataAdapter.SelectCommand.ExecuteReader();

                    dr.Read();


                setStudentID(Int32.Parse(dr.GetValue(1)+""));
                    setCRN(Int32.Parse(dr.GetValue(1)+""));

            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        } 

        public void InsertDB() {
        // +++++++++++++++++++++++++++  INSERT +++++++++++++++++++++++++++++++

            DBSetup();
            cmd = "INSERT into StudentSchedule values(" + getStudentID() + "," +
                             "'" + getCRN() + ")";

            OleDbDataAdapter.InsertCommand.CommandText = cmd;
            OleDbDataAdapter.InsertCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.InsertCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Inserted");
                else
                    Console.WriteLine("ERROR: Inserting Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                
        } 

        public void updateDB() 
        {
            //++++++++++++++++++++++++++  UPDATE  +++++++++++++++++++++++++

            cmd = "Update StudentSchedule set StudentID = '" + getStudentID() + "'," + 
                        "CRN = '" + getCRN();

            OleDbDataAdapter.UpdateCommand.CommandText = cmd;
            OleDbDataAdapter.UpdateCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.UpdateCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Updated");
                else
                    Console.WriteLine("ERROR: Updating Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }
        public void deleteDB() 
        {   
            //++++++++++++++++++++++++++  DELETE  +++++++++++++++++++++++++

            cmd = "Delete from StudentSchedule where StudentID = " + getStudentID();
            OleDbDataAdapter.DeleteCommand.CommandText = cmd;
            OleDbDataAdapter.DeleteCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.DeleteCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Deleted");
                else
                    Console.WriteLine("ERROR: Deleting Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }

       public void setStudentID(int studentID) {
        this.studentID = studentID;
    }
       public void setCRN(int cRN) {
        this.cRN = cRN;
    }
      public int getStudentID() {
        return studentID;
    } 
       public int getCRN() {
        return cRN;
    } 

         public void display(){
        System.Console.WriteLine("Student ID =  "+ getStudentID());
        System.Console.WriteLine("CRN =   "+ getCRN());



    }
    }
}

部分:

using System;
using System.Collections.Generic;
using System.Text;

    namespace Section

    {
        class Section
        {
            private int cRN;
            private String courseID;
            private String timeDays;
            private String roomNo;
            private int instructor;
            private String message;

            /**
             * No arg constructor that sets the default value of all
             * customer properties to an empty string value.
             */
            public Section()
            {

                this.cRN = 0;
                this.courseID = "";
                this.timeDays = "";
                this.roomNo = "";
                this.instructor = 0;


            }
            public Section(int cRN)
            {
                SelectDB(cRN);
            }



                    //++++++++++++++++  DATABASE Data Elements +++++++++++++++++
            public System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter;
            public System.Data.OleDb.OleDbCommand OleDbSelectCommand;
            public System.Data.OleDb.OleDbCommand OleDbInsertCommand;
            public System.Data.OleDb.OleDbCommand OleDbUpdateCommand;
            public System.Data.OleDb.OleDbCommand OleDbDeleteCommand;
            public System.Data.OleDb.OleDbConnection OleDbConnection;
            public string cmd;

            public void DBSetup(){
            // +++++++++++++++++++++++++++  DBSetup function +++++++++++++++++++++++++++
            // This DBSetup() method instantiates all the DB objects needed to access a DB, 
            // including OleDbDataAdapter, which contains 4 other objects(OlsDbSelectCommand, 
            // oleDbInsertCommand, oleDbUpdateCommand, oleDbDeleteCommand.) And each
            // Command object contains a Connection object and an SQL string object.
                OleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
                OleDbSelectCommand = new System.Data.OleDb.OleDbCommand();
                OleDbInsertCommand = new System.Data.OleDb.OleDbCommand();
                OleDbUpdateCommand = new System.Data.OleDb.OleDbCommand();
                OleDbDeleteCommand = new System.Data.OleDb.OleDbCommand();
                OleDbConnection = new System.Data.OleDb.OleDbConnection();


                OleDbDataAdapter.DeleteCommand = OleDbDeleteCommand;
                OleDbDataAdapter.InsertCommand = OleDbInsertCommand;
                OleDbDataAdapter.SelectCommand = OleDbSelectCommand;
                OleDbDataAdapter.UpdateCommand = OleDbUpdateCommand;


    OleDbConnection.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Reg"+
    "istry Path=;Jet OLEDB:Database L" + 
    "ocking Mode=1;Data Source=C:\Users\Tina\Desktop\RegistrationMDB.accdb;J" + 
    "et OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System datab" + 
    "ase=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=S" + 
    "hare Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet " + 
    "OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repai" + 
    "r=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";

            } 
            public void SelectDB(int crn) 
            { //++++++++++++++++++++++++++  SELECT +++++++++++++++++++++++++
                DBSetup();
                cmd = "Select * from Sections where CRN = " + cRN;
                OleDbDataAdapter.SelectCommand.CommandText = cmd;
                OleDbDataAdapter.SelectCommand.Connection = OleDbConnection;
                Console.WriteLine(cmd);
                try  {
                        OleDbConnection.Open();
                        System.Data.OleDb.OleDbDataReader dr;
                        dr = OleDbDataAdapter.SelectCommand.ExecuteReader();

                        dr.Read();
                        crn=cRN;
                        setCourseID(dr.GetValue(1)+"");
                        setTimeDays(dr.GetValue(2)+"");
                        setRoomNo(dr.GetValue(3)+""); 
                        setInstructor(Int32.Parse(dr.GetValue(4)+""));
                }
                catch (Exception ex) 
                {
                    Console.WriteLine(ex);
                }
                finally 
                {
                    OleDbConnection.Close();
                }                    
            }

                public void InsertDB() {
            // +++++++++++++++++++++++++++  INSERT +++++++++++++++++++++++++++++++

                DBSetup();
                cmd = "INSERT into Section values(" + getCRN() + "," +
                                 "'" + getCourseID() + "'," +
                                 "'" + getTimeDays() + "'," +
                                "'" + getRoomNo() + "'," +
                                 getInstructor() + ")";

                OleDbDataAdapter.InsertCommand.CommandText = cmd;
                OleDbDataAdapter.InsertCommand.Connection = OleDbConnection;
                Console.WriteLine(cmd);
                try  
                {
                    OleDbConnection.Open();
                    int n = OleDbDataAdapter.InsertCommand.ExecuteNonQuery();
                    if (n==1)
                        Console.WriteLine("Data Inserted");
                    else
                        Console.WriteLine("ERROR: Inserting Data");
                }
                catch (Exception ex) 
                {
                    Console.WriteLine(ex);
                }
                finally 
                {
                    OleDbConnection.Close();
                }                
            } 
            public void updateDB() 
            {
                //++++++++++++++++++++++++++  UPDATE  +++++++++++++++++++++++++

                cmd = "Update Sections set CRN = '" + getCRN() + "'," + 
                            "CourseID = '" + getCourseID() +    "', " +
                            "TimeDays = '" + getTimeDays() + "', " +
                             "RoomNo = " + getRoomNo() +  
                             " where Instructor = " + getInstructor();

                OleDbDataAdapter.UpdateCommand.CommandText = cmd;
                OleDbDataAdapter.UpdateCommand.Connection = OleDbConnection;
                Console.WriteLine(cmd);
                try  
                {
                    OleDbConnection.Open();
                    int n = OleDbDataAdapter.UpdateCommand.ExecuteNonQuery();
                    if (n==1)
                        Console.WriteLine("Data Updated");
                    else
                        Console.WriteLine("ERROR: Updating Data");
                }
                catch (Exception ex) 
                {
                    Console.WriteLine(ex);
                }
                finally 
                {
                    OleDbConnection.Close();
                }                    
            }

            public void deleteDB() 
            {   
                //++++++++++++++++++++++++++  DELETE  +++++++++++++++++++++++++

                cmd = "Delete from Sections where CRN = " + getCRN();
                OleDbDataAdapter.DeleteCommand.CommandText = cmd;
                OleDbDataAdapter.DeleteCommand.Connection = OleDbConnection;
                Console.WriteLine(cmd);
                try  
                {
                    OleDbConnection.Open();
                    int n = OleDbDataAdapter.DeleteCommand.ExecuteNonQuery();
                    if (n==1)
                        Console.WriteLine("Data Deleted");
                    else
                        Console.WriteLine("ERROR: Deleting Data");
                }
                catch (Exception ex) 
                {
                    Console.WriteLine(ex);
                }
                finally 
                {
                    OleDbConnection.Close();
                }                    
            } 


            public void setCRN(int cRN)
            {
                this.cRN = cRN;
            }



            public void setCourseID(String courseID)
            {
                this.courseID = courseID;
            }


            public void setTimeDays(String timeDays)
            {
                this.timeDays = timeDays;
            }


            public void setRoomNo(String roomNo)
            {
                this.roomNo = roomNo;
            }

            public void setInstructor(int instructor)
            {
                this.instructor = instructor;
            }



            public int getCRN()
            {
                return cRN;
            }

            public String getCourseID()
            {
                return courseID;
            }


            public String getTimeDays()
            {
                return timeDays;
            }


            public String getRoomNo()
            {
                return roomNo;
            }

            public int getInstructor()
            {
                return instructor;
            }



            public String getMessage()
            {
                return this.message;
            }


        public void display(){
            System.Console.WriteLine("CRN =  "+ getCRN());
            System.Console.WriteLine("CourseID =   "+ getCourseID());
            System.Console.WriteLine("Time Days =  " + getTimeDays());
            System.Console.WriteLine("Room Number =  " + getRoomNo());

        }



        }
    }

Here是学生时间表

This是各个部分。

1 个答案:

答案 0 :(得分:0)

虽然我不喜欢在没有数据库访问的情况下完成其他任务分解,正如问题读取可以分解为两个非常简单的类。

日程安排课程:

/// <summary>
/// Schedule class
/// </summary>
public class Schedule
{
    /// <summary>
    /// Creates a new instance of a schedule
    /// </summary>
    public Schedule()
    {
        this.sections = new List<Section>();
    }

    /// <summary>
    /// collection of sections
    /// </summary>
    ICollection<Section> sections;

    /// <summary>
    /// Adds a section
    /// </summary>
    /// <param name="section"></param>
    public void Add(Section section)
    {
        if (section == null)
            throw new ArgumentNullException("section");

        this.sections.Add(section);
    }

    /// <summary>
    /// Drops a section
    /// </summary>
    /// <param name="section"></param>
    public void Drop(Section section)
    {
        if (section == null)
            throw new ArgumentNullException("section");
        this.sections.Remove(section);
    }

    /// <summary>
    /// Returns an enumerable of all the sections
    /// </summary>
    /// <returns></returns>
    public IEnumerable<Section> Display()
    {
        return this.sections;
    }

}

章节类:

/// <summary>
/// Section class
/// </summary>
public class Section
{
    /// <summary>
    /// Creates a new instance of a section
    /// </summary>
    /// <param name="crn"></param>
    /// <param name="courseId"></param>
    /// <param name="timeDays"></param>
    /// <param name="roomNumber"></param>
    /// <param name="instructor"></param>
    public Section(int crn, string courseId, string timeDays, string roomNumber, int instructor)
        :this(crn, courseId, timeDays, roomNumber, instructor, "")
    {
    }

    /// <summary>
    /// Creates a new instance of a section
    /// </summary>
    /// <param name="crn"></param>
    /// <param name="courseId"></param>
    /// <param name="timeDays"></param>
    /// <param name="roomNumber"></param>
    /// <param name="instructor"></param>
    /// <param name="message"></param>
    public Section(int crn, string courseId, string timeDays, string roomNumber, int instructor, string message)
    {
        this.Crn = crn;
        this.CourseId = courseId;
        this.TimeDays = timeDays;
        this.RoomNumber = roomNumber;
        this.Instructor = instructor;
        this.Message = message;
    }

    /// <summary>
    /// Gets or sets the crn
    /// </summary>
    public int Crn { get; set; }

    /// <summary>
    /// gets or sets the course id
    /// </summary>
    public string CourseId { get; set; }

    /// <summary>
    /// gets or sets the time days
    /// </summary>
    public string TimeDays { get; set; }

    /// <summary>
    /// gets or sets the room number
    /// </summary>
    public string RoomNumber { get; set; }

    /// <summary>
    /// Gets or sets the instructor
    /// </summary>
    public int Instructor { get; set; }

    /// <summary>
    /// Gets or sets the message
    /// </summary>
    public string Message { get; set; }
}

现在,由于这是一项任务,您应该能够讨论它的运作方式。基本上Schedule类包含私有的部分集合。 Add方法通过调用节构造函数来添加Section

如果您正在使用数据访问,那么您只需删除该集合并直接使用数据库。