Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Sending an email of the results from 1 Class to another

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Sending an email of the results from 1 Class to another

    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
    import 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();   	
     
     
        }
    }
    Bank Account 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
    showDie 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
    SMTP Class
    // 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();
        }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Sending an email of the results from 1 Class to another

    Missing my die class but don't think its needed but here is the Error i am given from Eclipse


    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    mailSender cannot be resolved to a variable
    mailSender cannot be resolved to a variable

    at Playingdice.main(Playingdice.java:110)

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Sending an email of the results from 1 Class to another

    So how do you expect to access your mailSender variable in playingDice when It's only declared in your SMTPsender class?

    SMTPsender s = new SMTPsender(mailSender, mailSender, "WARNING! Your account is overdrawn.");
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Sending an email of the results from 1 Class to another

    Have it at the bottom of the driver program
    //Sending Email of Results		
     
     SMTPsender s = new SMTPsender(mailSender, mailSender,
        			"WARNING!  Your account is overdrawn.");
        	s.send();

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Sending an email of the results from 1 Class to another

    No, you've simply typed 'mailSender' twice.

    You need to provide accessors to get the value from your SMTPsender, or you need to declare and initialise mailSender in your test harness.
    What you've done is simply type 'mailSender' without any details as to what type it is and with no initialisation.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Sending an email of the results from 1 Class to another

    But I was under the impression that I was declaring it in when I
    SMTPsender s = new SMTPsender(mailSender, mailSender,
    			"WARNING!  Your account is overdrawn.");
    or at least calling the class, at least thats what I was doing in the program above

Similar Threads

  1. Sending large Strings ?! only sending a line
    By camel in forum Java Networking
    Replies: 2
    Last Post: April 19th, 2011, 12:41 PM
  2. How to Accumulate results
    By DreamNaut in forum Java Theory & Questions
    Replies: 2
    Last Post: October 29th, 2010, 01:10 AM
  3. Java problem -- sending email via JSP page
    By java_beginner in forum Java Theory & Questions
    Replies: 2
    Last Post: June 28th, 2010, 02:35 AM
  4. Football Results
    By RSYR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2009, 07:24 PM
  5. sending an email with pdf file everyday
    By pradeepptp in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: June 21st, 2009, 10:49 AM