I think I'm starting to get it, but have some questions - public classes

Discussion in 'Programming General' started by Burden, Apr 7, 2009.

I think I'm starting to get it, but have some questions - public classes
  1. Unread #1 - Apr 7, 2009 at 9:39 PM
  2. Burden
    Joined:
    Dec 30, 2008
    Posts:
    894
    Referrals:
    0
    Sythe Gold:
    0

    Burden Apprentice
    Banned

    I think I'm starting to get it, but have some questions - public classes

    I was making a test program, where I thought I coukld easily improve it as I go.

    It is a banking program, the first thign was this.

    1 class file
    Basically just had withdrawn, total, and deposited variables, no way of customizing them without changing source code.

    Second version was this
    2 class files
    methods.class
    deposit.class

    Open up deposit.class and it used methods I stated in methods.class, such as
    Code:
    public double withdraw( double amount )
    	{
                    // See if amount can be withdrawn
    		if (balance >= amount)
    		{
    			
    			balance -= amount;
    			totalWithdrawn += amount;
    			System.out.println("Withdrawal request accepted. Withdrawing..." );
                            return amount;
    		}
    		else
                    // Withdrawal not allowed
    				System.out.println("Not enough to withdraw, withdrawal request denied.");
                            return 0.0;
    And calling it like this in deposit.class
    Code:
    my_account.deposit(250.00);
    And it knew the deposit(); from methods.class, without me importing or stating or anything.

    But now, I'm stuck at user input.

    Here is account.class (used to be method.class)

    Code:
    import javax.swing.JOptionPane;
    
    public class Account 
    {
            protected double balance;
    		int totalWithdrawn;
    		static String amount;
    
            // Constructor to initialize balance
            public Account( double amount )
    	{
    		balance = amount;
    	}
    
            // Overloaded constructor for empty balance
            public Account()
    	{
    		balance = 0.0;
    	}
    
            public void deposit( double amount )
    	{
    		balance += amount;
    	}
    
            public double withdraw( double amount )
    	{
                    // See if amount can be withdrawn
    		if (balance >= amount)
    		{
    			
    			balance -= amount;
    			totalWithdrawn += amount;
    			System.out.println("Withdrawal request accepted. Withdrawing..." );
                            return amount;
    		}
    		else
                    // Withdrawal not allowed
    				System.out.println("Not enough to withdraw, withdrawal request denied.");
                            return 0.0;
    						
    	}
    //
            public double getbalance()
    	{
                    return balance;
    	}
    //	
    	public double getWithdrawn()
    	{
    		return totalWithdrawn;
    	}
    //
    	 public double withdrawInput()
    	 { 
    
              String userInput = JOptionPane.showInputDialog("Enter a value to withdraw: ");
              System.out.println(userInput);
              amount = (userInput);
              System.out.println("Withdrawing " +amount );
              return balance;
              
         }
    
    }  
    

    Here is withdrawInput, made to sue user input to state the withdraw amount.
    Code:
    import javax.swing.JOptionPane;
    
    
    public class withdrawInput 
    {
    
    		        public static void main(String args[])
    	{
                    // Create an empty account
                    Account my_account = new Account();
    				
    
                    // Deposit money
    		my_account.deposit(250.00);
    
                    // Print current balance
    		System.out.println ("Current balance: " +
    			my_account.getbalance());
    
                    // Withdraw money
    		my_account.withdraw(80.00);
    
                    // Print remaining balance
    		System.out.println ("Remaining balance: " + my_account.getbalance());
    		withdrawInput();
    			
    			// Print total amount withdrawn
    			System.out.println ("Total amount ever withdrawn: " + my_account.getWithdrawn());
    			
    
                    
    	}
    
    				private static void withdrawInput() {
    			          String userInput = JOptionPane.showInputDialog("Enter a value to withdraw: ");
    			          System.out.println(userInput);
    			          Object amount = (userInput);
    			          System.out.println("Withdrawing " +amount );
    			          return;
    					
    				}
    
    
         
    }
    first off, I know that private static void withdrawInput is in wrong spot, but every time I move it I get like 4 errors, and I spent forever trying to fix them, nothing worked.


    Questions: how do I fix the last 2 code s so that it will use the input dialogue to set the variable THAT IS NOT A STRING.

    I tried about 50 different ways, I keep getting told it's a string, then getting told it's not 3 errors later, etc.

    I'm really stuck :(
     
  3. Unread #2 - Apr 8, 2009 at 9:15 AM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    I think I'm starting to get it, but have some questions - public classes

    Your withdrawInput() method in the account class should look something like this.
    Code:
    	 public double withdrawInput()
    	 { 
              double userInput = 0;
              try {
                  double userInput = Double.parseDouble(JOptionPane.showInputDialog("Enter a value to withdraw: "));
                  withdraw(userInput);
              }
              catch(Exception e) {
              }
             return userInput;
         }
    
    Now I don't know why you're defining the withdrawInput() method again in the WithdrawInput class since you just want to use the one from the Account class anyway. In your main method you should just be calling my_account.withdrawInput().
     
< selling my expertise to make stuff. | severe issues >

Users viewing this thread
1 guest


 
 
Adblock breaks this site