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)
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
No comments:
Post a Comment