Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2016
    Posts
    7
    Rep Power
    0

    Default Final project in begginers java class giving me grief

    I am in a small class for beggining java. My last project is to take an existing code and make it work better. It is a simple banking program, but i believe i am making it out to be more than it is. I am very close to being done. However these are my issues im having.

    When user clicks "deposit" you can make a deposit but it immediately also makes you make a withdrawal. (I believe this is an issue with my while loop.)

    If the user types jibberish or nothing, it crashes the program. However i have error checking located in the getDouble method. I just dont believe it is calling it properly. This method should also prevent user from going into the negatives.

    Here is my code.

    Java Code:
    import javax.swing.*;
    import javax.swing.ImageIcon.*; 
    import java.lang.*;  // loaded by default
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    import java.awt.Font;  // need for Font and Point Size Changes
    import java.awt.Color;  // need to change Color
    import java.text.*;
                             
    
    class Lab3Final   // class Name will begin with a Capital Letter.
    {
    
        static double balance = 0;
        static double newBalance = 0;
        static double adjustment = 0;
        final static int EXIT = 4;     
        final static int X = 0;
        
        public static void main (String[] args)
        {
              runit();  // call method to start work
    			 
    			 System.exit (0);  // close program
    
        } // end of main
    	 
    	 public static void runit ()
    	 {
          setUI();
          openingmsg();
         
           
    	 
    	 }//method to tell order
        
        
        public static void openingmsg ()
        {
                
    	         int n = JOptionPane.showConfirmDialog(
                null,
                "Welcome, Would you like to do some banking?",
                "Bank of Adam",
                JOptionPane.YES_NO_OPTION);
                
                if (n == JOptionPane.YES_OPTION)
                {
                   int ch = getBankingChoice ();
                   getBankingChoice();
                
                }
                
                else
                {
                   final ImageIcon icon = new ImageIcon("E:\\AMillerLab2\\guess.png.png");
                   JOptionPane.showMessageDialog(null, "Goodbye!", "Closing screen", JOptionPane.INFORMATION_MESSAGE, icon);
                   System.exit (0);
                }
        }//opening message, and continue option
        
          public static void closingMsg ()
          {
             final ImageIcon icon = new ImageIcon("E:\\AMillerLab2\\guess.png.png");
             JOptionPane.showMessageDialog(null, "Goodbye!", "Closing screen", JOptionPane.INFORMATION_MESSAGE, icon);
             System.exit (0);
    
          }
       
           public static void getCom ()    
          {
          
          }
           public static void setUI ()
          {
             UIManager um = new UIManager();
             um.put("OptionPane.messageForeground", Color.yellow);
             um.put("Panel.background", Color.black);
    
    		 
    		   Font font1 = new Font("serif",Font.BOLD,24);
             Font font2 = new Font("sansserif",Font.BOLD,18);
    		 
             um.put ("Label.font", font1);
             um.put ("Button.font", font2);
             um.put ("TitledBorder.font",font2);
             um.put ("TextField.font",font1);
        
          }//UI manager.
        
          public static void makeDeposit ()
          {
              NumberFormat fmt = NumberFormat.getCurrencyInstance();
             adjustment = Double.parseDouble
    			(JOptionPane.showInputDialog( "Enter the Deposit Amount" ));
    		   newBalance = balance + adjustment;
    		   JOptionPane.showMessageDialog(null, "*** Bank of Adam ***\n\n" +
    				"Old Balance is: " + fmt.format(balance) + "\n" +
    				"Adjustment is: +" + fmt.format(adjustment) + "\n" +
    				"New Balance is: " + fmt.format(newBalance) + "\n");
    		   balance = newBalance;
    
          }//to make a deposit
          
          public static void makeWithdrawal()
          {
              NumberFormat fmt = NumberFormat.getCurrencyInstance();
                adjustment = Double.parseDouble
                (JOptionPane.showInputDialog( "Enter the Withdrawal Amount" ));
    		      newBalance = balance - adjustment;
    		      JOptionPane.showMessageDialog
    			(null, "*** Bank of Adam ***\n\n" +
    			"Old Balance is: " + fmt.format(balance) + "\n" +
    			"Adjustment is: -" + fmt.format(adjustment) + "\n" +
    			"New Balance is: " + fmt.format(newBalance) + "\n");
    		balance = newBalance;
    
          }//to make a withdrawal
          
          public static int getBankingChoice() 
          {  
                int mc = JOptionPane.QUESTION_MESSAGE;  
          String[] opts = { "Deposit", "Withdrawal", "Balance","Exit" };          
          int ch = JOptionPane.showOptionDialog (null, "Choose a banking operation!",
           "Banking Choices", 0, mc, null, opts, opts[2]);  
            
            int choice;
            boolean moreBankingBusiness; 
            moreBankingBusiness = true; 
          while (moreBankingBusiness)  // easier to understand?
          {                   
             ch++;
    
            switch (ch)
            {
             case 1:
                   makeDeposit();
                   break;
             case 2:
                   makeWithdrawal();
                   break;
             case 3:
                   checkBalance();
                   break;
           
             default:
                    moreBankingBusiness = false;      
            }//switch statement to get user banking choice
                 
           }//end of while       
            return ch; 
          }// get banking choice 
                                                 
          public static void checkBalance()
        	{
              NumberFormat fmt = NumberFormat.getCurrencyInstance();
    		   JOptionPane.showMessageDialog
    		   	(null, "*** Bank of Adam ***\n\n" +
    		   "Your Current Balance Balance is: " + fmt.format(balance) );
    	   }
          
          public static double getDouble()
          {
          double val = 0;
          
          while (true) // a seemingly endless loop
          {
             // loop until we get a valid int
            String s=JOptionPane.showInputDialog("Enter a whole number" );
            if (s == null)
            {         
             break;
            
             } // cancel if
           else
             if (s.equals(""))
             {
               JOptionPane.showMessageDialog
                (null, "You must make an entry in the Input Box");
             } // entered nothing
           try 
        {  
         val = Double.parseDouble (s);     
         if (val > 0)     
         {       
         return val;  //return the number to the calling method    
          }// if    
           else    
            {       
            JOptionPane.showMessageDialog (null, "Transactions need to be greater than zero.");     
            } // els e
              val = Double.parseDouble (s);
              break;
           }// try
           catch (Exception e) // generic Exception
           {
             JOptionPane.showMessageDialog(null, "Enter a whole number ");
             
            }// catch
         } // while
         
         return val;//return the number to the calling method     
              
         } //end getDouble
    
    
       }//end of class

    Any help is greatly appreciated. This has been stumping me since Sunday.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Final project in begginers java class giving me grief

    Your loop surrounding the switch statement increments ch. So first time thru it is 1 and selects deposit. Second time thru it is 2 and selects withdrawal.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Apr 2016
    Posts
    7
    Rep Power
    0

    Default Re: Final project in begginers java class giving me grief

    So i need to remove ch++?
    Edit: Removing ch++ made the program close after clicking deposit.
    Last edited by killahrepublic; 04-20-2016 at 09:29 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Final project in begginers java class giving me grief

    Quote Originally Posted by killahrepublic View Post
    So i need to remove ch++?
    It appears you are using ch select the transaction type. So you need to prompt for it so the user may select the transaction type within the loop.
    Then switch based on transaction type. Use a different type (say the number 4) to exit the process altogether.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Join Date
    Apr 2016
    Posts
    7
    Rep Power
    0

    Default Re: Final project in begginers java class giving me grief

    So put the entire selecting process in the while loop? Im a bit confused what you mean by using a different exit process.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Final project in begginers java class giving me grief

    Here is a contrived example of what I was talking about.

    Java Code:
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class PromptDemo {
    
       public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          boolean prompting = true;
          while (prompting) {
             try {
                System.out.print("Please enter 1, 2, or 3: ");
                int v = in.nextInt();
                switch (v) {
                   case 1:
                      System.out.println("You entered a 1.");
                      break;
                   case 2:
                      System.out.println("You entered a 2.");
                      break;
                   case 3:
                      System.out.println("You entered a 3.");
                      break;
                   default:
                      System.out.println("Unknown selection, exiting loop.");
                      prompting = false;
                }
             }
             catch (InputMismatchException ime) {
                // non-integer entered. Ignore and clear input buffer
                in.nextLine();
             }
          }
          System.out.println("Done!");
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. java final project
    By mikelmb in forum New To Java
    Replies: 8
    Last Post: 05-14-2014, 12:52 PM
  2. Final Java Project
    By ExX in forum New To Java
    Replies: 1
    Last Post: 05-06-2012, 09:57 AM
  3. Replies: 8
    Last Post: 12-06-2011, 01:14 AM
  4. Help me? Class final project.
    By Blue Energy in forum Advanced Java
    Replies: 2
    Last Post: 05-14-2011, 06:39 AM
  5. Java's Class Final Project - Tower Defense
    By lastpirate89 in forum New To Java
    Replies: 6
    Last Post: 05-11-2011, 03:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •