OOP ( Containment )
Task 1.2
Module Module1
Class Test
Private
TestID As String
Private
Questions(10) As Question
Private
NumberOfQuestions As Integer
Private
MaxMarks As Integer
Private
Level As Char
Private
DateSet As Date
Private
markCount As Integer
= 0
Private
flag As Boolean
= False
Public Sub DesignTest()
markCount = 0
Dim
qID As String
Dim
qText As String
Dim
Ans As String
Dim
Mar As Integer
= 0
Dim
Top As String
Console.Write(" Enter test ID: ")
TestID = Console.ReadLine()
Console.Write(" Enter number of questions: ")
Do
NumberOfQuestions =
Console.ReadLine()
If
NumberOfQuestions > 10 Then
Console.WriteLine()
Console.Write(" Invalid. The maximum number of questions is
10.")
Console.Write(" Enter number of questions again: ")
Else
flag = True
End
If
Loop
Until flag = True
flag = False
Console.Write(" Enter maximum number of marks: ")
MaxMarks = Console.ReadLine()
Console.Write(" Enter the test Level ( A , S, G ): ")
Do
Level = Console.ReadLine()
Level = UCase(Level)
If
Level = "A" Or Level = "S"
Or Level = "G"
Then
flag = True
Else
Console.WriteLine()
Console.Write("Invalid. Valid test levels are ( A, S, G): ")
End
If
Loop
Until flag = True
flag = False
Console.Write(" Enter the test date: ")
DateSet = Console.ReadLine()
Console.WriteLine()
For
counter As Integer
= 1 To NumberOfQuestions
Console.Write(" Enter question {0} QuestionID: ",
counter)
qID = Console.ReadLine()
Console.Write(" Enter Question Topic: ")
Top = Console.ReadLine()
Console.Write(" Enter question {0} Question text: ",
counter)
qText = Console.ReadLine()
Console.Write(" Enter question {0} question answer: ",
counter)
Ans = Console.ReadLine()
Console.Write(" Enter question {0} Question mark: ",
counter)
Do
Mar = Console.ReadLine()
markCount = Mar + markCount
If
markCount > MaxMarks Then
markCount = markCount -
Mar
Console.WriteLine()
Console.Write(" Invalid. You cannot give mark greater than maximum number of marks. Remaining number of marks is {0}. Enter again: ",
(MaxMarks - markCount))
Else
flag = True
End
If
Loop
Until flag = True
flag = False
Questions(counter) = New Question
Questions(counter).SetQuestion(qID, qText, Ans, Mar, Top)
Console.WriteLine()
Next
End Sub
Public Sub PrintTest()
Console.WriteLine("Test ID: " & TestID)
Console.WriteLine("Test Date: " & DateSet)
Console.WriteLine("Test Leve: " & Level)
Console.WriteLine("Number of questions in a test: " &
NumberOfQuestions)
Console.WriteLine()
For
counter As Integer
= 1 To NumberOfQuestions
Console.WriteLine(" Question {0} Topic: {1} ", counter,
Questions(counter).GetTopic)
Console.WriteLine(" Question {0} ID and Text: {1} ",
counter, Questions(counter).GetQuestion)
Console.WriteLine(" Question {0} mark: {1} ", counter,
Questions(counter).GetMarks)
Console.WriteLine()
Next
End Sub
Public Sub PrintAnswers()
Console.WriteLine("Test ID: " & TestID)
Console.WriteLine("Test Date: " & DateSet)
Console.WriteLine("Test Leve: " & Level)
Console.WriteLine("Number of questions in a test: " &
NumberOfQuestions)
Console.WriteLine()
For
counter As Integer
= 1 To NumberOfQuestions
Console.WriteLine(" Question {0} Topic: {1}", counter,
Questions(counter).GetTopic)
Console.WriteLine(" Answer of Question {0}: {1}", counter,
Questions(counter).GetAnswer)
Console.WriteLine(" Question {0} mark: {1}", counter,
Questions(counter).GetMarks)
Console.WriteLine()
Next
End Sub
End Class
Class Question
Private
QuestionID As String
Private
QuestionText As String
Private
Answer As String
Private
Marks As Integer
Private
Topic As String
Public Sub SetQuestion(ByVal
qID As String, ByVal qText As String, ByVal Ans As String, ByVal Mar As Integer, ByVal Top As String)
QuestionID = qID
QuestionText = qText
Answer = Ans
Marks = Mar
Topic = Top
End Sub
Public Function GetQuestion() As
String
Return
(QuestionID & " " &
QuestionText)
End Function
Public Function GetMarks() As
Integer
Return
(Marks)
End Function
Public Function GetTopic() As
String
Return
(Topic)
End Function
Public Function GetAnswer() As
String
Return
(Answer)
End Function
End Class
Sub Main()
Dim
access As Test = New
Test()
Dim
choice As Integer
choice = 0
Do
Console.WriteLine("1. Design Test")
Console.WriteLine("2. Print Question")
Console.WriteLine("3. Print Answer")
Console.WriteLine("4. Exit")
choice = Console.ReadLine()
Console.Clear()
Select
Case choice
Case
1
access.DesignTest()
Console.ReadLine()
Console.Clear()
Case
2
access.PrintTest()
Console.ReadLine()
Console.Clear()
Case
3
access.PrintAnswers()
Console.ReadLine()
Console.Clear()
End
Select
Loop Until choice = 4
End Sub
End Module