From 3e6c043e221250cc94f1b5e6bb634f88e6215099 Mon Sep 17 00:00:00 2001 From: Leon Montag Date: Fri, 30 Dec 2022 21:24:05 +0100 Subject: [PATCH] refactoring: Formating Code --- src/main/java/Gui/ActionHandler.java | 1 - src/main/java/Gui/StartGameGui.java | 4 +- src/main/java/Gui/gui.java | 98 +++++++++---------- src/main/java/game/runGame.java | 76 +++++++------- src/test/java/Gui/guiTest.java | 37 ++++--- src/test/java/game/runGameTest.java | 63 ++++++------ .../compile/default-compile/inputFiles.lst | 2 + 7 files changed, 141 insertions(+), 140 deletions(-) diff --git a/src/main/java/Gui/ActionHandler.java b/src/main/java/Gui/ActionHandler.java index f1adf45..c435468 100644 --- a/src/main/java/Gui/ActionHandler.java +++ b/src/main/java/Gui/ActionHandler.java @@ -3,7 +3,6 @@ package Gui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; - public class ActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { diff --git a/src/main/java/Gui/StartGameGui.java b/src/main/java/Gui/StartGameGui.java index d299107..666925d 100644 --- a/src/main/java/Gui/StartGameGui.java +++ b/src/main/java/Gui/StartGameGui.java @@ -5,7 +5,6 @@ import java.awt.event.ActionListener; import game.runGame; - public class StartGameGui implements ActionListener { @Override @@ -13,10 +12,11 @@ public class StartGameGui implements ActionListener { gui.Panel.setVisible(false); startGame(); } + public void startGame() { GameGuiInterface Gui = new GameGui(); Gui.createGui(); - + runGame rungame = new runGame(); rungame.run(GameGui.questionNr); } diff --git a/src/main/java/Gui/gui.java b/src/main/java/Gui/gui.java index 1eebce7..7f380f6 100644 --- a/src/main/java/Gui/gui.java +++ b/src/main/java/Gui/gui.java @@ -11,57 +11,57 @@ import javax.swing.JLabel; import javax.swing.JPanel; 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); + } } diff --git a/src/main/java/game/runGame.java b/src/main/java/game/runGame.java index 7caab72..747704a 100644 --- a/src/main/java/game/runGame.java +++ b/src/main/java/game/runGame.java @@ -8,58 +8,60 @@ import Gui.GameGui; public class runGame { 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); - 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; - 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; String[] Arr = new String[lines]; try { reader = new BufferedReader(new FileReader(Path)); String line = reader.readLine(); 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) { e.printStackTrace(); } return Arr; } + public static String[] SplitAnswers(String[] data2, int i) { - String[] parts = data2[i].split(","); - return parts; - } + String[] parts = data2[i].split(","); + return parts; + } } diff --git a/src/test/java/Gui/guiTest.java b/src/test/java/Gui/guiTest.java index 028947d..5ea01fc 100644 --- a/src/test/java/Gui/guiTest.java +++ b/src/test/java/Gui/guiTest.java @@ -9,46 +9,45 @@ class guiTest { @BeforeAll static void setUp() { - GuiInterface StartGui = new gui(); + GuiInterface StartGui = new gui(); StartGui.StartMainMenu(); } @Test void testCreateFrame() { - //arrange + // arrange int FrameWitdh = 800; int FrameHeight = 600; - //act + // act assertEquals(FrameWitdh, gui.Frame.getWidth()); - assertEquals(FrameHeight, gui.Frame.getHeight()); + assertEquals(FrameHeight, gui.Frame.getHeight()); } + @Test public void testCreateButtons() { - //arrange + // arrange String Start = "Starte Spiel"; String Close = "Schließen"; - - //act + + // act String StartTarget = gui.StartBtn.getText(); String CloseTarget = gui.CloseBtn.getText(); - //assert - assertEquals(Start,StartTarget); - assertEquals(Close,CloseTarget); + // assert + assertEquals(Start, StartTarget); + assertEquals(Close, CloseTarget); } - + @Test public void testCreatePanel() { - //arrange + // arrange 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); + } } diff --git a/src/test/java/game/runGameTest.java b/src/test/java/game/runGameTest.java index ce7349e..6588d69 100644 --- a/src/test/java/game/runGameTest.java +++ b/src/test/java/game/runGameTest.java @@ -17,57 +17,56 @@ class runGameTest { @Test public void testCountFileLinesWithThreeLines() { - //arrange + // arrange int numLines = 0; - File file; + File file; try { 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) { // TODO Auto-generated catch block e.printStackTrace(); } - //assert - assertEquals(4, numLines); + // assert + assertEquals(4, numLines); } - + @Test - public void testReadFile(){ - File file; + public void testReadFile() { + File file; try { 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) { // TODO Auto-generated catch block e.printStackTrace(); } - } + @Test 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]); } - } diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst index b7bea3d..84a0604 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ b/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\GameGuiInterface.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\main.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\GuiInterface.java