Skip to content

Commit

Permalink
feat : EndGamePrompt 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim-minseok123 committed May 30, 2023
1 parent 8bd3fb3 commit 82755cf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
9 changes: 9 additions & 0 deletions AgainGameCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class AgainGameCommand implements ButtonCommand {
EndGamePrompt endGamePrompt;
public AgainGameCommand(EndGamePrompt endGamePrompt){
this.endGamePrompt = endGamePrompt;
}
public void execute(){
endGamePrompt.setResult(1);
}
}
3 changes: 3 additions & 0 deletions ButtonCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface ButtonCommand {
public void execute();
}
9 changes: 9 additions & 0 deletions EndGameCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class EndGameCommand implements ButtonCommand {
EndGamePrompt endGamePrompt;
public EndGameCommand(EndGamePrompt endGamePrompt){
this.endGamePrompt = endGamePrompt;
}
public void execute(){
endGamePrompt.setResult(2);
}
}
44 changes: 23 additions & 21 deletions EndGamePrompt.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import java.util.*;
import java.text.*;

public class EndGamePrompt implements ActionListener {

public class EndGamePrompt {
private ButtonCommand command;
private JFrame win;
private JButton yesButton, noButton;

Expand Down Expand Up @@ -49,17 +44,19 @@ public EndGamePrompt( String partyName ) {
buttonPanel.setLayout(new GridLayout(1, 2));

Insets buttonMargin = new Insets(4, 4, 4, 4);

EndGamePromptClickEvent clickEvent = new EndGamePromptClickEvent(this);

yesButton = new JButton("Yes");
JPanel yesButtonPanel = new JPanel();
yesButtonPanel.setLayout(new FlowLayout());
yesButton.addActionListener(this);
yesButton.addActionListener(clickEvent);
yesButtonPanel.add(yesButton);

noButton = new JButton("No");
JPanel noButtonPanel = new JPanel();
noButtonPanel.setLayout(new FlowLayout());
noButton.addActionListener(this);
noButton.addActionListener(clickEvent);
noButtonPanel.add(noButton);

buttonPanel.add(yesButton);
Expand All @@ -82,16 +79,6 @@ public EndGamePrompt( String partyName ) {

}

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(yesButton)) {
result=1;
}
if (e.getSource().equals(noButton)) {
result=2;
}

}

public int getResult() {
while ( result == 0 ) {
try {
Expand All @@ -102,10 +89,25 @@ public int getResult() {
}
return result;
}

public void setResult(int i) {
result = i;
}
public void distroy() {
win.hide();
}

public JButton getYesButton() {
return yesButton;
}
public JButton getNoButton() {
return noButton;
}

public void setCommand(ButtonCommand command) {
this.command = command;
}
public void buttonPressed() {
command.execute();
}

}

18 changes: 18 additions & 0 deletions EndGamePromptClickEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EndGamePromptClickEvent implements ActionListener {
private EndGamePrompt endGamePrompt;
EndGamePromptClickEvent(EndGamePrompt endGamePrompt){
this.endGamePrompt = endGamePrompt;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(endGamePrompt.getYesButton())) {
endGamePrompt.setCommand(new AgainGameCommand(endGamePrompt));
}
if (e.getSource().equals(endGamePrompt.getNoButton())) {
endGamePrompt.setCommand(new EndGameCommand(endGamePrompt));
}
endGamePrompt.buttonPressed();
}
}

0 comments on commit 82755cf

Please sign in to comment.