Friday, February 4, 2011

A Nifty Banking Programme

Mostly taken from John Smiley's book on "Learn to program with Java", but i do face some problems which I don't yet know how to rectify (I.E. nullPointerExceptions)
Anyway for this programme, I created a separate class file to deal with the calculations and the actual file to interact with the user (or something like that I don't know how to express this well)

Photobucket

These are the 2 errors

The first is when you input an invalid amount (eg an alphabet) to deposit or withdraw, so I guess to rectify it would be to put in place a check before running the transaction.

The second is whenever the user presses cancel at a position where I had not put in place the "== null" check. One problem with this is when the programme loops I can't put this check in place properly hence on the first round it terminates properly, but once it loops it shows the error message. Another time this shows is when the "BankTransaction" file runs and the user presses cancel at the "enter deposit/withdrawing amount". This problem hopefully I'll find out how to solve soon.

Codes:

(Class File)

import javax.swing.JOptionPane;
public class BankTransaction
{
double adjustment;
double newBalance;
static double balance; // Class Variables!


public BankTransaction()
{
System.out.println ("BankTransaction's constructor");
}


public void makeDeposit()
{
adjustment = Double.parseDouble (JOptionPane.showInputDialog("Enter the Deposit Amount"));
if (adjustment == null)
{
JOptionPane.showMessageDialog (null, "Thank you for Banking with us!");
System.exit (0);
}
newBalance = balance + adjustment;
JOptionPane.showMessageDialog (null, "*** SMILEY NATIONAL BANK *** \n\n" +
"Old Balance is: " + balance + "\n" +
"Adjustment is: +" + adjustment + "\n" +
"New Balance is: " + newBalance + "\n");
balance = newBalance;
} //end of make deposit


public void makeWithdrawal()
{
adjustment = Double.parseDouble (JOptionPane.showInputDialog("Enter the Withdrawal Amount"));
if (adjustment == null)
{
JOptionPane.showMessageDialog (null, "Thank you for Banking with us!");
System.exit (0);
}
newBalance = balance - adjustment;
JOptionPane.showMessageDialog (null, "*** SMILEY NATIONAL BANK *** \n\n" +
"Old Balance is: " + balance + "\n" +
"Adjustment is: -" + adjustment + "\n" +
"New Balance is: " + newBalance + "\n");
balance = newBalance;
} //end of make withdrawal


public void getBalance()
{
JOptionPane.showMessageDialog (null, "*** SMILEY NATIONAL BANK ***\n\n" +
"Your current balance is: " + balance );
} //end of getBalance method


} //end of class


(Main File)


import javax.swing.JOptionPane;
class NationalBank 
{
public static void main (String[] args) 
{
String response;
String moreBankingBusiness;


moreBankingBusiness = JOptionPane.showInputDialog ("Do you want to do some banking?");


if (moreBankingBusiness == null)
{
JOptionPane.showMessageDialog (null, "Thank you for Banking with us!");
System.exit (0);
}
else
moreBankingBusiness = moreBankingBusiness.toUpperCase();
while (moreBankingBusiness.equals ("YES")) 
{
response = JOptionPane.showInputDialog("What would you like to do?" + "(1=Desposit, 2=Withdraw, 3=Get Balance)");
if (response == null)
{
JOptionPane.showMessageDialog (null, "Thank you for Banking with us!");
System.exit (0);
}
else
if (response.equals (""))
{
JOptionPane.showMessageDialog (null, "You must make an entry into the input box!");
System.exit (0);
}
else
if (Integer.parseInt (response) < 1 | Integer.parseInt (response) > 3)
{
JOptionPane.showMessageDialog (null, response + " - is not a valid input.");
System.exit (0);
}
if (Integer.parseInt (response) == 1)
{
BankTransaction transaction = new BankTransaction();
transaction.makeDeposit ();
}

if (Integer.parseInt (response) == 2)
{
BankTransaction transaction = new BankTransaction();
transaction.makeWithdrawal ();
}
if (Integer.parseInt (response) == 3)
{
BankTransaction transaction = new BankTransaction();
transaction.getBalance ();
}
moreBankingBusiness = JOptionPane.showInputDialog ("Do you have more banking business?");
moreBankingBusiness = moreBankingBusiness.toUpperCase();
} //end of while


JOptionPane.showMessageDialog (null, "Thanks for banking with us!");
System.exit (0);
} //end of main
} //end of class

Wednesday, February 2, 2011

What's your name?

What's your name?

Lester

(When pressing enter without any input)

Photobucket

(When pressing cancel)

Photobucket

I made 2 versions of this simple programme.  The first had no looping involved, meaning once I clicked 'ok' the programme will end. Following which I made a second version, which looped back to asking for your name when you had wrongly pressed enter without any input.

(Version 1):

import javax.swing.JOptionPane;
class Hello_sir {

public static void main (String args[]) {
String response;
response = JOptionPane.showInputDialog
("How may I address you by?");


if (response == null)
JOptionPane.showMessageDialog
(null, "Bye!");


else
if (response.equals(""))
JOptionPane.showMessageDialog
(null, "You must make an entry in the box!");

else
JOptionPane.showMessageDialog
(null, "It's nice to meet you, " + response);
System.exit (0);
} //End of main
}


(Version 2):

import javax.swing.JOptionPane;
class Hello_sir_a {


public static void main (String args[]) {
String response;
response = JOptionPane.showInputDialog
("How may I address you by?");


if (response == null)
JOptionPane.showMessageDialog
(null, "Bye!");


else
if (response.equals("")){
while (response.equals("")) {
JOptionPane.showMessageDialog
(null, "You must make an entry in the box!");
response = JOptionPane.showInputDialog
("How may I address you by?");
if (response == null)
{JOptionPane.showMessageDialog
(null, "Bye!");
System.exit (0);}


}
JOptionPane.showMessageDialog
(null, "It's nice to meet you, " + response);
}
else
JOptionPane.showMessageDialog
(null, "It's nice to meet you, " + response);
System.exit (0);
} //End of main
}

Do you love java?

Photobucket

code:

import javax.swing.JOptionPane;
class Lovejava {
public static void main (String args[]) {
JOptionPane.showMessageDialog (null, "I Love Java!");
}
}

Post #1

Hello World!
I shall begin to record down all the things I learn in this spunky blog!
Of course, this is subject to my avaliability