Skip to content

Commit

Permalink
feat : ScoreReport 리팩토링
Browse files Browse the repository at this point in the history
전략패턴 적용
  • Loading branch information
Kim-minseok123 committed May 30, 2023
1 parent 8007f63 commit e3a001c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 87 deletions.
57 changes: 57 additions & 0 deletions EmailReportSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.net.*;
import java.io.*;
public class EmailReportSender implements ScoreReportSender{
@Override
public void send(String recipient, String content) {
try {
Socket s = new Socket("osfmail.rit.edu", 25);
BufferedReader in =
new BufferedReader(
new InputStreamReader(s.getInputStream(), "8859_1"));
BufferedWriter out =
new BufferedWriter(
new OutputStreamWriter(s.getOutputStream(), "8859_1"));

String boundary = "DataSeparatorString";

// here you are supposed to send your username
sendln(in, out, "HELO world");
sendln(in, out, "MAIL FROM: <[email protected]>");
sendln(in, out, "RCPT TO: <" + recipient + ">");
sendln(in, out, "DATA");
sendln(out, "Subject: Bowling Score Report ");
sendln(out, "From: <Lucky Strikes Bowling Club>");

sendln(out, "Content-Type: text/plain; charset=\"us-ascii\"\r\n");
sendln(out, content + "\n\n");
sendln(out, "\r\n");

sendln(in, out, ".");
sendln(in, out, "QUIT");
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendln(BufferedReader in, BufferedWriter out, String s) {
try {
out.write(s + "\r\n");
out.flush();
// System.out.println(s);
s = in.readLine();
// System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
}

public void sendln(BufferedWriter out, String s) {
try {
out.write(s + "\r\n");
out.flush();
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
}
}
6 changes: 2 additions & 4 deletions EndGameReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

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


public class EndGameReport {

Expand Down Expand Up @@ -145,5 +144,4 @@ public void valueChanged(ListSelectionEvent e) {
((String) ((JList) e.getSource()).getSelectedValue());
}
}
}

}
8 changes: 5 additions & 3 deletions Lane.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,15 @@ public void run() {
int myIndex = 0;
while (scoreIt.hasNext()){
Bowler thisBowler = (Bowler)scoreIt.next();
ScoreReport sr = new ScoreReport( thisBowler, finalScores[myIndex++], gameNumber );
sr.sendEmail(thisBowler.getEmail());
ScoreReport sr = new ScoreReport( thisBowler, finalScores[myIndex++], gameNumber);
sr.setSender( new EmailReportSender());
sr.sendTo(thisBowler.getEmail());
Iterator printIt = printVector.iterator();
while (printIt.hasNext()){
if (thisBowler.getNick() == (String)printIt.next()){
System.out.println("Printing " + thisBowler.getNick());
sr.sendPrintout();
sr.setSender(new PrintReportSender());
sr.sendTo("Printer");
}
}

Expand Down
19 changes: 19 additions & 0 deletions PrintReportSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.awt.print.*;
public class PrintReportSender implements ScoreReportSender {
@Override
public void send(String recipient, String content) {
PrinterJob job = PrinterJob.getPrinterJob();

PrintableText printobj = new PrintableText(content);

job.setPrintable(printobj);

if (job.printDialog()) {
try {
job.print();
} catch (PrinterException e) {
System.out.println(e);
}
}
}
}
2 changes: 2 additions & 0 deletions SCOREHISTORY.DAT
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ Lana 18:4 4/2/2023 162
Tom 18:4 4/2/2023 109
Tom 18:9 4/2/2023 134
Jim 18:9 4/2/2023 101
Jim 22:34 4/2/2023 161
Mike 22:34 4/2/2023 120
87 changes: 7 additions & 80 deletions ScoreReport.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
/**
*
* SMTP implementation based on code by Réal Gagnon mailto:[email protected]
* SMTP implementation based on code by R�al Gagnon mailto:[email protected]
*
*/


import java.io.*;
import java.util.Vector;
import java.util.Iterator;
import java.net.*;
import java.awt.*;
import java.awt.print.*;

public class ScoreReport {

private ScoreReportSender sender;
private String content;

public ScoreReport( Bowler bowler, int[] scores, int games ) {
public ScoreReport( Bowler bowler, int[] scores, int games) {
String nick = bowler.getNick();
String full = bowler.getFullName();
Vector v = null;
Expand Down Expand Up @@ -49,77 +42,11 @@ public ScoreReport( Bowler bowler, int[] scores, int games ) {
content += "Thank you for your continuing patronage.";

}

public void sendEmail(String recipient) {
try {
Socket s = new Socket("osfmail.rit.edu", 25);
BufferedReader in =
new BufferedReader(
new InputStreamReader(s.getInputStream(), "8859_1"));
BufferedWriter out =
new BufferedWriter(
new OutputStreamWriter(s.getOutputStream(), "8859_1"));

String boundary = "DataSeparatorString";

// here you are supposed to send your username
sendln(in, out, "HELO world");
sendln(in, out, "MAIL FROM: <[email protected]>");
sendln(in, out, "RCPT TO: <" + recipient + ">");
sendln(in, out, "DATA");
sendln(out, "Subject: Bowling Score Report ");
sendln(out, "From: <Lucky Strikes Bowling Club>");

sendln(out, "Content-Type: text/plain; charset=\"us-ascii\"\r\n");
sendln(out, content + "\n\n");
sendln(out, "\r\n");

sendln(in, out, ".");
sendln(in, out, "QUIT");
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void sendPrintout() {
PrinterJob job = PrinterJob.getPrinterJob();

PrintableText printobj = new PrintableText(content);

job.setPrintable(printobj);

if (job.printDialog()) {
try {
job.print();
} catch (PrinterException e) {
System.out.println(e);
}
}

}

public void sendln(BufferedReader in, BufferedWriter out, String s) {
try {
out.write(s + "\r\n");
out.flush();
// System.out.println(s);
s = in.readLine();
// System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
public void setSender(ScoreReportSender sender) {
this.sender = sender;
}

public void sendln(BufferedWriter out, String s) {
try {
out.write(s + "\r\n");
out.flush();
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
public void sendTo(String recipient) {
sender.send(recipient, this.content);
}


}
3 changes: 3 additions & 0 deletions ScoreReportSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface ScoreReportSender {
void send(String recipient, String content);
}

0 comments on commit e3a001c

Please sign in to comment.