Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tester.kaspersky;
- import tester.kaspersky.gui.EducateScreen.EducateScreen;
- import tester.kaspersky.gui.EducateScreen.EducateScreenActionCommands;
- import tester.kaspersky.gui.EducateScreen.EducateScreenBuilder;
- import tester.kaspersky.gui.MenuScreen.MenuScreen;
- import tester.kaspersky.gui.MenuScreen.MenuScreenActionCommands;
- import tester.kaspersky.gui.MenuScreen.MenuScreenBuilder;
- import tester.kaspersky.gui.TestBeginScreen.TestBeginScreen;
- import tester.kaspersky.gui.TestBeginScreen.TestBeginScreenActionCommands;
- import tester.kaspersky.gui.TestBeginScreen.TestBeginScreenBuilder;
- import tester.kaspersky.gui.TestResultsScreen.TestResultsScreen;
- import tester.kaspersky.gui.TestResultsScreen.TestResultsScreenActionCommands;
- import tester.kaspersky.gui.TestResultsScreen.TestResultsScreenBuilder;
- import tester.kaspersky.gui.TestScreen.TestScreen;
- import tester.kaspersky.gui.TestScreen.TestScreenActionsCommands;
- import tester.kaspersky.gui.TestScreen.TestScreenBuilder;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionListener;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Stack;
- import java.util.stream.Stream;
- import static tester.kaspersky.ResultsCsvWriter.WriteResults;
- public class AppRunner extends JFrame {
- public AppRunner() {
- loadDataFromFiles();
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- createMenuBar();
- toMenuScreen();
- }
- private void toMenuScreen() {
- testStudentName = "";
- ActionListener actionListener = actionEvent -> {
- switch (actionEvent.getActionCommand()) {
- case MenuScreenActionCommands.START_EDUCATE:
- toEducateScreen(1);
- break;
- case MenuScreenActionCommands.START_TESTING:
- toTestBeginScreen();
- break;
- }
- };
- MenuScreen menuScreen = new MenuScreenBuilder()
- .addActionListener(actionListener)
- .buildScreen();
- setContentPane(menuScreen.getPanel());
- pack();
- centring();
- setVisible(true);
- }
- private void toEducateScreen(int pageNo) {
- ActionListener actionListener = actionEvent -> {
- switch (actionEvent.getActionCommand()) {
- case EducateScreenActionCommands.NEXT_PAGE:
- toEducateScreen(educateScreen.getPageNo() + 1);
- break;
- case EducateScreenActionCommands.PREV_PAGE:
- toEducateScreen(educateScreen.getPageNo() - 1);
- break;
- case EducateScreenActionCommands.TO_SET_PAGE:
- toEducateScreen(educateScreen.getPageNo());
- break;
- case EducateScreenActionCommands.TO_MAIN_MENU:
- toMenuScreen();
- break;
- }
- };
- educateScreen = new EducateScreenBuilder()
- .addActionListener(actionListener)
- .addButtonsStatus(pageNo != 1)
- .addPagesInfo(pageNo, educatePagesCount)
- .addPicture("img/" + pageNo + ".png")
- .buildScreen();
- setContentPane(educateScreen.getPanel());
- pack();
- centring();
- setVisible(true);
- }
- private void toTestBeginScreen() {
- ActionListener actionListener = actionEvent -> {
- testStudentName = "";
- if (TestBeginScreenActionCommands.BEGIN_TEST.equals(actionEvent.getActionCommand())) {
- if (testBeginScreen.getPersonName().trim().isEmpty()) {
- JOptionPane.showMessageDialog(this, "Имя не может быть пустым!",
- "Error", JOptionPane.WARNING_MESSAGE);
- } else {
- testStudentName = testBeginScreen.getPersonName().trim();
- toTestScreen(1);
- }
- }
- };
- testBeginScreen = new TestBeginScreenBuilder()
- .addActionListener(actionListener)
- .buildScreen();
- setContentPane(testBeginScreen.getPanel());
- pack();
- centring();
- setVisible(true);
- }
- private void toTestScreen(int testNo) {
- ActionListener actionListener = actionEvent -> {
- switch (actionEvent.getActionCommand()) {
- case TestScreenActionsCommands.NEXT_TEST:
- answersStack.push(testScreen.isAnswerCorrect());
- toTestScreen(testScreen.getTestNo() + 1);
- break;
- case TestScreenActionsCommands.PREV_TEST:
- answersStack.pop();
- toTestScreen(testScreen.getTestNo() - 1);
- break;
- case TestScreenActionsCommands.FINISH_TEST:
- answersStack.push(testScreen.isAnswerCorrect());
- toTestFinishScreen();
- break;
- }
- };
- String data = readFile("tests/" + testNo + ".txt").trim();
- testScreen = new TestScreenBuilder()
- .addActionListener(actionListener)
- .addTestData(data)
- .addButtonsStates(testNo != 1, testNo != testQuestionsCount)
- .addTestNo(testNo)
- .buildScreen();
- setContentPane(testScreen.getPanel());
- pack();
- centring();
- setVisible(true);
- }
- private void toTestFinishScreen() {
- int pointsCounter = 0;
- while (!answersStack.empty()) {
- if (answersStack.peek())
- ++pointsCounter;
- answersStack.pop();
- }
- try {
- WriteResults(testStudentName, pointsCounter, testQuestionsCount);
- } catch (IOException e) {
- JOptionPane.showMessageDialog(this, "Не удалось сохранить результат",
- "Error", JOptionPane.ERROR_MESSAGE);
- }
- ActionListener actionListener = actionEvent -> {
- switch (actionEvent.getActionCommand()) {
- case TestResultsScreenActionCommands.DO_RETEST:
- answersStack.empty();
- toTestBeginScreen();
- break;
- case TestResultsScreenActionCommands.TO_MAIN_MENU:
- answersStack.empty();
- testStudentName = "";
- toMenuScreen();
- break;
- }
- };
- testResultsScreen = new TestResultsScreenBuilder()
- .addActionListener(actionListener)
- .addPointsData(pointsCounter, testQuestionsCount)
- .buildScreen();
- setContentPane(testResultsScreen.getPanel());
- pack();
- centring();
- setVisible(true);
- }
- private void centring() {
- Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
- setLocation(dim.width / 2 - getWidth() / 2,
- dim.height / 2 - getHeight() / 2);
- }
- private void createMenuBar() {
- JMenu file = new JMenu("Файл");
- JMenuItem fileToMainScreen = new JMenuItem("Перейти в главное меню");
- fileToMainScreen.addActionListener(actionEvent -> toMenuScreen());
- file.add(fileToMainScreen);
- JMenuItem fileExit = new JMenuItem("Выход");
- fileExit.addActionListener(actionEvent -> terminate());
- file.add(fileExit);
- JMenu help = new JMenu("Справка");
- JMenuItem helpAbout = new JMenuItem("О программе");
- helpAbout.addActionListener(actionEvent -> aboutProgramDialog());
- help.add(helpAbout);
- JMenuBar menuBar = new JMenuBar();
- menuBar.add(file);
- menuBar.add(help);
- setJMenuBar(menuBar);
- }
- private void aboutProgramDialog() {
- String about = "<html>" + "Обучающий комплекс по настройке средства антивирусной защиты информации<br>" +
- "Kaspersky Endpoint Security 10 для Windows" + "<div align =\"left\"> " +
- "<br>Стасюк Иван,<br>2019" + "</div>" + "</html>";
- JOptionPane.showMessageDialog(this, about, "О программе", JOptionPane.INFORMATION_MESSAGE);
- }
- private void educateScreen() {
- System.out.println(educateScreen.getPageNo());
- }
- private void terminate() {
- dispose();
- System.exit(0);
- }
- private String readFile(String filePath) {
- StringBuilder contentBuilder = new StringBuilder();
- try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
- stream.forEach(s -> contentBuilder.append(s).append("\n"));
- }
- catch (IOException e)
- {
- e.printStackTrace();
- terminate();
- }
- return contentBuilder.toString();
- }
- private void loadDataFromFiles() {
- String[] strings = readFile("img/imgProps.txt").split("=");
- educatePagesCount = Integer.parseInt(strings[1].trim());
- strings = readFile("tests/testsProps.txt").split("=");
- testQuestionsCount = Integer.parseInt(strings[1].trim());
- }
- private EducateScreen educateScreen;
- private int educatePagesCount;
- private TestBeginScreen testBeginScreen;
- private TestScreen testScreen;
- private int testQuestionsCount;
- private Stack<Boolean> answersStack = new Stack<>();
- private TestResultsScreen testResultsScreen;
- private String testStudentName;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement