Results 1 to 7 of 7
  1. #1
    tsing520 is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Help changing code to different form

    I have the following code which all works but I want to do the same thing just using methods instead. How would I go about doing this? I'm VERY new to java (been programming just a few days and I've already hit this block =( Any hep will be much appreciated. Here's the code:

    Java Code:
    public class ATM {
    
    	public static void main (String[] args){
    		ATM myATM = new ATM();
    		myATM.go();
    		
    	}
    	
    
    	public void go() {
    
    		System.out.println("Welcome to online ATM banking");
    		System.out.println("How much do you want in your account?");
    
    		int balance = 0;
    
    		Toolbox myToolbox = new Toolbox();
    		
    		do{		
    			balance = myToolbox.readIntegerFromCmd();
    			if (balance < 0) {
    				System.out.println("please enter a positive balance");
    			} 
    
    		}while (balance < 0);
    		
    			System.out.println("Your balance  is " + balance);
    
    			int choice = 0;
    		
    			do{
    	
    				System.out.println("What do you want to do?");
    				System.out.println("1 : Withdraw");
    				System.out.println("2 : Deposit");
    				System.out.println("3 : Inquire");
    				System.out.println("4 : Quit");
    
    				choice = myToolbox.readIntegerFromCmd();
    
    				if (choice == 1) {
    					System.out.println("*****************************************");
    					System.out.println("              Withdrawal                 ");
    					System.out.println("*****************************************");
    					System.out.println("How much would you like to withdraw?");
    				
    					int amount = myToolbox.readIntegerFromCmd();
    					
    					if (amount > balance) {
    						System.out.println("You do not have enough money");
    				
    					}else if (amount < 0) {
    						System.out.println("Please enter a positive value");
    
    					}else {
    						balance = (balance - amount);
    					}
    
    
    				}else if (choice == 2) {
    					System.out.println("*****************************************");
    					System.out.println("                Deposit                  ");
    					System.out.println("*****************************************");
    					System.out.println("How much would you like to deposit?");
    			
    					int amount = myToolbox.readIntegerFromCmd();
    
    					if (amount < 0) {
    						System.out.println("Please enter a positive value");
    
    					}else {
    						balance = (balance + amount); 
    					}
    
    				}else if (choice == 3) {
    					System.out.println("*****************************************");
    					System.out.println("        Your balance is " + balance); 
    					System.out.println("*****************************************");
    			
    				}else if (choice == 4) {
    					System.out.println("*****************************************");
    					System.out.println("                Goodbye                  ");
    					System.out.println("*****************************************");
    
    				}
    
    			}while (choice < 4);
    	
    	}	
    		
    
    
    }
    Last edited by Tolls; 10-11-2013 at 03:28 PM. Reason: Added code tags

  2. #2
    tsing520 is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help changing code to different form

    sorry this was indented, not sure what happened when I posted it

  3. #3
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    55
    Rep Power
    0

    Default Re: Help changing code to different form

    I dont understand. You have a program that works. It uses methods. What do you want to change, and why?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Help changing code to different form

    Quote Originally Posted by tsing520 View Post
    sorry this was indented, not sure what happened when I posted it
    You need to put your code in [code] tags [/code].
    I've edited your post to do that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Help changing code to different form

    As for your question, break out the obvious bits into their own methods.
    This will involve making some of your data attributes of your class.

    For example add a method that returns the users choice. This method will hold the code for prompting the user for a choice, and reading the input, returning the value.
    Part of doing this will involve moving the toolbox out of the go() method and into the class as a member variable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    tsing520 is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help changing code to different form

    i'm very new to this- is there any way you could make what you said even simpler? The missing bits of code (such as the bit which prompts for the user to input a number) is handled by a seperate file called toolbox which i forgot to put here but is fine as it is- how would i go about moving it into the class as a member variable? Which bits would you say are the obvious bits i should do this to?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Help changing code to different form

    The obivous bits of code are in the if statement.
    Java Code:
    if() {
        // 10 lines of code
    } else if() {
        //15 lines of code
    } else {
        // 20 lines of code
    }
    // change to
    if() {
        methodA();
    } else if() {
        methodB();
    } else {
        methodC();
    }

Similar Threads

  1. Changing a code to include a main method
    By Shlr in forum New To Java
    Replies: 3
    Last Post: 04-23-2012, 04:33 PM
  2. Replies: 1
    Last Post: 05-27-2010, 09:48 PM
  3. Changing text colour in code
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-29-2009, 03:32 PM
  4. POI changing text to a form label
    By natt in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 10:49 AM
  5. Changing plugins source code
    By yakupemre in forum Eclipse
    Replies: 1
    Last Post: 08-15-2007, 09:31 AM

Tags for this Thread

Posting Permissions

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