Browse Source

refactoring: Formating Code

master
Leon Montag 2 years ago
parent
commit
3e6c043e22
  1. 1
      src/main/java/Gui/ActionHandler.java
  2. 4
      src/main/java/Gui/StartGameGui.java
  3. 98
      src/main/java/Gui/gui.java
  4. 76
      src/main/java/game/runGame.java
  5. 37
      src/test/java/Gui/guiTest.java
  6. 63
      src/test/java/game/runGameTest.java
  7. 2
      target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst

1
src/main/java/Gui/ActionHandler.java

@ -3,7 +3,6 @@ package Gui;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
public class ActionHandler implements ActionListener { public class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {

4
src/main/java/Gui/StartGameGui.java

@ -5,7 +5,6 @@ import java.awt.event.ActionListener;
import game.runGame; import game.runGame;
public class StartGameGui implements ActionListener { public class StartGameGui implements ActionListener {
@Override @Override
@ -13,10 +12,11 @@ public class StartGameGui implements ActionListener {
gui.Panel.setVisible(false); gui.Panel.setVisible(false);
startGame(); startGame();
} }
public void startGame() { public void startGame() {
GameGuiInterface Gui = new GameGui(); GameGuiInterface Gui = new GameGui();
Gui.createGui(); Gui.createGui();
runGame rungame = new runGame(); runGame rungame = new runGame();
rungame.run(GameGui.questionNr); rungame.run(GameGui.questionNr);
} }

98
src/main/java/Gui/gui.java

@ -11,57 +11,57 @@ import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
public class gui implements GuiInterface { public class gui implements GuiInterface {
public static JFrame Frame;
public static JButton StartBtn, CloseBtn;
public static JPanel Panel;
public static JLabel Label;
public static int width = 800, height = 600;
public static JFrame Frame;
public static JButton StartBtn, CloseBtn;
public static JPanel Panel;
public static JLabel Label;
public static int width = 800, height = 600;
@Override
public void StartMainMenu() {
createFrame();
createButtons();
createPanel();
}
@Override
public void createFrame(){
Frame = new JFrame("MiniGames");
Frame.setSize(width,height);
Frame.setLocationRelativeTo(null);
Frame.setResizable(false);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void createButtons() {
StartBtn = new JButton("Starte Spiel");
StartBtn.addActionListener(new StartGameGui());
CloseBtn = new JButton("Schließen");
CloseBtn.addActionListener(new ActionHandler());
@Override
public void StartMainMenu() {
createFrame();
createButtons();
createPanel();
}
@Override
public void createFrame() {
Frame = new JFrame("MiniGames");
Frame.setSize(width, height);
Frame.setLocationRelativeTo(null);
Frame.setResizable(false);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
@Override
public void createPanel() {
Panel = new JPanel();
Panel.setSize(width,height);
Panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 300;
gbc.ipady = 100;
gbc.insets = new Insets(20, 0, 0, 0);
Label = new JLabel("MiniGames");
Label.setHorizontalAlignment(JLabel.CENTER);
Label.setFont(new Font("Serif", Font.BOLD, 28));
Panel.add(Label,gbc);
Panel.add(StartBtn,gbc);
Panel.add(CloseBtn,gbc);
Frame.add(Panel);
Frame.setVisible(true);
Panel.setVisible(true);
}
@Override
public void createButtons() {
StartBtn = new JButton("Starte Spiel");
StartBtn.addActionListener(new StartGameGui());
CloseBtn = new JButton("Schließen");
CloseBtn.addActionListener(new ActionHandler());
}
@Override
public void createPanel() {
Panel = new JPanel();
Panel.setSize(width, height);
Panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 300;
gbc.ipady = 100;
gbc.insets = new Insets(20, 0, 0, 0);
Label = new JLabel("MiniGames");
Label.setHorizontalAlignment(JLabel.CENTER);
Label.setFont(new Font("Serif", Font.BOLD, 28));
Panel.add(Label, gbc);
Panel.add(StartBtn, gbc);
Panel.add(CloseBtn, gbc);
Frame.add(Panel);
Frame.setVisible(true);
Panel.setVisible(true);
}
} }

76
src/main/java/game/runGame.java

@ -8,58 +8,60 @@ import Gui.GameGui;
public class runGame { public class runGame {
public static String QuestionsPath = "QandA/Questions.txt"; public static String QuestionsPath = "QandA/Questions.txt";
public static String AnswersPath = "QandA/Answers.txt";
public static int lines = CountFileLines(QuestionsPath);
public static String[] arrQuestions = readFile(QuestionsPath);
public static String[] arrAnswers = readFile(AnswersPath);
public static String[] splitAns;
public static String AnswersPath = "QandA/Answers.txt";
public static int lines = CountFileLines(QuestionsPath);
public static String[] arrQuestions = readFile(QuestionsPath);
public static String[] arrAnswers = readFile(AnswersPath);
public static String[] splitAns;
public void run(int i) {
public void run(int i) {
splitAns = SplitAnswers(arrAnswers, i); splitAns = SplitAnswers(arrAnswers, i);
GameGui.QuestionLabel.setText(arrQuestions[i]);
GameGui.AnswersBtn1.setText(splitAns[0]);
GameGui.AnswersBtn2.setText(splitAns[1]);
GameGui.AnswersBtn3.setText(splitAns[2]);
GameGui.AnswersBtn4.setText(splitAns[3]);
GameGui.questionNr++;
}
public static int CountFileLines(String Path) {
GameGui.QuestionLabel.setText(arrQuestions[i]);
GameGui.AnswersBtn1.setText(splitAns[0]);
GameGui.AnswersBtn2.setText(splitAns[1]);
GameGui.AnswersBtn3.setText(splitAns[2]);
GameGui.AnswersBtn4.setText(splitAns[3]);
GameGui.questionNr++;
}
public static int CountFileLines(String Path) {
int Numlines = 0; int Numlines = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(Path));
while (reader.readLine() != null) {
Numlines++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return Numlines+1;
try {
BufferedReader reader = new BufferedReader(new FileReader(Path));
while (reader.readLine() != null) {
Numlines++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String[] readFile(String Path) {
return Numlines + 1;
}
public static String[] readFile(String Path) {
BufferedReader reader; BufferedReader reader;
String[] Arr = new String[lines]; String[] Arr = new String[lines];
try { try {
reader = new BufferedReader(new FileReader(Path)); reader = new BufferedReader(new FileReader(Path));
String line = reader.readLine(); String line = reader.readLine();
int i = 0; int i = 0;
while (line != null) {
Arr[i] = line;
line = reader.readLine();
i++;
}
reader.close();
while (line != null) {
Arr[i] = line;
line = reader.readLine();
i++;
}
reader.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return Arr; return Arr;
} }
public static String[] SplitAnswers(String[] data2, int i) { public static String[] SplitAnswers(String[] data2, int i) {
String[] parts = data2[i].split(",");
return parts;
}
String[] parts = data2[i].split(",");
return parts;
}
} }

37
src/test/java/Gui/guiTest.java

@ -9,46 +9,45 @@ class guiTest {
@BeforeAll @BeforeAll
static void setUp() { static void setUp() {
GuiInterface StartGui = new gui();
GuiInterface StartGui = new gui();
StartGui.StartMainMenu(); StartGui.StartMainMenu();
} }
@Test @Test
void testCreateFrame() { void testCreateFrame() {
//arrange
// arrange
int FrameWitdh = 800; int FrameWitdh = 800;
int FrameHeight = 600; int FrameHeight = 600;
//act
// act
assertEquals(FrameWitdh, gui.Frame.getWidth()); assertEquals(FrameWitdh, gui.Frame.getWidth());
assertEquals(FrameHeight, gui.Frame.getHeight());
assertEquals(FrameHeight, gui.Frame.getHeight());
} }
@Test @Test
public void testCreateButtons() { public void testCreateButtons() {
//arrange
// arrange
String Start = "Starte Spiel"; String Start = "Starte Spiel";
String Close = "Schließen"; String Close = "Schließen";
//act
// act
String StartTarget = gui.StartBtn.getText(); String StartTarget = gui.StartBtn.getText();
String CloseTarget = gui.CloseBtn.getText(); String CloseTarget = gui.CloseBtn.getText();
//assert
assertEquals(Start,StartTarget);
assertEquals(Close,CloseTarget);
// assert
assertEquals(Start, StartTarget);
assertEquals(Close, CloseTarget);
} }
@Test @Test
public void testCreatePanel() { public void testCreatePanel() {
//arrange
// arrange
int NumPanels = 3; int NumPanels = 3;
//act
int TargetNumPanels = gui.Panel.getComponents().length;
//assert
assertEquals(NumPanels,TargetNumPanels);
}
// act
int TargetNumPanels = gui.Panel.getComponents().length;
// assert
assertEquals(NumPanels, TargetNumPanels);
}
} }

63
src/test/java/game/runGameTest.java

@ -17,57 +17,56 @@ class runGameTest {
@Test @Test
public void testCountFileLinesWithThreeLines() { public void testCountFileLinesWithThreeLines() {
//arrange
// arrange
int numLines = 0; int numLines = 0;
File file;
File file;
try { try {
file = File.createTempFile("temp", ".txt"); file = File.createTempFile("temp", ".txt");
FileWriter writer = new FileWriter(file);
writer.write("Zeile 1\n");
writer.write("Zeile 2\n");
writer.write("Zeile 3\n");
writer.close();
//act
numLines = runGame.CountFileLines(file.getAbsolutePath());
FileWriter writer = new FileWriter(file);
writer.write("Zeile 1\n");
writer.write("Zeile 2\n");
writer.write("Zeile 3\n");
writer.close();
// act
numLines = runGame.CountFileLines(file.getAbsolutePath());
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
//assert
assertEquals(4, numLines);
// assert
assertEquals(4, numLines);
} }
@Test @Test
public void testReadFile(){
File file;
public void testReadFile() {
File file;
try { try {
file = File.createTempFile("temp", ".txt"); file = File.createTempFile("temp", ".txt");
FileWriter writer = new FileWriter(file);
writer.write("Zeile 1\n");
writer.write("Zeile 2\n");
writer.write("Zeile 3\n");
writer.close();
String[] lines = runGame.readFile(file.getAbsolutePath());
assertEquals("Zeile 1", lines[0]);
assertEquals("Zeile 2", lines[1]);
assertEquals("Zeile 3", lines[2]);
FileWriter writer = new FileWriter(file);
writer.write("Zeile 1\n");
writer.write("Zeile 2\n");
writer.write("Zeile 3\n");
writer.close();
String[] lines = runGame.readFile(file.getAbsolutePath());
assertEquals("Zeile 1", lines[0]);
assertEquals("Zeile 2", lines[1]);
assertEquals("Zeile 3", lines[2]);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
@Test @Test
public void testSplitAnswers() { public void testSplitAnswers() {
String[] StringToSplit = {"Das,ist,ein,Test", "1,2,3,4"};
String[] parts = runGame.SplitAnswers(StringToSplit, 1);
assertEquals(4, parts.length);
assertEquals("1", parts[0]);
assertEquals("2", parts[1]);
assertEquals("3", parts[2]);
assertEquals("4", parts[3]);
String[] StringToSplit = { "Das,ist,ein,Test", "1,2,3,4" };
String[] parts = runGame.SplitAnswers(StringToSplit, 1);
assertEquals(4, parts.length);
assertEquals("1", parts[0]);
assertEquals("2", parts[1]);
assertEquals("3", parts[2]);
assertEquals("4", parts[3]);
} }
} }

2
target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst

@ -1,6 +1,8 @@
C:\Users\Leon\MiniGameProject\src\main\java\Gui\ActionHandler.java C:\Users\Leon\MiniGameProject\src\main\java\Gui\ActionHandler.java
C:\Users\Leon\MiniGameProject\src\main\java\Gui\GameGuiInterface.java
C:\Users\Leon\MiniGameProject\src\main\java\Gui\gui.java C:\Users\Leon\MiniGameProject\src\main\java\Gui\gui.java
C:\Users\Leon\MiniGameProject\src\main\java\game\runGame.java C:\Users\Leon\MiniGameProject\src\main\java\game\runGame.java
C:\Users\Leon\MiniGameProject\src\main\java\main.java C:\Users\Leon\MiniGameProject\src\main\java\main.java
C:\Users\Leon\MiniGameProject\src\main\java\Gui\StartGameGui.java C:\Users\Leon\MiniGameProject\src\main\java\Gui\StartGameGui.java
C:\Users\Leon\MiniGameProject\src\main\java\Gui\GameGui.java C:\Users\Leon\MiniGameProject\src\main\java\Gui\GameGui.java
C:\Users\Leon\MiniGameProject\src\main\java\Gui\GuiInterface.java
Loading…
Cancel
Save