-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
244 additions
and
14 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...n-patterns/src/main/java/com/ontotext/javacourse/designpatterns/abstractfactory/Game.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory; | ||
|
||
import com.ontotext.javacourse.designpatterns.abstractfactory.factories.GeographyFactory; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.factories.HistoryFactory; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.factories.MathsFactory; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.factories.QuestionFactory; | ||
import java.security.InvalidParameterException; | ||
import java.util.Random; | ||
|
||
/** | ||
* This is a trivia game. Each game you start has rounds and every round the player gets a multiple | ||
* choice question and an open answer question on a randomly selected topic - History, Maths or | ||
* Geography. | ||
*/ | ||
public class Game { | ||
private static final Random random = new Random(); | ||
private static final String[] questionTopics = new String[] {"History", "Maths", "Geography"}; | ||
|
||
/** Selects a random topic and starts the round. */ | ||
public void startRound() { | ||
String topic = pickRandomTopic(); | ||
QuestionFactory factory; | ||
if (topic.equals("History")) { | ||
factory = new HistoryFactory(); | ||
} else if (topic.equals("Maths")) { | ||
factory = new MathsFactory(); | ||
} else if (topic.equals("Geography")) { | ||
factory = new GeographyFactory(); | ||
} else { | ||
throw new InvalidParameterException("Unknown topic!"); | ||
} | ||
Round round = new Round(factory); | ||
round.start(); | ||
} | ||
|
||
private String pickRandomTopic() { | ||
int randomNumber = random.nextInt(3); | ||
return questionTopics[randomNumber]; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...-patterns/src/main/java/com/ontotext/javacourse/designpatterns/abstractfactory/Round.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory; | ||
|
||
import com.ontotext.javacourse.designpatterns.abstractfactory.factories.QuestionFactory; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MultipleChoiceQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.OpenQuestion; | ||
import lombok.AllArgsConstructor; | ||
|
||
/** | ||
* The round class is a part of the game. Every round one multiple choice and one open answer | ||
* questions are asked. | ||
*/ | ||
@AllArgsConstructor | ||
public class Round { | ||
private QuestionFactory factory; | ||
|
||
/** Starts the round and asks two questions both on the same randomly chosen topic. */ | ||
public void start() { | ||
MultipleChoiceQuestion multipleChoiceQuestion = factory.createMultipleChoiceQuestion(); | ||
OpenQuestion openQuestion = factory.createOpenQuestion(); | ||
multipleChoiceQuestion.askQuestion(); | ||
openQuestion.writeAnswer("invalid answer"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...va/com/ontotext/javacourse/designpatterns/abstractfactory/factories/GeographyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.factories; | ||
|
||
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.GeographyMultipleChoiceQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MultipleChoiceQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.GeographyOpenQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.OpenQuestion; | ||
|
||
/** | ||
* The GeographyFactory class contains method that create a multiple choice geography question and | ||
* an open answer geography question. | ||
*/ | ||
public class GeographyFactory implements QuestionFactory { | ||
@Override | ||
public MultipleChoiceQuestion createMultipleChoiceQuestion() { | ||
return new GeographyMultipleChoiceQuestion(); | ||
} | ||
|
||
@Override | ||
public OpenQuestion createOpenQuestion() { | ||
return new GeographyOpenQuestion(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...java/com/ontotext/javacourse/designpatterns/abstractfactory/factories/HistoryFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.factories; | ||
|
||
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.HistoryMultipleChoiceQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MultipleChoiceQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.HistoryOpenQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.OpenQuestion; | ||
|
||
/** | ||
* The HistoryFactory class contains method that create a multiple choice history question and an | ||
* open answer history question. | ||
*/ | ||
public class HistoryFactory implements QuestionFactory { | ||
@Override | ||
public MultipleChoiceQuestion createMultipleChoiceQuestion() { | ||
return new HistoryMultipleChoiceQuestion(); | ||
} | ||
|
||
@Override | ||
public OpenQuestion createOpenQuestion() { | ||
return new HistoryOpenQuestion(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...n/java/com/ontotext/javacourse/designpatterns/abstractfactory/factories/MathsFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.factories; | ||
|
||
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MathsMultipleChoiceQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MultipleChoiceQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.MathsOpenQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.OpenQuestion; | ||
|
||
/** | ||
* The MathsFactory class contains method that create a multiple choice maths question and an open | ||
* answer maths question. | ||
*/ | ||
public class MathsFactory implements QuestionFactory { | ||
@Override | ||
public MultipleChoiceQuestion createMultipleChoiceQuestion() { | ||
return new MathsMultipleChoiceQuestion(); | ||
} | ||
|
||
@Override | ||
public OpenQuestion createOpenQuestion() { | ||
return new MathsOpenQuestion(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ava/com/ontotext/javacourse/designpatterns/abstractfactory/factories/QuestionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.factories; | ||
|
||
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MultipleChoiceQuestion; | ||
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.OpenQuestion; | ||
|
||
/** | ||
* Defines a question factory that can create a multiple choice question and an open question | ||
* depending on the factory type. | ||
*/ | ||
public interface QuestionFactory { | ||
MultipleChoiceQuestion createMultipleChoiceQuestion(); | ||
|
||
OpenQuestion createOpenQuestion(); | ||
} |
15 changes: 15 additions & 0 deletions
15
...signpatterns/abstractfactory/multiplechoicequestions/GeographyMultipleChoiceQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions; | ||
|
||
/** This class defines a multiple choice geography question. */ | ||
public class GeographyMultipleChoiceQuestion implements MultipleChoiceQuestion { | ||
|
||
@Override | ||
public void askQuestion() { | ||
System.out.println("Which is the capital city of Germany?"); | ||
} | ||
|
||
@Override | ||
public boolean checkAnswer(String answer) { | ||
return answer.equalsIgnoreCase("berlin"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...designpatterns/abstractfactory/multiplechoicequestions/HistoryMultipleChoiceQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions; | ||
|
||
/** | ||
* This class defines a multiple choice history question. | ||
*/ | ||
public class HistoryMultipleChoiceQuestion implements MultipleChoiceQuestion { | ||
|
||
@Override | ||
public void askQuestion() { | ||
System.out.println("What year did WW2 start?"); | ||
} | ||
|
||
@Override | ||
public boolean checkAnswer(String answer) { | ||
return answer.equalsIgnoreCase("1939"); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...e/designpatterns/abstractfactory/multiplechoicequestions/MathsMultipleChoiceQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions; | ||
|
||
/** This class defines a multiple choice math question. */ | ||
public class MathsMultipleChoiceQuestion implements MultipleChoiceQuestion { | ||
|
||
@Override | ||
public void askQuestion() { | ||
System.out.println("What is the value of PI?"); | ||
} | ||
|
||
@Override | ||
public boolean checkAnswer(String answer) { | ||
return answer.equalsIgnoreCase("3.14"); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...course/designpatterns/abstractfactory/multiplechoicequestions/MultipleChoiceQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions; | ||
|
||
/** Defines a multiple choice question that can check an answer is correct. */ | ||
public interface MultipleChoiceQuestion { | ||
void askQuestion(); | ||
|
||
boolean checkAnswer(String answer); | ||
} |
9 changes: 9 additions & 0 deletions
9
...totext/javacourse/designpatterns/abstractfactory/openquestions/GeographyOpenQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.openquestions; | ||
|
||
/** This class defines an open geography question. */ | ||
public class GeographyOpenQuestion implements OpenQuestion { | ||
@Override | ||
public void writeAnswer(String answer) { | ||
System.out.println(answer); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ontotext/javacourse/designpatterns/abstractfactory/openquestions/HistoryOpenQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.openquestions; | ||
|
||
/** | ||
* This class defines an open history question. | ||
*/ | ||
public class HistoryOpenQuestion implements OpenQuestion { | ||
@Override | ||
public void writeAnswer(String answer) { | ||
System.out.println(answer); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...m/ontotext/javacourse/designpatterns/abstractfactory/openquestions/MathsOpenQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.openquestions; | ||
|
||
/** This class defines an open maths question. */ | ||
public class MathsOpenQuestion implements OpenQuestion { | ||
@Override | ||
public void writeAnswer(String answer) { | ||
System.out.println(answer); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...va/com/ontotext/javacourse/designpatterns/abstractfactory/openquestions/OpenQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.ontotext.javacourse.designpatterns.abstractfactory.openquestions; | ||
|
||
/** Defines an open question that can accept an answer. */ | ||
public interface OpenQuestion { | ||
void writeAnswer(String answer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...rc/main/java/com/ontotext/javacourse/designpatterns/mail/builders/BuilderAttachments.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters