Alright so I have written this dice wagering game where we use classes that store a bank account balacne class for die rolling and a class that send an email of the results/when they are out of money. My issue seems to be when I try to call the SMTP method into the Driver program I am given an error no matter what I do.
Driver Class
Bank Account Classimport java.util.Scanner; public class Playingdice { public static void main(String[] args) { System.out.println("Playing A Game Of Dice"); Scanner keyb = new Scanner(System.in); //Players //Name System.out.println("Please enter a name Player 1: "); String name = keyb.nextLine(); Bankaccount b1 = new Bankaccount(name); System.out.println("Please enter a name Player 2: "); name = keyb.nextLine(); Bankaccount b2 = new Bankaccount(name); //Balance int balance = b1.getBalance(); System.out.println("Your Balance " + b1.getName() + " is " + balance); balance = b2.getBalance(); System.out.println("Your Balance " + b2.getName() + " is " + balance); do { //Wager System.out.println("Please enter your wager " + b1.getName() + ": "); int wager1 = Integer.parseInt(keyb.next()); if (wager1 == 0){ System.out.println("Why are you quitting?"); System.out.println("Your Balance " + b1.getName() + " is " + b1.getBalance()); System.out.println("Your Balance " + b2.getName() + " is " + b2.getBalance()); System.exit(0); } else if ( wager1 > b1.getBalance()){ System.out.println("You are a cheater"); System.out.println("Your Balance " + b1.getName() + " is " + b1.getBalance()); System.out.println("Your Balance " + b2.getName() + " is " + b2.getBalance()); System.exit(0); } System.out.println("Please enter your wager " + b2.getName() + ": "); int wager2 = Integer.parseInt(keyb.next()); if (wager2 == 0){ System.out.println("Why are you quitting?"); System.out.println("Your Balance " + b1.getName() + " is " + b1.getBalance()); System.out.println("Your Balance " + b2.getName() + " is " + b2.getBalance()); System.exit(0); } else if(wager2 > b2.getBalance()){ System.out.println("You are a cheater"); System.out.println("Your Final Balance " + b1.getName() + " is " + b1.getBalance()); System.out.println("Your Final Balance " + b2.getName() + " is " + b2.getBalance()); System.exit(0); } //Game of Chance Die d1 = new Die(); Die d2 = new Die(); Die d3 = new Die(); Die d4 = new Die(); int r1 = d1.roll(); int r2 = d2.roll(); int r3 = d3.roll(); int r4 = d3.roll(); int total1 = r1 + r2; int total2 = r3 + r4; int p1 = total1; int p2 = total2; if (p1==7){ System.out.println("You win " + b1.getName() ); System.out.println("You win " + wager2); b2.withdraw(wager2); b1.deposit( wager2 ); } else if(p2==7){ System.out.println("You win you rolled a 7 " + b2.getName()); System.out.println("You win " + wager1 ); b1.withdraw(wager1); b2.deposit( wager1 ); } else if (p1== 2) { System.out.println("You win you rolled a 2 "+ b1.getName()); System.out.println("You win "+ wager2 ); b2.withdraw(wager2); b1.deposit( wager2 ); } else if(p2== 2){ System.out.println("You win you rolled a 2 " + b2.getName()); b1.withdraw(wager1); b2.deposit( wager1 ); } else if (p1!=7||p1!=2||p1!=7||p2!=2){ System.out.println(b1.getName() + " you rolled a " + r1 + " and a " + r2+" which equals "+ total1); System.out.println(b2.getName() + " you rolled a " + r3 + " and a " + r4+" which equals "+ total2); System.out.println("We Will Roll Again because neither of those were equal to 7 or 2 please re-enter you wager or a new one if you wish to change it."); } //Giving the Balance System.out.println("Your Balance " + b1.getName() + " is " + b1.getBalance()); System.out.println("Your Balance " + b2.getName() + " is " + b2.getBalance()); }while(true); //Sending Email of Results SMTPsender s = new SMTPsender(mailSender, mailSender, "WARNING! Your account is overdrawn."); s.send(); } }
showDie Class// Java packages import javax.swing.*; class Die { /** * A die is defined in terms of its value, which must be between * 1 and 6 */ private int value; /** * Default constructor for a die. The die is initialized to a * random integer value between 1 and 6 */ public Die() { value = (int)(Math.random() * 6.0 + 1.0); } /** Returns the value of a die, without affecting it */ public int getValue() { return value; } /** Displays a die, i.e., its value */ public ShowDie display(int x, int y) { System.out.println("The value of the die is " + value); ShowDie sd = new ShowDie(value, x, y); sd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); return sd; } /** Removes the display of a die */ public void undisplay(ShowDie sd) { sd.dispose(); } /** Rolls the die, sets its value, and returns the new value */ public int roll() { value = (int)(Math.random() * 6.0 + 1.0); return value; } }// Die
SMTP Class// Java packages import java.awt.*; import javax.swing.*; /** * ShowDie.java * * Draw a graphic image of a die. * * Created: Thursday September 15 8:28:49 2011 * * @author Prof. Michael N. Huhns<a href="mailto:huhns@sc.edu"></a> * @version 1.0 */ public class ShowDie extends JFrame { private int dots; public ShowDie(int d, int x, int y) { super("Die"); dots = d; setSize(100, 100); setLocation(x, y); setVisible(true); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.WHITE); g.fillRect(25, 25, 50, 50); g.setColor(Color.BLACK); if (dots == 1) g.fillOval(46, 46, 8, 8); else if (dots == 2) { g.fillOval(32, 32, 8, 8); g.fillOval(60, 60, 8, 8); } else if (dots == 3) { g.fillOval(46, 46, 8, 8); g.fillOval(32, 32, 8, 8); g.fillOval(60, 60, 8, 8); } else if (dots == 4) { g.fillOval(32, 32, 8, 8); g.fillOval(60, 60, 8, 8); g.fillOval(32, 60, 8, 8); g.fillOval(60, 32, 8, 8); } else if (dots == 5) { g.fillOval(46, 46, 8, 8); g.fillOval(32, 32, 8, 8); g.fillOval(60, 60, 8, 8); g.fillOval(32, 60, 8, 8); g.fillOval(60, 32, 8, 8); } else if (dots == 6) { g.fillOval(32, 32, 8, 8); g.fillOval(32, 46, 8, 8); g.fillOval(32, 60, 8, 8); g.fillOval(60, 32, 8, 8); g.fillOval(60, 46, 8, 8); g.fillOval(60, 60, 8, 8); } else System.out.println("Error in number of dots being displayed"); } }// ShowDie
// SMTPsender.java import java.net.*; import java.util.*; import java.io.*; /********************************************************************* * * Module: SMTPsender.java * * Description: this class uses the SMTP (Simple Mail Transfer Protocol) * to connect to an email server and send a message. * The entire mail sending process is saved as a LOG file. * * @author Professor Michael N. Huhns * @version September 15, 2011 ********************************************************************/ public class SMTPsender { // class constants private static final int SMTP_PORT = 25; private static final String SENDLOGPATH = ""; // current directory // ** EDIT NEXT LINE TO BE YOUR OWN VALID EMAIL ADDRESS private static String mailSender = "null@null"; private static String mailServer = "129.252.158.124"; private boolean readyForSend = false; private String message; private String recipient; private String logFileName; private String sentTime; // IO variables private BufferedReader in; private BufferedWriter out; private FileWriter fw; /** * Constructor for sending a message to a recipient * @param recip (a String) the email address of the recipient * @param msg (a String) the message to be sent */ public SMTPsender(String sndr, String recip, String msg) { mailSender = sndr; int indexOfAt = recip.indexOf('@'); if (indexOfAt < 1) System.out.println("Malformed recipient address.\n"); else { this.recipient = recip; this.sentTime = new Date().toString(); this.message = "Date: " + sentTime + "\nFrom: CSCE145_Bank" + "\nReply-To: " + mailSender + "\nSubject: Bank Accounts for Dice Players\n\n" + msg; readyForSend = true; } } /** * Set the name of the log file to be milliseconds.log from * January 1, 1970, 00:00:00 GMT */ private void setLogFileName() { String seconds = Long.toString(new Date().getTime()- new Date(0).getTime()); this.logFileName = SENDLOGPATH + seconds + ".log"; } /** Get the name of the log file */ public String getLogFileName(){ return logFileName; } /** * This method implements the SMTP protocol to send an email * message by engaging in a dialog with the mail server. It also * records the dialog in a log file. */ public void send() { if (!readyForSend) System.out.println("Attempt to send incomplete message."); else { try { //set log file name... setLogFileName(); fw = new FileWriter(getLogFileName()); //create a connection with a mail server... InetAddress ia = InetAddress.getByName(mailServer); Socket s = new Socket(ia, SMTP_PORT); in = new BufferedReader(new InputStreamReader(s.getInputStream())); out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); //engage in standard SMTP dialog... String response; response = reply(); sendOut("HELO cse.sc.edu" + (char)(13)+(char)(10)); response = reply(); sendOut("MAIL FROM: " + mailSender + (char)(13)+(char)(10)); response = reply(); sendOut("RCPT TO: " + recipient + (char)(13)+(char)(10)); response = reply(); sendOut("Data" + (char)(13)+(char)(10)); response = reply(); sendOut(message + (char)(13)+(char)(10) + '.'+(char)(13)+(char)(10)); response = reply(); sendOut("QUIT" + (char)(13)+(char)(10)); s.close(); } catch(IOException ioe) {System.out.println(ioe.getMessage());} } } /** * This method is used to send SMTP responses to the SMTP mail * server and then write them into the log file. * @param content (a String) that is written into the log file. */ private void sendOut(String content) throws IOException { out.write(content); fw.write(content); fw.write(13); fw.write(10); fw.flush(); //System.out.println(content); out.flush(); } /** * This method is used to write the replies we get from the SMTP * mail server into the log file. In SMTP, all success code * begins with 2 or 3, so we check the first character to see if * there has been an SMTP problem. */ private String reply() throws IOException { String strInfo = in.readLine(); String flag = "23"; if (flag.indexOf(strInfo.charAt(0)) < 0) { throw new IOException("SMTP problem " + strInfo); } fw.write(strInfo); fw.write(13); fw.write(10); fw.flush(); //System.out.println(strInfo); //System.out.flush(); return strInfo; } /** Send a test message to and from mailSender... */ public static void main(String arg[]) { SMTPsender s = new SMTPsender(mailSender, mailSender, "WARNING! Your account is overdrawn."); s.send(); } }