
import java.awt.*;
import javax.swing.*;
import java.text.*;

/**
 * Class TuitionProject - an applet that calculates total tuition cost.
 * 
 * @author Chase Conner
 * @version JDK 8 
 * @course 1130-01
 * @date 9/18/15
 */
public class TuitionProject extends JApplet
{
    
        /**
         * Initialize all the components of calculating the tuition. 
         */
        double numCredits=3.0; //initializes the number of credits
        double costPerCredit=15000.00; //initializes the cost per credit
        double parkingFee=25.00; //initializes the parking fee
        double totalCostForBooks=1000.00; //initialize total cost for books
        double calculateTuition=12000.00; //initialize the method for calculating the tuition
        double result;
        
        String numberInStringFormat; 
        DecimalFormat myFormat = new DecimalFormat (".00"); 
    // instance variables - replace the example below with your own
    private int x;

    /**
     * Called by the browser or applet viewer to inform this JApplet that it
     * has been loaded into the system. It is always called before the first 
     * time that the start method is called.
     */
    public void init()
    {
        // this is a workaround for a security conflict with some browsers
        // including some versions of Netscape & Internet Explorer which do 
        // not allow access to the AWT system event queue which JApplets do 
        // on startup to check access. May not be necessary with your browser. 
        JRootPane rootPane = this.getRootPane();    
        rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
    
        // provide any initialisation necessary for your JApplet
        
        
        String input; //declare input as a strin
        
        /*
         * Turns the componenets into JOptionsPanes.
         */ 
  
        input =JOptionPane.showInputDialog (null, numberInStringFormat, "Type in your number of credits.",  JOptionPane.QUESTION_MESSAGE);
        numCredits=Double.parseDouble(input);
        
        input =JOptionPane.showInputDialog (null, numberInStringFormat, "Type in your cost per credit.",  JOptionPane.QUESTION_MESSAGE);
        costPerCredit=Double.parseDouble(input);
        
        input =JOptionPane.showInputDialog (null, numberInStringFormat, "Type in your parking fee.",  JOptionPane.QUESTION_MESSAGE);
        parkingFee=Double.parseDouble(input);
        
        input =JOptionPane.showInputDialog (null, numberInStringFormat, "Type in your total cost for textbooks.",  JOptionPane.QUESTION_MESSAGE);
        totalCostForBooks=Double.parseDouble(input);

        calculateTuition = (numCredits * costPerCredit) + parkingFee + totalCostForBooks; // Totals the costs of all componenets
        
        String FormattedcalculateTuition;
        FormattedcalculateTuition = myFormat.format(calculateTuition);
        
        JOptionPane.showMessageDialog(null,"Tuition Cost = \u0024"+FormattedcalculateTuition); //shows the total in a dialog box
        
    }
    
    /**
     * Called by the browser or applet viewer to inform this JApplet that it 
     * should start its execution. It is called after the init method and 
     * each time the JApplet is revisited in a Web page. 
     */
    public void start()
    {
        // provide any code requred to run each time 
        // web page is visited
    }

    /** 
     * Called by the browser or applet viewer to inform this JApplet that
     * it should stop its execution. It is called when the Web page that
     * contains this JApplet has been replaced by another page, and also
     * just before the JApplet is to be destroyed. 
     */
    public void stop()
    {
        // provide any code that needs to be run when page
        // is replaced by another page or before JApplet is destroyed 
    }

    /**
     * Paint method for applet.
     * 
     * @param  g   the Graphics object for this applet
     */
    public void paint(Graphics g)
    {
        //declares string to format the components into two decimal places
        String FormattedcostPerCredit;
        FormattedcostPerCredit = myFormat.format(costPerCredit);
        
        String FormattedparkingFee;
        FormattedparkingFee = myFormat.format(parkingFee);
        
        String FormattedtotalCostForBooks;
        FormattedtotalCostForBooks = myFormat.format(totalCostForBooks);
        
        String FormattedcalculateTuition;
        FormattedcalculateTuition = myFormat.format(calculateTuition);
        
        //Inserts and sets background image
        //Image source: http://www.clker.com/cliparts/v/f/9/u/U/B/notepad-page-hi.png
        Image img = getImage (getDocumentBase(), "Notepad.png"); 
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
       
        g.setColor(Color.blue);
        
        g.setFont(new Font ("Times New Roman", Font.BOLD, 30)); //changes the font to "Times new Roman, style to BOLD, and the size to 30
        g.drawString("Tuition Calculation Applet", 100, 60); //Adds title 
        
        g.setColor(Color.black);
        g.setFont(new Font ("Times New Roman", Font.PLAIN, 20)); // changes font to "Times New Roman", style to PLAIN, and size to 20
        
        /*
         * Displays the list of costs and the tuition total 
         */
        g.drawString("Number of Credits = " + numCredits, 120, 120);
        g.drawString("Cost per Credit = \u0024" + FormattedcostPerCredit, 120, 160);
        g.drawString("Parking Fee = \u0024" + FormattedparkingFee, 120, 200);
        g.drawString("Total Cost for Textbooks = \u0024" + FormattedtotalCostForBooks, 120, 240);
        
        
        g.setFont (new Font ("Times New Roman", Font. BOLD, 25)); //changes the font to "Times New Roman", the style to BOLD, and the size to 
        g.drawString("TOTAL TUITION = \u0024" + FormattedcalculateTuition, 120, 320);
        
        g.drawRect( 100, 85, 320, 175); //draws a rectangle around componenets 
        g.drawRect( 100, 290, 340, 50); //draws a rectangle around tuition cost 
        
        
        
        
        
        
        
        
        
    }

    /**
     * Called by the browser or applet viewer to inform this JApplet that it
     * is being reclaimed and that it should destroy any resources that it
     * has allocated. The stop method will always be called before destroy. 
     */
    public void destroy()
    {
        // provide code to be run when JApplet is about to be destroyed.
    }


    /**
     * Returns information about this applet. 
     * An applet should override this method to return a String containing 
     * information about the author, version, and copyright of the JApplet.
     *
     * @return a String representation of information about this JApplet
     */
    public String getAppletInfo()
    {
        // provide information about the applet
        return "Title: Tuition Project  \nAuthor: Chase Conner   \nA tuition calculation program. ";
    }


    /**
     * Returns parameter information about this JApplet. 
     * Returns information about the parameters than are understood by this JApplet.
     * An applet should override this method to return an array of Strings 
     * describing these parameters. 
     * Each element of the array should be a set of three Strings containing 
     * the name, the type, and a description.
     *
     * @return a String[] representation of parameter information about this JApplet
     */
    public String[][] getParameterInfo()
    {
        // provide parameter information about the applet
        String paramInfo[][] = {
                 {"firstParameter",    "1-10",    "description of first parameter"},
                 {"status", "boolean", "description of second parameter"},
                 {"images",   "url",     "description of third parameter"}
        };
        return paramInfo;
    }
}
