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 4 of 4

Thread: Question About Program

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Question About Program

    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;
        }


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question About Program

    What's the dilly, I thought everyone here was suppose to know all about Java. I figured at least one person would have posted by now.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Question About Program

    Quote Originally Posted by metaleddie13 View Post
    What's the dilly, I thought everyone here was suppose to know all about Java. I figured at least one person would have posted by now.
    Based upon the timestamps of your posts - its been 2 hours. Give it some time. While we do our best, there is no guarantee people are online 24/7, and when we are there are hundreds of other posts here that are just as important as yours. This is not our job and no one is paid to be here.

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question About Program

    True, it was a joke anyway. Thanks.

Similar Threads

  1. First post, I have a question regarding this program
    By ResidentBiscuit in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 14th, 2011, 12:19 PM
  2. Question About A Program
    By torres9 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 15th, 2011, 08:56 PM
  3. Question about writing a phonebook sorting program
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 2
    Last Post: March 25th, 2011, 09:25 AM
  4. Question about my coin toss program
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2010, 04:14 AM
  5. [SOLVED] How to narrow down the range of input in java?
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2009, 11:38 AM