djjd47130

Quiz Objects

Sep 27th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.79 KB | None | 0 0
  1. (*
  2.   Quiz.pas - contains object structure of quiz questions and answers
  3.  
  4.   There are two major parts this unit contains:
  5.   - TQuizList - contains a list of quizes which can be taken
  6.   - TQuiz - contains copmlete quiz with questions and answers
  7.  
  8.  
  9. *)
  10.  
  11. unit Quiz;
  12.  
  13. interface
  14.  
  15. uses
  16.   System.Classes, System.SysUtils, System.Types, System.Generics.Collections,
  17.   FMX.Graphics;
  18.  
  19. type
  20.   TQuizList = class;
  21.   TQuizListItem = class;
  22.   TQuiz = class;
  23.   TQuizQuestionList = class;
  24.   TQuizQuestion = class;
  25.   TQuizQuestionContentList = class;
  26.   TQuizQuestionContent = class;
  27.   TQuizQuestionChoiceList = class;
  28.   TQuizQuestionChoice = class;
  29.  
  30.  
  31.  
  32.   //Contains a list of TQuizListItem - typically listing results of quiz search
  33.   TQuizList = class(TObject)
  34.   private
  35.     FItems: TObjectList<TQuizListItem>;
  36.     function GetItem(Index: Integer): TQuizListItem;
  37.   public
  38.     constructor Create;
  39.     destructor Destroy; override;
  40.     function Count: Integer;
  41.     procedure Clear;
  42.     property Items[Index: Integer]: TQuizListItem read GetItem;
  43.   end;
  44.  
  45.   //Contains basic information of a quiz to be displayed in a list
  46.   TQuizListItem = class(TObject)
  47.   private
  48.     FOwner: TQuizList;
  49.     FQuizId: Integer;
  50.     FTitle: String;
  51.     FDescription: TStringList;
  52.     FCategory: String;
  53.     FTags: TStringList;
  54.     procedure SetCategory(const Value: String);
  55.     procedure SetTitle(const Value: String);
  56.     procedure SetQuizId(const Value: Integer);
  57.     function GetTags: TStrings;
  58.     procedure SetTags(const Value: TStrings);
  59.     function GetDescription: TStrings;
  60.     procedure SetDescription(const Value: TStrings);
  61.   public
  62.     constructor Create(AOwner: TQuizList);
  63.     destructor Destroy; override;
  64.     //Unique identifier of a particular quiz
  65.     property QuizId: Integer read FQuizId write SetQuizId;
  66.     //Display title of the quiz
  67.     property Title: String read FTitle write SetTitle;
  68.     //Detailed multi-line description of the quiz
  69.     property Description: TStrings read GetDescription write SetDescription;
  70.     //Category of the quiz
  71.     property Category: String read FCategory write SetCategory;
  72.     //Specific tags related to the quiz
  73.     property Tags: TStrings read GetTags write SetTags;
  74.   end;
  75.  
  76.  
  77.  
  78.  
  79.   //Contains detailed information of a quiz, including questions and answers
  80.   TQuiz = class(TObject)
  81.   private
  82.     FTitle: String;
  83.     FCategory: String;
  84.     FQuestions: TQuizQuestionList;
  85.     FQuizId: Integer;
  86.     procedure SetCategory(const Value: String);
  87.     procedure SetTitle(const Value: String);
  88.     procedure SetQuizId(const Value: Integer);
  89.   public
  90.     constructor Create;
  91.     destructor Destroy; override;
  92.     property QuizId: Integer read FQuizId write SetQuizId;
  93.     property Title: String read FTitle write SetTitle;
  94.     property Category: String read FCategory write SetCategory;
  95.     property Questions: TQuizQuestionList read FQuestions;
  96.   end;
  97.  
  98.   //Contains list of all questions in a quiz, including answers
  99.   TQuizQuestionList = class(TObject)
  100.   private
  101.     FOwner: TQuiz;
  102.     FTotalCount: Integer;
  103.     FItems: TObjectList<TQuizQuestion>;
  104.     function GetItem(Index: Integer): TQuizQuestion;
  105.   public
  106.     constructor Create(AOwner: TQuiz);
  107.     destructor Destroy; override;
  108.     function LoadedCount: Integer;
  109.     function TotalCount: Integer;
  110.     function LoadNext: TQuizQuestion;
  111.     property Items[Index: Integer]: TQuizQuestion read GetItem; default;
  112.   end;
  113.  
  114.   //Contains details of a quiz question, including content variation and answer
  115.   TQuizQuestion = class(TObject)
  116.   private
  117.     FOwner: TQuizQuestionList;
  118.     FQuestionId: Integer;
  119.     FContents: TQuizQuestionContentList;
  120.     FChoices: TQuizQuestionChoiceList;
  121.     FCorrectAnswerId: Integer;
  122.     FSelectedAnswerId: Integer;
  123.     procedure SetQuestionId(const Value: Integer);
  124.   public
  125.     constructor Create(AOwner: TQuizQuestionList);
  126.     destructor Destroy; override;
  127.     property QuestionId: Integer read FQuestionId write SetQuestionId;
  128.     function CorrectAnswer: TQuizQuestionChoice;
  129.     function SelectedAnswer: TQuizQuestionChoice;
  130.   end;
  131.  
  132.   //Contains list of content variations of a question
  133.   TQuizQuestionContentList = class(TObject)
  134.   private
  135.     FOwner: TQuizQuestion;
  136.     FItems: TObjectList<TQuizQuestionContent>;
  137.   public
  138.     constructor Create(AOwner: TQuizQuestion);
  139.     destructor Destroy; override;
  140.   end;
  141.  
  142.   //Contains a single variation of a single question
  143.   TQuizQuestionContent = class(TObject)
  144.   private
  145.     FOwner: TQuizQuestionContentList;
  146.     FContent: TStringList;
  147.     function GetContent: TStrings;
  148.     procedure SetContent(const Value: TStrings);
  149.   public
  150.     constructor Create(AOwner: TQuizQuestionContentList);
  151.     destructor Destroy; override;
  152.     property Content: TStrings read GetContent write SetContent;
  153.   end;
  154.  
  155.   //Contains all possible multiple choice answers to a question
  156.   TQuizQuestionChoiceList = class(TObject)
  157.   private
  158.     FOwner: TQuizQuestion;
  159.     FItems: TObjectList<TQuizQuestionChoice>;
  160.   public
  161.     constructor Create(AOwner: TQuizQuestion);
  162.     destructor Destroy; override;
  163.     function Count: Integer;
  164.   end;
  165.  
  166.   //Contains a single multiple choice answer to a question
  167.   TQuizQuestionChoice = class(TObject)
  168.   private
  169.     FOwner: TQuizQuestionChoiceList;
  170.     FText: String;
  171.     FPicture: TBitmap;
  172.   public
  173.     constructor Create(AOwner: TQuizQuestionChoiceList);
  174.     destructor Destroy; override;
  175.   end;
  176.  
  177. implementation
  178.  
  179. { TQuizList }
  180.  
  181. constructor TQuizList.Create;
  182. begin
  183.   FItems:= TObjectList<TQuizListItem>.Create(True);
  184. end;
  185.  
  186. destructor TQuizList.Destroy;
  187. begin
  188.   FItems.DisposeOf;
  189.   inherited;
  190. end;
  191.  
  192. function TQuizList.GetItem(Index: Integer): TQuizListItem;
  193. begin
  194.   Result:= FItems[Index];
  195. end;
  196.  
  197. procedure TQuizList.Clear;
  198. begin
  199.   FItems.Clear;
  200. end;
  201.  
  202. function TQuizList.Count: Integer;
  203. begin
  204.   Result:= FItems.Count;
  205. end;
  206.  
  207. { TQuizListItem }
  208.  
  209. constructor TQuizListItem.Create(AOwner: TQuizList);
  210. begin
  211.   FOwner:= AOwner;
  212.   FDescription:= TStringList.Create;
  213.   FTags:= TStringList.Create;
  214. end;
  215.  
  216. destructor TQuizListItem.Destroy;
  217. begin
  218.   FTags.DisposeOf;
  219.   FDescription.DisposeOf;
  220.   inherited;
  221. end;
  222.  
  223. function TQuizListItem.GetDescription: TStrings;
  224. begin
  225.   Result:= TStrings(FDescription);
  226. end;
  227.  
  228. function TQuizListItem.GetTags: TStrings;
  229. begin
  230.   Result:= TStrings(FTags);
  231. end;
  232.  
  233. procedure TQuizListItem.SetCategory(const Value: String);
  234. begin
  235.   FCategory := Value;
  236. end;
  237.  
  238. procedure TQuizListItem.SetDescription(const Value: TStrings);
  239. begin
  240.   FDescription.Assign(Value);
  241. end;
  242.  
  243. procedure TQuizListItem.SetQuizId(const Value: Integer);
  244. begin
  245.   FQuizId := Value;
  246. end;
  247.  
  248. procedure TQuizListItem.SetTags(const Value: TStrings);
  249. begin
  250.   FTags.Assign(Value);
  251. end;
  252.  
  253. procedure TQuizListItem.SetTitle(const Value: String);
  254. begin
  255.   FTitle := Value;
  256. end;
  257.  
  258. { TQuiz }
  259.  
  260. constructor TQuiz.Create;
  261. begin
  262.   FQuestions:= TQuizQuestionList.Create(Self);
  263. end;
  264.  
  265. destructor TQuiz.Destroy;
  266. begin
  267.   FQuestions.DisposeOf;
  268.   inherited;
  269. end;
  270.  
  271. procedure TQuiz.SetCategory(const Value: String);
  272. begin
  273.   FCategory := Value;
  274. end;
  275.  
  276. procedure TQuiz.SetQuizId(const Value: Integer);
  277. begin
  278.   FQuizId := Value;
  279. end;
  280.  
  281. procedure TQuiz.SetTitle(const Value: String);
  282. begin
  283.   FTitle := Value;
  284. end;
  285.  
  286. { TQuizQuestionList }
  287.  
  288. constructor TQuizQuestionList.Create(AOwner: TQuiz);
  289. begin
  290.   FOwner:= AOwner;
  291.   FItems:= TObjectList<TQuizQuestion>.Create(True);
  292. end;
  293.  
  294. destructor TQuizQuestionList.Destroy;
  295. begin
  296.   FItems.DisposeOf;
  297.   inherited;
  298. end;
  299.  
  300. function TQuizQuestionList.GetItem(Index: Integer): TQuizQuestion;
  301. begin
  302.   Result:= FItems[Index];
  303. end;
  304.  
  305. function TQuizQuestionList.LoadedCount: Integer;
  306. begin
  307.   Result:= FItems.Count;
  308. end;
  309.  
  310. function TQuizQuestionList.LoadNext: TQuizQuestion;
  311. begin
  312.   //TODO: Execute event which requests next question to be loaded
  313.  
  314.  
  315.  
  316. end;
  317.  
  318. function TQuizQuestionList.TotalCount: Integer;
  319. begin
  320.   Result:= FTotalCount;
  321. end;
  322.  
  323. { TQuizQuestion }
  324.  
  325. constructor TQuizQuestion.Create(AOwner: TQuizQuestionList);
  326. begin
  327.   FOwner:= AOwner;
  328.   FContents:= TQuizQuestionContentList.Create;
  329.   FChoices:= TQuizQuestionChoiceList.Create(Self)
  330. end;
  331.  
  332. destructor TQuizQuestion.Destroy;
  333. begin
  334.   FChoices.DisposeOf;
  335.   FContents.DisposeOf;
  336.   inherited;
  337. end;
  338.  
  339. procedure TQuizQuestion.SetQuestionId(const Value: Integer);
  340. begin
  341.   FQuestionId := Value;
  342. end;
  343.  
  344. function TQuizQuestion.CorrectAnswer: TQuizQuestionChoice;
  345. var
  346.   X: Integer;
  347. begin
  348.   Result:= nil;
  349.   for X := 0 to FChoices.Count-1 do begin
  350.  
  351.  
  352.  
  353.   end;
  354. end;
  355.  
  356. function TQuizQuestion.SelectedAnswer: TQuizQuestionChoice;
  357. begin
  358.  
  359. end;
  360.  
  361. { TQuizQuestionContentList }
  362.  
  363. constructor TQuizQuestionContentList.Create(AOwner: TQuizQuestion);
  364. begin
  365.   FOwner:= AOwner;
  366.   FItems:= TObjectList<TQuizQuestionContent>.Create(True);
  367. end;
  368.  
  369. destructor TQuizQuestionContentList.Destroy;
  370. begin
  371.   FItems.DisposeOf;
  372.   inherited;
  373. end;
  374.  
  375. { TQuizQuestionContent }
  376.  
  377. constructor TQuizQuestionContent.Create(AOwner: TQuizQuestionContentList);
  378. begin
  379.   FOwner:= AOwner;
  380.   FContent:= TStringList.Create;
  381. end;
  382.  
  383. destructor TQuizQuestionContent.Destroy;
  384. begin
  385.   FContent.DisposeOf;
  386.   inherited;
  387. end;
  388.  
  389. function TQuizQuestionContent.GetContent: TStrings;
  390. begin
  391.   Result:= TStrings(FContent);
  392. end;
  393.  
  394. procedure TQuizQuestionContent.SetContent(const Value: TStrings);
  395. begin
  396.   FContent.Assign(Value);
  397. end;
  398.  
  399. { TQuizQuestionChoiceList }
  400.  
  401. constructor TQuizQuestionChoiceList.Create(AOwner: TQuizQuestion);
  402. begin
  403.   FOwner:= AOwner;
  404. end;
  405.  
  406. destructor TQuizQuestionChoiceList.Destroy;
  407. begin
  408.  
  409.   inherited;
  410. end;
  411.  
  412. function TQuizQuestionChoiceList.Count: Integer;
  413. begin
  414.   Result:= Fitems.Count;
  415. end;
  416.  
  417. { TQuizQuestionChoice }
  418.  
  419. constructor TQuizQuestionChoice.Create(AOwner: TQuizQuestionChoiceList);
  420. begin
  421.   FOwner:= AOwner;
  422. end;
  423.  
  424. destructor TQuizQuestionChoice.Destroy;
  425. begin
  426.  
  427.   inherited;
  428. end;
  429.  
  430. end.
Advertisement
Add Comment
Please, Sign In to add comment