C# 列出课程表
我完成了我的学生时间表课程,交了上来,教授还给了我的作业,宣称“时间表是一个列表类,而不仅仅是studentid和crn” 好吧,现在我把这个列表放到了问题中,但是我每次都会犯错误。 我如何解决这个问题 我的教授要求: 属性:节数组和计数。构造函数:只需要一个默认构造函数。行为:添加(部分s)和显示()。drop()函数是可选的。要测试Schedule类,请实例化一个计划并向该计划中添加2个部分,然后显示()该计划 我知道这就是我测试它的方式:C# 列出课程表,c#,C#,我完成了我的学生时间表课程,交了上来,教授还给了我的作业,宣称“时间表是一个列表类,而不仅仅是studentid和crn” 好吧,现在我把这个列表放到了问题中,但是我每次都会犯错误。 我如何解决这个问题 我的教授要求: 属性:节数组和计数。构造函数:只需要一个默认构造函数。行为:添加(部分s)和显示()。drop()函数是可选的。要测试Schedule类,请实例化一个计划并向该计划中添加2个部分,然后显示()该计划 我知道这就是我测试它的方式: sch1 = new 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());
}
}
}
这是学生时间表吗
是部分。尽管我不喜欢做其他作业,但在没有数据库访问的情况下简单地将其分解,正如问题所读到的,可以分解为两个非常简单的类 课程表类别:
/// <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;
}
}
//
///课程表
///
公课时间表
{
///
///创建明细表的新实例
///
公共附表(
{
this.sections=新列表();
}
///
///章节集
///
i采集断面;
///
///添加一个节
///
///
公共无效添加(第节)
{
if(节==null)
抛出新的异常(“节”);
本节。添加(节);
}
///
///删除一节
///
///
公共空投(第节)
{
if(节==null)
抛出新的异常(“节”);
本节。删除(节);
}
///
///返回所有节的可枚举项
///
///
公共IEnumerable显示()
{
将此文件退还给我;
}
}
组别:
/// <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; }
}
//
///节课
///
公共课组
{
///
///创建节的新实例
///
///
///
///
///
///
公共部分(int crn、字符串courseId、字符串timeDays、字符串roomNumber、int讲师)
:此(crn、courseId、timeDays、roomNumber、讲师,“”)
{
}
///
///创建节的新实例
///
///
///
///
///
///
///
公共部分(int crn、字符串courseId、字符串timeDays、字符串roomNumber、int讲师、字符串消息)
{
这个.Crn=Crn;
this.CourseId=CourseId;
this.TimeDays=TimeDays;
this.RoomNumber=RoomNumber;
这个。讲师=讲师;
this.Message=消息;
}
///
///获取或设置crn
///
公共int Crn{get;set;}
///
///获取或设置课程id
///
公共字符串CourseId{get;set;}
///
///获取或设置每天的时间
///
公共字符串TimeDays{get;set;}
///
///获取或设置房间号
///
公共字符串RoomNumber{get;set;}
///
///获取或设置讲师
///
公共int讲师{get;set;}
///
///获取或设置消息
///
公共字符串消息{get;set;}
}
既然这是一项作业,你应该能够讨论它是如何运作的。基本上,Schedule
类包含一个节的私有集合。Add
方法通过调用节构造函数来添加一个节
如果您使用的是数据访问,则只需删除集合并直接使用数据库即可。您会遇到什么样的错误?如果您将问题本地化,则会有所帮助,否则此问题不适合StackOverflow。将节添加到计划列表是一个错误。当我看到名为deleteDB()
的函数时,我会感到害怕。它是用于学校的,而不是真实的数据库。这是教我们如何做任何我感到困惑的事情
/// <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; }
}