This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

[Visual Basic 2010] Chapter 11 College Registration Cost

By: kplusn on May 8th, 2012  |  syntax: VisualBasic  |  size: 11.34 KB  |  views: 269  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ' Program:      College Registration Costs
  2. ' Developer:    XXXXX
  3. ' Date:         April 12, 2012
  4. ' Purpose:      This program calculates the registration costs for a college
  5. '               student. It also records the costs in a text file.
  6.  
  7. Option Strict On
  8.  
  9. Public Class frmRegistrationCostForm
  10.  
  11.     Private Sub btnCalculatesCosts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculatesCosts.Click
  12.         'This Calculate Costs button click event handler edits the
  13.        ' registration (costs) form to ensure it contains valid data.
  14.        ' Then, after passing control to the business class, it
  15.        ' displays the registration cost.
  16.  
  17.         Dim objStudent As Student
  18.         Dim objOnCampusStudent As OnCampusStudent
  19.         Dim InputError As Boolean = False
  20.  
  21.         ' Is student ID entered properly
  22.        If txtStudentID.MaskFull = False Then
  23.             MsgBox("Enter your Student ID in the Student ID box", , _
  24.                    "Error")
  25.             txtStudentID.Clear()
  26.             txtStudentID.Focus()
  27.             InputError = True
  28.             ' Is student name entered properly
  29.        ElseIf txtStudentName.TextLength < 1 Or _
  30.             txtStudentName.Text < "A" Then
  31.             MsgBox("Enter your name in the Student Name box", , "Error")
  32.             txtStudentName.Clear()
  33.             txtStudentName.Focus()
  34.             InputError = True
  35.             ' Is number of units entered properly
  36.        ElseIf Not IsNumeric(txtNumberOfUnits.Text) Then
  37.             MsgBox("Enter the units in the Number of Units box", , _
  38.                    "Error")
  39.             txtNumberOfUnits.Clear()
  40.             txtNumberOfUnits.Focus()
  41.             ' Has 1-24 units been entered
  42.        ElseIf Convert.ToInt32(txtNumberOfUnits.Text) < 1 _
  43.             Or Convert.ToInt32(txtNumberOfUnits.Text) > 24 Then
  44.             MsgBox("Units must be 1-24", , "Error")
  45.             txtNumberOfUnits.Clear()
  46.             txtNumberOfUnits.Focus()
  47.             InputError = True
  48.             ' Has a major been selected
  49.        ElseIf cboMajor.SelectedIndex < 0 Then
  50.             MsgBox("Please select a major", , "Error")
  51.             cboMajor.Focus()
  52.             InputError = True
  53.         End If
  54.         ' If no input error, process the registration costs
  55.        If Not InputError Then
  56.             If radOffCampus.Checked Then
  57.                 objStudent = New Student(txtStudentID.Text, _
  58.                                          txtStudentName.Text, Convert.ToString(cboMajor.SelectedItem), _
  59.                                          txtNumberOfUnits.Text)
  60.                 lblCosts.Visible = True
  61.                 lblCosts.Text = "Total semester costs are: " _
  62.                     & (objStudent.ComputeCosts()).ToString("C2")
  63.             Else
  64.                 objOnCampusStudent = New OnCampusStudent(txtStudentID.Text, _
  65.                                                          txtStudentName.Text, Convert.ToString(cboMajor.SelectedItem), _
  66.                                                          txtNumberOfUnits.Text, radCooperDorm.Checked, _
  67.                                                          radPercyHall.Checked, radJulianSuites.Checked)
  68.                 lblCosts.Visible = True
  69.                 lblCosts.Text = "Total semester costs are: " _
  70.                     & (objOnCampusStudent.ComputeCosts()).ToString("C2")
  71.             End If
  72.         End If
  73.  
  74.     End Sub
  75.  
  76.     Private Sub radOffCampus_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOffCampus.CheckedChanged
  77.         ' This event handler is executed when the Off Campus radio
  78.        ' button is selected. It hides the Housing/Board radio buttons
  79.  
  80.         grpHousingBoard.Visible = False
  81.  
  82.     End Sub
  83.  
  84.     Private Sub radOnCampus_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOnCampus.CheckedChanged
  85.         ' This event handler is executed when the On Campus radio button
  86.        ' is selected. It makes the Housing/Board radio buttons visible
  87.  
  88.         grpHousingBoard.Visible = True
  89.  
  90.     End Sub
  91.  
  92.     Private Sub btnClearForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearForm.Click
  93.         ' This event handler is executed when the user clicks the
  94.        ' Clear Form button. It resets all objects on the user interface.
  95.  
  96.         txtStudentID.Clear()
  97.         txtStudentName.Clear()
  98.         txtNumberOfUnits.Clear()
  99.         cboMajor.SelectedIndex = -1
  100.         cboMajor.Text = "Select a Major"
  101.         radOffCampus.Checked = True
  102.         radCooperDorm.Checked = True
  103.         grpHousingBoard.Visible = False
  104.         lblCosts.Visible = False
  105.         txtStudentID.Focus()
  106.  
  107.     End Sub
  108. End Class
  109.  
  110. ---------------------------------------------------------------------------------------------------------
  111. ---------------------------------------------------------------------------------------------------------
  112. ---------------------------------------------------------------------------------------------------------
  113.  
  114. ' Class:        Student
  115. ' Developer:    XXXXX
  116. ' Date:         April 12, 2012
  117. ' Purpose:      This business class for a registering college student
  118. '               calculates the semester costs for tuition. It also causes the
  119. '               student costs file to be written.
  120.  
  121. Option Strict On
  122.  
  123. Public Class Student
  124.  
  125.     ' Class variables
  126.    Protected _strStudentID As String
  127.     Protected _strStudentName As String
  128.     Protected _strMajor As String
  129.     Protected _intUnits As Integer
  130.     Protected _decCost As Decimal
  131.     Protected _decCostsPerUnit As Decimal = 450D
  132.  
  133.     Dim objStudentCostsFile As StudentCostsFile
  134.  
  135.     Sub New(ByVal strStudentID As String, ByVal strStudentName As String, _
  136.             ByVal strMajor As String, ByVal intUnits As String)
  137.         ' This subprocedure is a constructor for the Student class. It is
  138.        ' called when the object is instantiated with arguments
  139.  
  140.         ' The following code assigns the arguments to class variables
  141.        _strStudentID = strStudentID
  142.         _strStudentName = strStudentName
  143.         _strMajor = strMajor
  144.         _intUnits = Convert.ToInt32(intUnits)
  145.  
  146.     End Sub
  147.  
  148.     Overridable Function ComputeCosts() As Decimal
  149.         ' This function computes the registration costs, writes a record
  150.        ' in the student cost file, and returns the registration costs
  151.  
  152.         ' Calculate cost
  153.        _decCost = _intUnits * _decCostsPerUnit
  154.  
  155.         ' Write the student record
  156.        objStudentCostsFile = New StudentCostsFile(_strStudentID, _
  157.                                                    _strStudentName, _strMajor, _decCost)
  158.         objStudentCostsFile.WriteRecords()
  159.  
  160.         ' Return the calculated cost
  161.        Return _decCost
  162.  
  163.     End Function
  164.  
  165. End Class
  166.  
  167. ---------------------------------------------------------------------------------------------------------
  168. ---------------------------------------------------------------------------------------------------------
  169. ---------------------------------------------------------------------------------------------------------
  170.  
  171. ' Class:        OnCampusStudent
  172. ' Developer:    XXXXX
  173. ' Date:         April 12, 2012
  174. ' Purpose:      This business class for registering an on-campus college
  175. '               student calculates the semester costs, including tuition
  176. '               and housing. It also causes the student costs file to be written.
  177.  
  178. Option Strict On
  179.  
  180. Public Class OnCampusStudent
  181.     Inherits Student
  182.  
  183.     ' Class variables
  184.    Private _Cooper As Boolean
  185.     Private _Percey As Boolean
  186.     Private _Julian As Boolean
  187.  
  188.     Dim objStudentCostsFile As StudentCostsFile
  189.  
  190.     Sub New(ByVal StudentID As String, ByVal StudentName As String, _
  191.             ByVal Major As String, ByVal Units As String, _
  192.             ByVal Cooper As Boolean, ByVal Percey As Boolean, _
  193.             ByVal Julian As Boolean)
  194.         ' This subprocedure is a constructor for the Student class. It
  195.        ' is called when instantiated with arguments
  196.  
  197.         MyBase.New(StudentID, StudentName, Major, Units)
  198.  
  199.         ' The following code assigns the arguments to class variables
  200.        _Cooper = Cooper
  201.         _Percey = Percey
  202.         _Julian = Julian
  203.     End Sub
  204.  
  205.     Overrides Function ComputeCosts() As Decimal
  206.         ' This function computes the registration costs, writes a record
  207.        ' in the student costs file, and returns the registration costs
  208.  
  209.         ' Define variables
  210.        Dim HousingCost As Decimal
  211.         Const cdecCooperHousingCost As Decimal = 2900D
  212.         Const cdecPerceyHousingCost As Decimal = 3400D
  213.         Const cdecJulianHousingCost As Decimal = 4000D
  214.  
  215.         ' Calculate the cost
  216.        If _Cooper Then
  217.             HousingCost = cdecCooperHousingCost
  218.         ElseIf _Percey Then
  219.             HousingCost = cdecPerceyHousingCost
  220.         ElseIf _Julian Then
  221.             HousingCost = cdecJulianHousingCost
  222.         End If
  223.  
  224.         _decCost = (_intUnits * _decCostsPerUnit) + HousingCost
  225.  
  226.         'Write the student record
  227.        objStudentCostsFile = New StudentCostsFile(_strStudentID, _
  228.                                                    _strStudentName, _strMajor, _decCost)
  229.         objStudentCostsFile.WriteRecords()
  230.  
  231.         ' Return the calculated cost
  232.        Return _decCost
  233.  
  234.     End Function
  235. End Class
  236.  
  237. ---------------------------------------------------------------------------------------------------------
  238. ---------------------------------------------------------------------------------------------------------
  239. ---------------------------------------------------------------------------------------------------------
  240.  
  241. ' Class:            Student Costs File
  242. ' Developer:        XXXXX
  243. ' Date:             This class represents the Student Costs File. The WriteRecord
  244. '                   procedure writes a comma-delimited student costs file that
  245. '                   contains the student ID, Student Name, Major,
  246. '                   and Student Costs.
  247.  
  248. Option Strict On
  249.  
  250. Public Class StudentCostsFile
  251.  
  252.     ' Class variables
  253.    Private _strStudentID As String
  254.     Private _strStudentName As String
  255.     Private _strMajor As String
  256.     Private _decStudentCosts As Decimal
  257.  
  258.     Sub New(ByVal StudentID As String, ByVal StudentName As String, _
  259.             ByVal Major As String, ByVal Costs As Decimal)
  260.         ' This sub procedure is the constructor for the StudentCostsFile
  261.        ' class.
  262.  
  263.         ' The following code assigns the arguments to class variables
  264.        _strStudentID = StudentID
  265.         _strStudentName = StudentName
  266.         _strMajor = Major
  267.         _decStudentCosts = Costs
  268.  
  269.     End Sub
  270.  
  271.     Sub WriteRecords()
  272.         ' This subprocedure opens the StudentCosts ouput text file and then
  273.        ' writes a record in the comma-delimited file.
  274.  
  275.         Dim strNameandLocationOfFile As String = "E:\StudentCosts.txt"
  276.  
  277.         Try
  278.             Dim objWriter As IO.StreamWriter = _
  279.                 IO.File.AppendText(strNameandLocationOfFile)
  280.  
  281.             objWriter.Write(_strStudentID & ",")
  282.             objWriter.Write(_strStudentName & ",")
  283.             objWriter.Write(_strMajor & ",")
  284.             objWriter.WriteLine(_decStudentCosts)
  285.             objWriter.Close()
  286.  
  287.         Catch ex As Exception
  288.             MsgBox("No device available - program aborted", , "Error")
  289.             Application.Exit()
  290.  
  291.         End Try
  292.  
  293.     End Sub
  294. End Class
clone this paste RAW Paste Data