Skip to content

Commit e78220c

Browse files
authored
modularizing the code for shorter methods
1 parent 25b111c commit e78220c

File tree

9 files changed

+412
-0
lines changed

9 files changed

+412
-0
lines changed

ButtonClicked.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package dashboard;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
import java.util.ArrayList;
6+
import javax.swing.JButton;
7+
import javax.swing.JFrame;
8+
import javax.swing.JPanel;
9+
import javax.swing.JTextArea;
10+
11+
12+
import java.awt.*;
13+
14+
public class ButtonClicked implements ActionListener{
15+
16+
private JButton like;
17+
private JButton dislike;
18+
private JButton displayQuotes;
19+
private static JPanel panel;
20+
private JFrame frame;
21+
private ArrayList<Quote> quotes;
22+
23+
@Override
24+
public void actionPerformed(ActionEvent e)
25+
{
26+
if (e.getSource() == displayQuotes);
27+
{
28+
29+
displayQuotes();
30+
}
31+
32+
if(e.getSource() == like) {
33+
//
34+
}
35+
if(e.getSource() == dislike) {
36+
//
37+
}
38+
}
39+
40+
public void displayQuotes() {
41+
JTextArea text = new JTextArea();
42+
for(Quote q : quotes)
43+
{
44+
text.append(q + System.getProperty("line.separator"));
45+
}
46+
panel = new JPanel();
47+
panel.add(text, BorderLayout.LINE_START);
48+
frame.setContentPane(panel);
49+
frame.pack();
50+
frame.setLocationByPlatform(true);
51+
panel.setVisible(true);
52+
}
53+
54+
public void setButton(JButton like) {
55+
this.like = like;
56+
}
57+
public JButton getButton() {
58+
return like;
59+
}
60+
public void setFrame(JFrame frame) {
61+
this.frame = frame;
62+
}
63+
public JFrame getFrame() {
64+
return frame;
65+
}
66+
public void setDisplayQuoteButton(JButton displayQuotes) {
67+
this.displayQuotes = displayQuotes;
68+
}
69+
public void setQuotes(ArrayList<Quote> quotes) {
70+
this.quotes = quotes;
71+
}
72+
public void setDislike(JButton dislike) {
73+
this.dislike = dislike;
74+
}
75+
public static JPanel getPanel() {
76+
return panel;
77+
}
78+
79+
}

DislikeClicked.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package dashboard;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
import java.util.ArrayList;
6+
7+
import javax.swing.JButton;
8+
import javax.swing.JFrame;
9+
10+
public class DislikeClicked implements ActionListener{
11+
private JButton dislike;
12+
private JFrame frame;
13+
private static ArrayList<Quote> quotesList;
14+
@Override
15+
public void actionPerformed(ActionEvent e) {
16+
if(e.getSource() == "dislike") {
17+
18+
}
19+
20+
}
21+
public void setDislike(JButton dislike) {
22+
this.dislike = dislike;
23+
}
24+
public void setFrame(JFrame frame) {
25+
this.frame = frame;
26+
}
27+
public static void setQuotes(ArrayList<Quote> quotes) {
28+
quotesList = quotes;
29+
}
30+
31+
}

LikeClicked.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dashboard;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
import javax.swing.JButton;
7+
import javax.swing.JFrame;
8+
9+
public class LikeClicked implements ActionListener{
10+
private JButton like;
11+
private JFrame frame;
12+
@Override
13+
public void actionPerformed(ActionEvent e) {
14+
// TODO Auto-generated method stub
15+
16+
}
17+
public void setLike(JButton like) {
18+
this.like = like;
19+
}
20+
public void setFrame(JFrame frame) {
21+
this.frame = frame;
22+
}
23+
24+
}

Main.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dashboard;
2+
3+
import java.util.ArrayList;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
//create an ArrayList of quotes
9+
ArrayList<Quote> quotes = new ArrayList<>();
10+
Person author1 = new Person("Jess","Glynne ");
11+
Person author2 = new Person("","Anonymous");
12+
Person author3 = new Person("Mihaly","Csikszentmihalyi");
13+
Person author4 = new Person("Susan Singh", "Rajput");
14+
Person author5 = new Person("Princess", "Diana");
15+
16+
Quote quote1 = new Quote("My style is unique and random. But I think it's important that it still makes sense. ",author1);
17+
Quote quote2 = new Quote("Your limitation - it's only your imagination. ",author2);
18+
Quote quote3 = new Quote("Goals transform a random walk into a chase. ", author3);
19+
Quote quote4 = new Quote("Everything happening around me is very random. I am enjoying the phase, as the journey is far more enjoyable than the destination. ", author4);
20+
Quote quote5 = new Quote("Carry out a random act of kindness, with no expectation of reward, safe in the knowledge that one day someone might do the same for you. ",author5);
21+
22+
quotes.add(quote1);
23+
quotes.add(quote2);
24+
quotes.add(quote3);
25+
quotes.add(quote4);
26+
quotes.add(quote5);
27+
28+
//create the window
29+
Window w = new Window(quotes);
30+
w.createWindow(quotes);
31+
32+
//
33+
}
34+
}

NextQuoteClicked.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package dashboard;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.Color;
5+
import java.awt.Dimension;
6+
import java.awt.Font;
7+
import java.awt.event.ActionEvent;
8+
import java.awt.event.ActionListener;
9+
import java.util.ArrayList;
10+
import javax.swing.JButton;
11+
import javax.swing.JFrame;
12+
import javax.swing.JLabel;
13+
import javax.swing.JPanel;
14+
import javax.swing.SwingConstants;
15+
16+
public class NextQuoteClicked implements ActionListener{
17+
private JButton nextQuote;
18+
private JLabel textLabel;
19+
private JFrame frame;
20+
private static ArrayList<Quote> quotesList;
21+
int quoteUpTo = 0;
22+
@Override
23+
public void actionPerformed(ActionEvent e) {
24+
if(e.getSource() == nextQuote) {
25+
setupQuotePanel();
26+
//this.setTextLabel(textLabel);
27+
likeAndDislikeButtons();
28+
likeAndDislikeButtonsClicked();
29+
setupFrame();
30+
nextQuotepanel.setVisible(true);
31+
}
32+
33+
}
34+
35+
public void setupQuotePanel() {
36+
//ButtonClicked.getPanel().setVisible(false);
37+
JPanel nextQuotepanel = new JPanel();
38+
nextQuotepanel.setBackground(Color.gray);
39+
textLabel = new JLabel("<html>"+quotesList.get(quoteUpTo).toString()+"</html",SwingConstants.CENTER);
40+
quoteUpTo++;
41+
if(quoteUpTo > quotesList.size() -1) {
42+
quoteUpTo = 0;
43+
}
44+
textLabel.setFont(new Font("Magneto Bold", Font.BOLD,20));
45+
textLabel.setPreferredSize(new Dimension(1000,100));
46+
textLabel.setForeground(Color.pink);
47+
nextQuotepanel.add(textLabel, BorderLayout.LINE_START);
48+
}
49+
50+
public void likeAndDislikeButtons() {
51+
JButton like=new JButton("Like");
52+
like.setBackground(Color.pink);
53+
54+
JButton dislike=new JButton("Dislike");
55+
dislike.setBackground(Color.pink);
56+
57+
like.setBounds(500,400,100,40); //dimensions where the button should go
58+
dislike.setBounds(800,400,100,40); // dimensions where the button should go.
59+
60+
nextQuotepanel.add(like);
61+
nextQuotepanel.add(dislike);
62+
}
63+
64+
public void likeAndDislikeButtonsClicked() {
65+
DislikeClicked dislikeClicked = new DislikeClicked();
66+
dislikeClicked.setDislike(dislike);
67+
dislikeClicked.setFrame(frame);
68+
69+
LikeClicked likeClicked = new LikeClicked();
70+
likeClicked.setLike(like);
71+
likeClicked.setFrame(frame);
72+
73+
like.addActionListener(likeClicked);
74+
dislike.addActionListener(dislikeClicked);
75+
}
76+
77+
public void setupFrame() {
78+
frame.getContentPane().add(nextQuotepanel, BorderLayout.CENTER);
79+
frame.setContentPane(nextQuotepanel);
80+
frame.pack();
81+
frame.setLocationByPlatform(true);
82+
}
83+
84+
public void setNextQuote(JButton nextQuote) {
85+
this.nextQuote = nextQuote;
86+
}
87+
public JButton getNextQuote() {
88+
return nextQuote;
89+
}
90+
public void setFrame(JFrame frame) {
91+
this.frame = frame;
92+
}
93+
public static void setQuotes(ArrayList<Quote> quotes) {
94+
quotesList = quotes;
95+
}
96+
public ArrayList<Quote> getQuotes(){
97+
return quotesList;
98+
}
99+
public void setTextLabel(JLabel textLabel) {
100+
this.textLabel = textLabel;
101+
}
102+
public JLabel getTextLabel() {
103+
return textLabel;
104+
}
105+
106+
107+
}

Person.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package dashboard;
2+
3+
public class Person {
4+
private String firstName;
5+
private String lastName;
6+
7+
public Person(String firstName, String lastName) {
8+
this.firstName = firstName;
9+
this.lastName = lastName;
10+
}
11+
public String getFirstName() {
12+
return firstName;
13+
}
14+
public String getLastName() {
15+
return lastName;
16+
}
17+
@Override
18+
public String toString() {
19+
StringBuilder sb = new StringBuilder();
20+
sb.append(firstName + " " + lastName);
21+
return sb.toString();
22+
}
23+
}

Quote.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dashboard;
2+
3+
public class Quote {
4+
private String quote;
5+
private Reaction reaction;
6+
private Person author;
7+
8+
public Quote(String quote, Person author) {
9+
this.quote = quote;
10+
this.author = author;
11+
12+
}
13+
14+
public void setReaction(Reaction reaction) {
15+
this.reaction = reaction;
16+
}
17+
public Reaction getReaction() {
18+
return reaction;
19+
}
20+
@Override
21+
public String toString() {
22+
StringBuilder sb = new StringBuilder();
23+
sb.append(quote + author);
24+
return sb.toString();
25+
}
26+
27+
}

Reaction.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dashboard;
2+
3+
public class Reaction {
4+
private String reaction;
5+
6+
public Reaction(String s) {
7+
if((s.toLowerCase()) == "like") {
8+
reaction = "like";
9+
}
10+
else if (s.toLowerCase() == "dislike") {
11+
reaction = "dislike";
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)