2015-04-10 2 views
-1

Я закончил свой график студента класс повернул его, и мой профессор вернулся моей работой объявляя, что «Расписание класса списка, а не только studentid и CRN»Список Расписание занятий

Ну теперь я поместил список в проблему но я получаю ошибки большое время. Как это решить?

Мой профессор просит: Свойства: массив разделов и графа. Конструкторы: требуется только конструктор по умолчанию. Поведение: добавить (раздел s) и display(). Функция drop() является необязательной. Чтобы проверить класс Schedule, создайте расписание и добавьте в расписание, возможно, 2 раздела, а затем отобразите() расписание.

Я знаю, что это, как я проверить:

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 - это разделы.

+0

Какие ошибки вы получаете? Это поможет, если вы локализуете проблему, иначе этот вопрос не подходит для StackOverflow. –

+0

Это ошибка добавления раздела в список расписаний – user3741806

+0

Я боюсь, когда вижу функцию с именем 'deleteDB()' – Eric

ответ

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, вызывая конструкторы раздела.

Если вы используете доступ к данным, вы можете просто удалить коллекцию и работать напрямую с базой данных.

Смежные вопросы