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

Thread: help! Atm bank machine problem

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help! Atm bank machine problem

    Hello.

    Can somebody help me with this program code that contains a ATM program.
    The program should display the 10 latest transactions.
    In the array of int [] transactions = new int[10];
    The problem is that it only shows or prints only the 9 last transactions, when I chose to show the balance.
    What am I doing wrong with this code? Can somebody help me...
    Thanks for my attention.

    import java.util.Scanner;
     
     
    public class Atm 
    {	
     
    	private static int balance = 0;
     
     
        	public static void main(String[] args)
        	{
     
             	Scanner input = new Scanner(System.in);
     
     
             	int amount = 0;
             	int choice = 0;
             	int [] transactions = new int[10];
             	int sum;
     
     
             		while (choice != 4)
             		{
     
             			choice = menu();
     
     
             				switch(choice)
             				{
     
             					case 1:
     
             						System.out.print("Hur much you want to deposit? :");
     
             						sum = input.nextInt();
             							if(sum == 0)
             							{
             								System.out.print("Wrong amount");
             								System.out.println();
             								System.out.println();
             							}
             								else
             								{
     
             									amount = (int) + sum;
     
             									makeTransactions(amount, transactions);
             								}
             								break;
     
     
             					case 2:
     
             						System.out.print("How much do you want to withdrawl : ");
             						sum = input.nextInt();
             							if(sum == 0)
             							{	
     
             								System.out.print("Wrong amount");
             								System.out.println();
             								System.out.println();
             							}
             								else
             								{
             								amount = (int) - sum;
     
             								makeTransactions(amount, transactions);
             								}
             								break;
     
     
             					case 3:
     
             						showTransactions(transactions, balance);
             						break;
     
     
             					case 4:
     
             						System.out.println("Program exit");
             						break;
             				}	
     
            			 }
        }
     
        		public static int menu()
        		{	
        			Scanner input = new Scanner(System.in);
     
        			int choice = 0;
     
     
        				System.out.println("Enkel BankoMat ");
        				System.out.println();
        				System.out.println("1. Deposit ");
        				System.out.println("2. Withdrawl ");
        				System.out.println("3. balance ");
        				System.out.println("4. Exit ");
        				System.out.println();
        				System.out.println("Your choice: ");
     
     
        			choice = input.nextInt();
     
        				return choice;
     
        	} 
     
     
     
    			public static void showTransactions(int [] transactions, int balance)
    			{
     
    				System.out.println();
    				System.out.println("The 10 last transactions:");
    				System.out.println();
     
     
    					for(int i = 0; i <= transactions.length-1; i++)
    					{
    						if(transactions[i] == 0)
    						{
    						System.out.print("");
    						}
    							else
    							{	
    								for( i = 0;i < transactions.length-1; i++)
    								{	
    								System.out.println(transactions[i]);
    								}
    							}
     
    				}
     
    				System.out.println();
    				System.out.println("balance: " + balance + " kr" + "\n" );
    				System.out.println();		
     
    			} 
     
     
     
    			public static void makeTransactions(int amount, int [] transactions)
    			{
    				int position = findNr(transactions);
    				if(position == -1)
    				{
    					moveTrans(transactions);
    						position = findNr(transactions);
    						transactions[position] = amount;
    				}
    				transactions[position] = amount;
      				balance += amount;
     
    			}
     
     
    			public static int findNr(int [] transactions)
    			{
    				int position = -1;
     
    				for(int i = 0; i < transactions.length-1; i++)
    				{
    					if(transactions[i] == 0)
    					{
    						position = i;
    						break;
    					}	
    				}
    				return position;
    			}
     
     
    			public static void moveTrans(int [] transactions)
    			{
    				for(int i = 0; i < transactions.length-1; i++)
     
    					transactions[i] = transactions[i + 1] ;
     
    			}
     
     
    }
    Last edited by Freaky Chris; January 5th, 2011 at 06:11 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help! Atm bank machine problem

    Change it to transactions.length;

    Like this:

    for(int i = 0; i <= transactions.length-1; i++)
    {
    if(transactions[i] == 0)
    {
    System.out.print("");
    }
    else
     
    for( i = 0;i < transactions.length; i++)
    {
    System.out.println(transactions[i]);
    }
    }
    Last edited by Freaky Chris; January 5th, 2011 at 06:12 PM.

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: help! Atm bank machine problem

    @OP & JavaPenguin, please use [highlight=java][/highlight] tags, thanks.

    I've amended your posts.

    Chris

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help! Atm bank machine problem

    Quote Originally Posted by Freaky Chris View Post
    @OP & JavaPenguin, please use [highlight=java][/highlight] tags, thanks.

    I've amended your posts.

    Chris
    I normally would but didn't see the need as it was so short.

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: help! Atm bank machine problem

    Quote Originally Posted by javapenguin View Post
    I normally would but didn't see the need as it was so short.
    It's always nice to have syntax highlighting It also stops automatic formatting issues.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help! Atm bank machine problem

    No is still has the same problem.
    Instead of :

    10
    20
    30
    40
    50
    60
    70
    80
    90
    100


    with your code:

    20
    30
    40
    50
    60
    70
    80
    90
    100
    0

    The result is not right.......
    What is wrong with my code?????
    Cen someone help me out!

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help! Atm bank machine problem

    Ok.

    However, I know this probably isn't right:

    amount = (int) + sum;
     
                                                makeTransactions(amount, transactions);

    Looks like your casting to an int.

    Also, maybe if fixed here too, it might work.

     
    public static int findNr(int [] transactions)
                {
                    int position = -1;
     
                    for(int i = 0; i < transactions.length-1; i++)
                    {
                        if(transactions[i] == 0)
                        {
                            position = i;
                            break;
                        }  
                    }
                    return position;
                }

    If you're trying to get through an array and get to all of its elements.
     
    int[] arrayExample = new int[10];
     
    for (int i =0; i < arrayExample.length; i++)
    {
     
    }

    The code
    for (int i =0; i < arrayExample.length -1 ; i++)
    {
     
    }
    will ignore the element at the last index.

    However, if you changed it to
     
    for (int i =0; i <=arrayExample.length -1; i++)
    {
     
    }

    That should work too.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help! Atm bank machine problem

    Also, I might add, you should probably have so kind of way of stopping the user from going into the negatives.

    I mean, withdrawing far more than they have in their account.
    if ((account - sum) < 0)
    {
     
    }

    Also, it'd be best if you stopped them from adding negative amounts to deposit.

    if (sum < 0)
    {
     
    }

Similar Threads

  1. Bank Application Project please help
    By mbouster in forum Paid Java Projects
    Replies: 2
    Last Post: January 5th, 2011, 06:50 AM
  2. Pls Help me run bank java application
    By chukster in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2010, 07:28 PM
  3. Moving MySql Database from one machine to another machine
    By vaishali in forum JDBC & Databases
    Replies: 5
    Last Post: July 21st, 2010, 01:21 AM
  4. Please help - Bank account application
    By brandonssss in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 2nd, 2010, 03:10 PM
  5. Bank account GUI using swing
    By AlanM595 in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2009, 04:39 AM