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

Thread: Program not ending correctly

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program not ending correctly

     
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    public class Assignment1 
    {
     
     
     
           public static CheckingAccount info;
     
     
     
         public static void main(String[] args)
        {
           String balance, message;
     
     
           double ibalance;
           boolean above = true;
           DecimalFormat formatter = new DecimalFormat("#0.00");
           int tcode = 1;
           double tamount;
     
     
           balance = JOptionPane.showInputDialog ("Enter your initial balance: ");
            ibalance = Double.parseDouble(balance);
     
            info = new CheckingAccount(ibalance);
     
     
     
     
           if (info.getBalance() < 500)
     
               above = false;
     
     
     
     
            while(tcode != 0)
            {
                          tcode = getTransCode();
     
     
            if (tcode == 1 )
            {
     
             tamount = getTransAmt();
     
              info.setBalance(tamount, tcode);
              info.setServiceCharge(tcode);
     
              if(info.getBalance() <500 && above ==true)
              {
                  info.setServiceCharge(1.0);
                  processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
                  above = false;
     
              }
              else if (info.getBalance() <0  && above == false)
              {
                  info.setServiceCharge(2.0);
                  processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
              }
              else if (info.getBalance() <0 && above == true)
              {
                  info.setServiceCharge(2.0);
                  processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
                  above = false;
              }
              else 
              {
                  processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
              }
     
            }
            else if (tcode ==2)
            {
     
                tamount = getTransAmt();
                info.setBalance (tamount, tcode);
                info.setServiceCharge(tcode);
                processDeposit (tcode, info.getBalance(), tamount, info.getServiceCharge());
            }
     
            }
            if (tcode ==3)
            {
     
     
                 message = "End of Transaction" + 
                     "\n" +
                       "\nTotal balance: $" + formatter.format(info.getBalance()) +
                       "\nTotal service charge: $" + formatter.format(info.getServiceCharge()) +
                       "\nFinal Balance: $" + formatter.format(info.EndBalance());
                       JOptionPane.showMessageDialog (null, message);
            }
        }
     
     
     
     
            public static int getTransCode()
            {
                int tcode;
                String transcode;
                do
                {
     
                      transcode = JOptionPane.showInputDialog ("Enter your transaction" + 
                           " code:" + 
                           "\n1 For Check" +
                           "\n2 For Deposit" +
                           "\n3 To Exit Program" );
                        tcode = Integer.parseInt(transcode);
     
     
     
                }while (tcode >2 || tcode < 0);
     
                 return tcode;
            }
     
            public static double getTransAmt()
            {
                  double tamount;
                  String transamount;
             transamount = JOptionPane.showInputDialog ("Enter your trans amount:");
             tamount = Double.parseDouble(transamount);
     
             return tamount;
            }
     
            public static void processCheck(int Transcode, double Balance, double TransAmt, double totalServiceCharge, boolean above)
            {
                DecimalFormat formatter = new DecimalFormat("#0.00");
                String message;
                if (Balance >= 500)
                {
                   message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nService Charge: Below $500 --- charge 5.00" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null,message);
     
                }
                else if (Balance < 500 && Balance > 0 && above == true)
                {
                     message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nService Charge: Below $500 --- charge 5.00" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
                }
                else if (Balance < 500 && Balance > 0 && above == false)
                {
                     message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
     
                }
                else
                {
                     message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nService Charge: Below $0 --- charge 10.00" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
     
                }
     
     
            }
     
            public static void processDeposit(int Transcode, double Balance, double TransAmt, double totalServiceCharge)
     
            {
                String  message;
                DecimalFormat formatter = new DecimalFormat("#0.00");
     
                message = "Transaction: Deposit in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Deposit --- charge $0.10" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
     
     
            }
     
     
     
     
     
    }

    public class CheckingAccount 
    {
        private double balance;
        private double totalServiceCharge = 0;
     
        public CheckingAccount(double number)
        {
     
            balance = number;
     
     
        }
     
        public double getBalance()
        {
            return balance;
     
     
     
        }
     
        public void setBalance(double transAmt, int tcode)
         {
     
               if(tcode == 1)
     
               {
     
                balance -= transAmt;
               }
     
     
     
               else if(tcode == 2)
               {
     
                   balance += transAmt;
               }
     
     
        }
        public double EndBalance()
        {
            balance = balance - totalServiceCharge;
            return balance;
     
        }
     
        public double CurrentBalance()
        {
            balance = balance - totalServiceCharge;
            return balance;
        }
     
        public double getServiceCharge()
        {
     
          return totalServiceCharge;
     
        }
        public void setServiceCharge(int tcode)
        {
     
            if (tcode ==1)
            {
                totalServiceCharge = totalServiceCharge + 0.15;
     
     
            }
            else    if (tcode ==2)
            {
                totalServiceCharge = totalServiceCharge + 0.10;
            }
     
     
        }
        public void setServiceCharge(double sale)
       {
           if (sale == 1)
           {
               totalServiceCharge = totalServiceCharge + 5.00;
     
           }
           else   if (sale ==2)
           {
               totalServiceCharge = totalServiceCharge + 10.00;
           }
     
     
       }
    }


    So as you can see, if the user inputs 3 into tcode, the program is supposed to print the last transaction invoice and end the loop/program.

    However, when the user inputs 3, the program does not end, and does not print the last transaction.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Program not ending correctly

    Quote Originally Posted by alex067 View Post
    ..However, when the user inputs 3, the program does not end, and does not print the last transaction.
    The main() method needs to get a value of 3 from getTransCode() in order to terminate the program the way you have in mind, right?

    Then:

    Look at the method that gives the transaction code to main():
            public static int getTransCode()
            {
                int tcode;
                String transcode;
                do
                {
                    transcode = JOptionPane.showInputDialog ("Enter your transaction" + 
                           " code:" + 
                           "\n1 For Check" +
                           "\n2 For Deposit" +
                           "\n3 To Exit Program" );
                    tcode = Integer.parseInt(transcode);
                } while (tcode > 2 || tcode < 0);
     
                 return tcode;
            }
    What happens when you enter a value of 3? How can it ever leave that loop?

    Cheers!

    Z

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not ending correctly

    Quote Originally Posted by Zaphod_b View Post
    The main() method needs to get a value of 3 from getTransCode() in order to terminate the program the way you have in mind, right?

    Then:

    Look at the method that gives the transaction code to main():
            public static int getTransCode()
            {
                int tcode;
                String transcode;
                do
                {
                    transcode = JOptionPane.showInputDialog ("Enter your transaction" + 
                           " code:" + 
                           "\n1 For Check" +
                           "\n2 For Deposit" +
                           "\n3 To Exit Program" );
                    tcode = Integer.parseInt(transcode);
                } while (tcode > 2 || tcode < 0);
     
                 return tcode;
            }
    What happens when you enter a value of 3? How can it ever leave that loop?

    Cheers!

    Z
    So how should the while statement be constructed in order to make tcode = 3 to input the last of the transaction and end the program?

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Program not ending correctly

    For what values of tcode do you want to leave the loop? Write them down. Write all of them down.

    For what values of tcode do you want to stay in the loop? Don't try to write all of them down. They are all of the values except the three values that you wrote, right? Can you think of a way to express these values with a mathematical expression involving ">" and "<" and logical operators?

    Now write a logic expression that is true for all values for which you want to stay in the loop. That's what goes into the parentheses at the end of do{}while();


    Cheers!

    Z

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not ending correctly

    Quote Originally Posted by Zaphod_b View Post
    For what values of tcode do you want to leave the loop? Write them down. Write all of them down.

    For what values of tcode do you want to stay in the loop? Don't try to write all of them down. They are all of the values except the three values that you wrote, right? Can you think of a way to express these values with a mathematical expression involving ">" and "<" and logical operators?

    Now write a logic expression that is true for all values for which you want to stay in the loop. That's what goes into the parentheses at the end of do{}while();


    Cheers!

    Z
    Values of tcode that would end the loop would just be 3

    Values for tcode to stay in the loop would be 1,2

    So I would do, }while (tcode > 0 || tcode < 3) ? So for every tcode greater than 0 (to make the tcode choice of 1 and 2 valid) and for every tcode less than 3 (to make tcode choice of 1 and 2 valid)


    Isnt the argument inside the while loop have to be false in order to return the value? For example, }while (statement), if the statement = false, then the loop would continue, and if the statement = true, the loop would end?

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Program not ending correctly

    Quote Originally Posted by alex067 View Post
    Values of tcode that would end the loop would just be 3
    That's just wrong.

    ...

    If the value of tcode is 1 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
    If the value of tcode is 2 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
    If the value of tcode is 3 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)

    If the value of tcode is less than 1 do you want to stay in the loop or exit? (Answer: Stay in the loop and let the user try again)
    If the value of tcode is greater than 3 do you want to stay int he loop or exit? (Answer: Stay in the loop and let the user try again)



    Quote Originally Posted by alex067 View Post
    Isnt the argument inside the while loop have to be false in order to return the value?
    I think you are getting it. I would put it like this, which is what I think you mean:
    If the value of the expression inside the parentheses is true, the loop continues. If the value is false, the loop terminates, and the function returns the value of tcode.


    Cheers!

    Z
    Last edited by Zaphod_b; October 14th, 2012 at 10:28 PM.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not ending correctly

    Quote Originally Posted by Zaphod_b View Post
    That's just wrong.

    ...

    If the value of tcode is 1 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
    If the value of tcode is 2 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
    If the value of tcode is 3 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)

    If the value of tcode is less than 1 do you want to stay in the loop or exit? (Answer: Stay in the loop and let the user try again)
    If the value of tcode is greater than 3 do you want to stay int he loop or exit? (Answer: Stay in the loop and let the user try again)



    I think you are getting it. I would put it like this, which is what I think you mean:
    If the value of the expression inside the parentheses is true, the loop continues. If the value is false, the loop terminates, and the function returns the value of tcode.


    Cheers!

    Z

    So if I wanted tcode 3 to terminate the program, wouldn't my while statement be:

    }while (tcode > 0 || tcode < 3);

    this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?


    If this is true, wouldn't any value greater than 3 terminate the program?

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not ending correctly

    Quote Originally Posted by Zaphod_b View Post
    That's just wrong.

    ...

    If the value of tcode is 1 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
    If the value of tcode is 2 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
    If the value of tcode is 3 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)

    If the value of tcode is less than 1 do you want to stay in the loop or exit? (Answer: Stay in the loop and let the user try again)
    If the value of tcode is greater than 3 do you want to stay int he loop or exit? (Answer: Stay in the loop and let the user try again)



    I think you are getting it. I would put it like this, which is what I think you mean:
    If the value of the expression inside the parentheses is true, the loop continues. If the value is false, the loop terminates, and the function returns the value of tcode.


    Cheers!

    Z


    So if I wanted tcode 3 to terminate the program, wouldn't my while statement be:

    }while (tcode > 0 || tcode < 3);

    this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?


    If this is true, wouldn't any value greater than 3 terminate the program?

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Program not ending correctly

    Quote Originally Posted by alex067 View Post
    }while (tcode > 0 || tcode < 3);

    this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?
    Did you try it? Is that really what the statement means? Try running the statement with -1, 0, 1, 2, 3, 4 and see what happens.

  10. #10
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Program not ending correctly

    Quote Originally Posted by alex067 View Post
    So if I wanted tcode 3 to terminate the program, wouldn't my while statement be:

    }while (tcode > 0 || tcode < 3);

    this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?
    That code implies the following:

    If tcode is greater than 0, continue the loop.
    If tcode is less than 3, continue the loop.
    For any other range of values, terminate the loop.

    That is really, really not what you want, right?

    What you want is:

    If tcode is less than zero continue the loop.
    If tcode is greater than 3 continue the loop.

    As jps says: If you get an idea that you think might work, try it. I can't see any point in asking whether might work. Try it!

    Try it with each and every one of the values for which you want to terminate the loop: 1, 2, 3
    Try it with various values other than these to make sure it stays in the loop and gives the user a chance to try again. Try some smaller numbers. Try some larger numbers.



    Cheers!

    Z
    Last edited by Zaphod_b; October 15th, 2012 at 08:56 AM.

Similar Threads

  1. Program not executing correctly
    By daemonlies in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 2nd, 2012, 12:14 AM
  2. Having trouble ending a game
    By wakaxwaka in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2012, 07:33 PM
  3. [SOLVED] JOptionPane preventing program from ending
    By RichTea in forum AWT / Java Swing
    Replies: 2
    Last Post: February 23rd, 2012, 07:42 AM
  4. Ending a method that has no return type
    By Blehs in forum Java Theory & Questions
    Replies: 3
    Last Post: August 12th, 2011, 02:56 AM
  5. program loops never ending
    By Ccogh05 in forum Loops & Control Statements
    Replies: 5
    Last Post: February 24th, 2011, 01:42 AM