I have to write the methods for a program and the first one, a boolean method called openAccount, takes first name, last name and opening amount [positive or negative] as a starting amount. This method sets all the class variables properly upon call and returns a true (unless the account was already marked as being opened in which case it returns a false. Note: if any operation on an account makes the dollars go into the negative range, then the account should be flagged as being overdrawn. The class variable isOpened is not the same as what is being returned from this method. I was wondering what would be a simple method that sets all the class variables properly? I have tried a lot of different true, false problems, but cannot get it. If someone can help, I would greatly appreciate it. The main method was provided and cannot be changed.
import javax.swing.*; public class Program4 { // Class Variables. public static String firstName = ""; public static String lastName = ""; public static double dollars = 0.0; public static boolean isOpened = false; public static boolean isOverdrawn = false; // Replace "CIS Faculty" with your name. private final static String TITLE_BAR = "Program 4 (My Name)"; // Class Methods // Add your methods below here. // You should leave the main method below ALONE!! public static void main(String[] args) { showAccount(); // Will show error message about account not open // and will then continue. // Open new account. Give error message if it fails! if (!openAccount("Bill", "Smith", 100.50)) { reportErrorAndBomb("Coding error 1. Open account failed on a new account!"); } showAccount(); // Check that accountOwner works correctly - bomb if not. if (!accountOwner().equals("Bill Smith")) { reportErrorAndBomb("Coding error 2. Call to accountOwner failed."); } if (balance() != 100.50) // Verify balance is correct! { reportErrorAndBomb("Coding error 3. Balance is wrong!"); } // Confirm that I cannot reopen it! if (openAccount("Bogus", "Try", 55.00)) { reportErrorAndBomb("Coding error 4. You allowed an account " + "to be re-opened!"); } deposit(50.00); showAccount(); if (balance() != 150.50) { reportErrorAndBomb("Coding error 5. Balance is wrong!"); } if (isNowOverdrawn()) { reportErrorAndBomb("Coding error 6. Reports overdrawn when it should not."); } // Confirm correct workings of approveCheckFor method. if (approveCheckFor(150.51)) { reportErrorAndBomb("Coding error 7. Approved a check for too much."); } if (!approveCheckFor(150.50)) { reportErrorAndBomb("Coding error 8. Failed to approve a check " + "for a good amount!"); } withdraw(25.00); if (balance() != 125.50) { reportErrorAndBomb("Coding error 9. Balance is wrong!"); } withdraw(125.75); if (balance() != -0.25) { reportErrorAndBomb("Coding error 10. Balance is wrong!"); } showAccount(); // Should show a deficit of 25 cents. // and that account is now overdrawn. if (!isNowOverdrawn()) { reportErrorAndBomb("Coding error 11. Should respond as overdrawn now."); } // Well... if you made no calls above to reportErrorAndBomb it might not // be working... so let us end the program with its use. reportErrorAndBomb("No Errors reported after testing all methods. " + "\nThis tests reportErrorAndBomb. \nTesting complete with no errors!"); } // main private static boolean isNowOverdrawn() { if (balance() < dollars) // To check if the account is now overdrawn // after a transaction. return true; else return false; } private static boolean approveCheckFor(double cash) { if (cash <= balance()) // To check if incoming check is less than or // more than balance. return true; else return false; } private static double balance() { return dollars; // Enter dollars into balance. } private static void withdraw(double moneyInBank) { if (moneyInBank >= dollars) // Withdrawn amount. dollars -= moneyInBank; } private static void deposit(double amount) { dollars += amount; // Deposited amount. } private static void reportErrorAndBomb(String message) { JOptionPane.showMessageDialog(null, message, TITLE_BAR, // Prints the // error message // to // JOptionPane. JOptionPane.ERROR_MESSAGE); // and exit. System.exit(0); } private static String accountOwner() { return firstName + " " + lastName; // Return first name, space, last name. } private static void showAccount() { String.format("%.2f", dollars); // Format output of dollars two decimal // places. if (isOpened) // If account already exists. JOptionPane.showMessageDialog(null, "Account Owner: " + firstName + lastName + "\n" + "Account Balance: " + dollars + "\n" + "Account Overdrawn: " + isOverdrawn); else JOptionPane.showMessageDialog(null, "You have attempted to display an accont which is not " // Displayed if account does not exist. + "opened yet. ", TITLE_BAR, JOptionPane.ERROR_MESSAGE); } private static boolean openAccount(String name, String name2, double money) { return false; }