Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store
27 changes: 27 additions & 0 deletions src/main/java/entity/Joke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package entity;

// there should be a joke factory, a user factory
public class Joke {

private final String content;
private String explanation;
private final int score;

public Joke(String content, int score) {
this.content = content;
this.explanation = "explanation";
this.score = score;
}

public String getContent() {
return content;
}

public String getExplanation() {
return explanation;
}

public void setExplanation(String explanation) {
this.explanation = explanation;
}
}
14 changes: 13 additions & 1 deletion src/main/java/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package entity;

import java.util.List;

/**
* The representation of a password-protected user for our program.
*/
public class User {

private final String name;
private final String password;
private List<Joke> favorites;

public User(String name, String password) {
public User(String name, String password, List<Joke> favorites) {
this.name = name;
this.password = password;
this.favorites = favorites;
}

public String getName() {
Expand All @@ -21,4 +25,12 @@ public String getPassword() {
return password;
}

public List<Joke> getFavorites() {
return favorites;
}

public void addToFavorites(Joke joke) {
this.favorites.add(joke);
}

}
104 changes: 104 additions & 0 deletions src/main/java/view/JokeView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package view;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import interface_adapter.joke.JokeController;
import interface_adapter.joke.JokeState;
import interface_adapter.joke.JokeViewModel;

/**
* The View for displaying jokes and interacting with the Joke Application.
*/
public class JokeAppView extends JPanel implements ActionListener, PropertyChangeListener {

private final String viewName = "joke view";
private final JokeViewModel jokeViewModel;

private final JLabel userIdLabel = new JLabel("User ID: ");
private final JLabel jokeDisplayArea = new JLabel("Your joke will appear here");
private final JButton generateJokeButton = new JButton("Generate Joke");
private final JButton searchJokeButton = new JButton("Search Joke");
private final JButton favoriteJokeButton = new JButton("Favorite Joke");
private JokeController jokeController;

public JokeAppView(JokeViewModel jokeViewModel) {
this.jokeViewModel = jokeViewModel;
this.jokeViewModel.addPropertyChangeListener(this);

setupButtons();

this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(title);
this.add(userIdLabel);
this.add(jokeDisplayArea);
this.add(generateJokeButton);
this.add(searchJokeButton);
this.add(favoriteJokeButton);
}

/**
* Configures the actions for each button.
*/
private void setupButtons() {

generateJokeButton.addActionListener(e -> jokeController.execute("Generate", ""));

searchJokeButton.addActionListener(e -> {
String query = JOptionPane.showInputDialog(this, "Search Joke:");
if (query != null && !query.trim().isEmpty()) {
jokeController.execute("search", query);
}
});

favoriteJokeButton.addActionListener(e -> jokeController.execute("Favourite", ""));
}

/**
* React to a button click that results in evt.
* @param evt the ActionEvent to react to
*/
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Click " + evt.getActionCommand());
}

/**
* Handles updates from the JokeViewModel, updating the UI display accordingly.
*/
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("state".equals(evt.getPropertyName()) && evt.getNewValue() instanceof JokeState) {
JokeState jokeState = (JokeState) evt.getNewValue();
updateFields(jokeState);
} else if ("error".equals(evt.getPropertyName())) {
JOptionPane.showMessageDialog(this, evt.getNewValue().toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
}

/**
* Updates the UI fields based on the current state of JokeState.
* @param state the current JokeState
*/
private void updateFields(JokeState state) {
jokeDisplayArea.setText(state.getJokeText());
favoriteJokeButton.setText(state.isFavorite() ? "Unfavourite Joke" : "Favourite Joke");
userIdLabel.setText("User ID: " + state.getErrorMessage());
}

public String getViewName() {
return viewName;
}

public void setJokeController(JokeController jokeController) {
this.jokeController = jokeController;
}
}
116 changes: 62 additions & 54 deletions src/main/java/view/NoteView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,99 @@
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import interface_adapter.note.NoteController;
import interface_adapter.note.NoteState;
import interface_adapter.note.NoteViewModel;
import interface_adapter.joke.JokeController;
import interface_adapter.joke.JokeState;
import interface_adapter.joke.JokeViewModel;

/**
* The View for when the user is viewing a note in the program.
* The View for displaying jokes and interacting with the Joke Application.
*/
public class NoteView extends JPanel implements ActionListener, PropertyChangeListener {

private final NoteViewModel noteViewModel;

private final JLabel noteName = new JLabel("note for jonathan_calver2");
private final JTextArea noteInputField = new JTextArea();

private final JButton saveButton = new JButton("Save");
private final JButton refreshButton = new JButton("Refresh");
private NoteController noteController;

public NoteView(NoteViewModel noteViewModel) {
public class JokeAppView extends JPanel implements ActionListener, PropertyChangeListener {

noteName.setAlignmentX(Component.CENTER_ALIGNMENT);
this.noteViewModel = noteViewModel;
this.noteViewModel.addPropertyChangeListener(this);
private final String viewName = "joke view";
private final JokeViewModel jokeViewModel;

final JPanel buttons = new JPanel();
buttons.add(saveButton);
buttons.add(refreshButton);
private final JLabel userIdLabel = new JLabel("User ID: ");
private final JLabel jokeDisplayArea = new JLabel("Your joke will appear here");
private final JButton generateJokeButton = new JButton("Generate Joke");
private final JButton searchJokeButton = new JButton("Search Joke");
private final JButton favoriteJokeButton = new JButton("Favorite Joke");
private JokeController jokeController;

saveButton.addActionListener(
evt -> {
if (evt.getSource().equals(saveButton)) {
noteController.execute(noteInputField.getText());
public JokeAppView(JokeViewModel jokeViewModel) {
this.jokeViewModel = jokeViewModel;
this.jokeViewModel.addPropertyChangeListener(this);

}
}
);

refreshButton.addActionListener(
evt -> {
if (evt.getSource().equals(refreshButton)) {
noteController.execute(null);

}
}
);
setupButtons();

this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

this.add(noteName);
this.add(noteInputField);
this.add(buttons);
this.add(title);
this.add(userIdLabel);
this.add(jokeDisplayArea);
this.add(generateJokeButton);
this.add(searchJokeButton);
this.add(favoriteJokeButton);
}
/**
* Configures the actions for each button.
*/
private void setupButtons() {
// Generate Joke Action
generateJokeButton.addActionListener(e -> jokeController.execute("Generate a joke", ""));

// Search Joke Action
searchJokeButton.addActionListener(e -> {
String query = JOptionPane.showInputDialog(this, "Search for a joke:");
if (query != null && !query.trim().isEmpty()) {
jokeController.execute("search", query);
}
})
favoriteJokeButton.addActionListener(e -> jokeController.execute("Favourite", ""));
}

/**
* React to a button click that results in evt.
* @param evt the ActionEvent to react to
*/
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Click " + evt.getActionCommand());
}

/**
* Handles updates from the JokeViewModel, updating the UI display accordingly.
*/
@Override
public void propertyChange(PropertyChangeEvent evt) {
final NoteState state = (NoteState) evt.getNewValue();
setFields(state);
if (state.getError() != null) {
JOptionPane.showMessageDialog(this, state.getError(),
"Error", JOptionPane.ERROR_MESSAGE);
if ("state".equals(evt.getPropertyName()) && evt.getNewValue() instanceof JokeState) {
JokeState jokeState = (JokeState) evt.getNewValue();
updateFields(jokeState);
} else if ("error".equals(evt.getPropertyName())) {
JOptionPane.showMessageDialog(this, evt.getNewValue().toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
}

private void setFields(NoteState state) {
noteInputField.setText(state.getNote());
/**
* Updates the UI fields based on the current state of JokeState
* @param state the current JokeState
*/
private void updateFields(JokeState state) {
jokeDisplayArea.setText(state.getJokeText());
favoriteJokeButton.setText(state.isFavorite() ? "Unfavourite Joke" : "Favorite Joke");
userIdLabel.setText("User ID: " + state.getErrorMessage());
}

public void setNoteController(NoteController controller) {
this.noteController = controller;
public String getViewName() {
return viewName;
}
}

public void setJokeController(JokeController jokeController) {
this.jokeController = jokeController;
}
}