fix lab09 && move term project out

This commit is contained in:
SowinskiBraeden committed 2025-11-13 22:07:37 -08:00
1 parent 936ca07ad2
commit e264e757e4
28 files changed
+1100 -80

No files matched your search

@@ -50,7 +50,9 @@ public class WordList
*/
public String getRandomWord()
{
final Random rand = new Random();
final Random rand;
rand = new Random();
return this.wordList.get(rand.nextInt(this.wordList.size()));
}
@@ -1,25 +0,0 @@
package ca.bcit.comp2522.project;
import java.util.Arrays;
/**
* Country class
*
* @author Braeden Sowinski
* @version 1.0.0
*/
public class Country {
final String name;
final String capitalCityName;
final String[] facts;
Country(
final String name,
final String capitalCityName,
final String[] facts
) {
this.name = name;
this.capitalCityName = capitalCityName;
this.facts = facts;
}
}
@@ -1,54 +0,0 @@
package ca.bcit.comp2522.project;
import java.util.Scanner;
/**
* Main program entry to access a games menu
*
* @author Braeden Sowinski
* @version 1.0.0
*/
public class Main {
public static void main(final String[] args) {
final Scanner scanner;
scanner = new Scanner(System.in);
boolean run = true;
do {
final String input;
System.out.println("Select a game: ");
System.out.println("Word Game (W)");
System.out.println("Number Game (N)");
System.out.println("My Game (M)");
System.out.println("Quit (Q)");
System.out.print("> ");
input = scanner.nextLine().toUpperCase();
switch (input) {
case "W":
System.out.println("Word game...");
break;
case "N":
System.out.println("Number game...");
break;
case "M":
System.out.println("My game...");
break;
case "Q":
System.out.println("Quitting...");
run = false;
break;
default:
System.out.println("Invalid input: " + input);
break;
}
} while (run);
scanner.close();
}
}
@@ -0,0 +1,141 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ScoreTest {
private static final String SCORE_FILE = "test_score.txt";
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@BeforeEach
void setUp() throws IOException {
// Clear the score file before each test to ensure no leftover data
new FileWriter(SCORE_FILE, false).close();
}
@Test
void testScoreCalculation() {
// Testing score calculation based on correct first and second attempts
Score score = new Score(LocalDateTime.now(), 1, 6, 2, 1); // 6 first attempts (2 points each), 2 second attempts (1 point each)
assertEquals(14, score.getScore(), "Score should be 14 points (6 * 2 + 2 * 1)");
}
@Test
void testToStringFormat() {
// Testing the formatting of the toString() method
LocalDateTime dateTime = LocalDateTime.now();
Score score = new Score(dateTime, 1, 6, 2, 1);
String expected = String.format(
"Date and Time: %s\nGames Played: 1\nCorrect First Attempts: 6\nCorrect Second Attempts: 2\nIncorrect Attempts: 1\nScore: 14 points\n",
dateTime.format(formatter)
);
assertEquals(expected, score.toString(), "The toString format should match the expected format.");
}
@Test
void testAppendAndRetrieveLargeNumberOfScores() throws IOException {
// Create 25 scores and write them to the file
for (int i = 0; i < 25; i++) {
Score score = new Score(LocalDateTime.now(), 1, i + 1, (i % 3) + 1, (i % 2) + 1);
Score.appendScoreToFile(score, SCORE_FILE);
}
// Read scores from the file
List<Score> scores = Score.readScoresFromFile(SCORE_FILE);
// Validate the number of scores and their content
assertEquals(25, scores.size(), "Twenty-five scores should have been read from the file.");
for (int i = 0; i < 25; i++) {
int expectedScore = ((i + 1) * 2) + ((i % 3) + 1);
assertEquals(expectedScore, scores.get(i).getScore(), "Score for entry " + i + " should match the calculated value.");
}
}
@Test
void testCheckForNewHighScore() throws IOException {
// Create some initial scores and add them to the file
Score score1 = new Score(LocalDateTime.now(), 1, 6, 2, 1); // 14 points
Score score2 = new Score(LocalDateTime.now(), 1, 9, 1, 0); // 19 points
Score.appendScoreToFile(score1, SCORE_FILE);
Score.appendScoreToFile(score2, SCORE_FILE);
// Add a new score that is NOT a high score
Score score3 = new Score(LocalDateTime.now(), 1, 7, 2, 1); // 16 points
Score.appendScoreToFile(score3, SCORE_FILE);
// Read scores from the file
List<Score> scores = Score.readScoresFromFile(SCORE_FILE);
// Verify the highest score is still 19
int highScore = scores.stream().mapToInt(Score::getScore).max().orElse(0);
assertEquals(19, highScore, "The highest score should still be 19 points.");
}
@Test
void testNewHighScore() throws IOException {
// Create some initial scores and add them to the file
Score score1 = new Score(LocalDateTime.now(), 1, 6, 2, 1); // 14 points
Score score2 = new Score(LocalDateTime.now(), 1, 9, 1, 0); // 19 points
Score.appendScoreToFile(score1, SCORE_FILE);
Score.appendScoreToFile(score2, SCORE_FILE);
// Add a new score that IS a high score
Score score3 = new Score(LocalDateTime.now(), 1, 10, 1, 0); // 21 points
Score.appendScoreToFile(score3, SCORE_FILE);
// Read scores from the file
List<Score> scores = Score.readScoresFromFile(SCORE_FILE);
// Verify the highest score is now 21
int highScore = scores.stream().mapToInt(Score::getScore).max().orElse(0);
assertEquals(21, highScore, "The highest score should now be 21 points.");
}
@Test
void testAppendAndCheckMultipleReads() throws IOException {
// Create initial scores and add them to the file
for (int i = 0; i < 5; i++) {
Score score = new Score(LocalDateTime.now(), 1, 5 + i, 1, 1); // Variable scores
Score.appendScoreToFile(score, SCORE_FILE);
}
// Perform the first read
List<Score> scores1 = Score.readScoresFromFile(SCORE_FILE);
assertEquals(5, scores1.size(), "There should be 5 scores after the first write and read.");
// Add more scores to the file
for (int i = 5; i < 10; i++) {
Score score = new Score(LocalDateTime.now(), 1, 5 + i, 1, 1); // Variable scores
Score.appendScoreToFile(score, SCORE_FILE);
}
// Perform the second read
List<Score> scores2 = Score.readScoresFromFile(SCORE_FILE);
assertEquals(10, scores2.size(), "There should be 10 scores after the second write and read.");
}
@Test
void testEmptyScoreFile() throws IOException {
// Test reading from an empty score file, should return an empty list
List<Score> scores = Score.readScoresFromFile(SCORE_FILE);
assertTrue(scores.isEmpty(), "Reading from an empty file should return an empty list.");
}
@AfterEach
void tearDown() {
// Clean up by deleting the test score file after each test
new File(SCORE_FILE).delete();
}
}