simple atm machine

simple atm machine that an deposit, withdrawl, show 10 transactions

Page 1 of 1

9 Replies - 43184 Views - Last Post: 08 December 2010 - 11:15 PM Rate Topic: -----

#1 galva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-December 10

simple atm machine

Posted 07 December 2010 - 01:01 PM

Hello

I wonder if someone can help me with a simple ATM machine
that can deposit, withdrawl an show the 10 latest transactions in an array of 10 index values.
The problem is that i can't see the transactions after the 10:th.
the program don't take the amount or don't display the transactions after the 10:th operation.

Could someone help me with this.
I have written a code and i don't know wath wrong i doo in the soure code.

Here is the source code:

import java.util.Scanner;


public class BankoMat 
{
	
    public static void main(String[] args)
    {
         Scanner keyboard = new Scanner(System.in);
         
         int amount = 0;
         int choice = 0;
         int [] transactions = new int[10];
         int sum;
         int balance = 0;
         
         while (choice != 4)
         {
         	choice = menu();
         	switch(choice)
         	{
         		case 1:
         			System.out.print("Enter amount to deposit? :");
         			sum = keyboard.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("Hoe much do you want to withdrawl?: ");
         				sum = keyboard.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("You chosed to end ");
         				break;
         	}	
         	
         }
    }
    
    
    	public static int menu()
    	{
    		Scanner keyboard = new Scanner(System.in);
    		int choice = 0;
    		
    		System.out.println("Simple ATM");
    		System.out.println();
    		System.out.println("1. Deposit ");
    		System.out.println("2. Withdrawl ");
    		System.out.println("3. transactions ");
    		System.out.println("4. End ");
    		System.out.println();
    		System.out.println("Your choice ");
    		
    		choice = keyboard.nextInt();
    		return choice;
    	}
    	
		public static void showTransactions(int [] transactions, int balance)
		{
			System.out.println();
			System.out.println("Last transactions");
			System.out.println();
			
				for(int i = 0; i < transactions.length-1; i++)
				{
					if(transactions[i] == 0)
					{
						System.out.print("");
					}
					
						else
						{
							System.out.print(transactions[i] + "\n");
							balance = balance + 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;
			}
			else
			{
				transactions[position] = 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[0] = transactions[i + 1] ;
				
		}
		

}


This post has been edited by baavgai: 07 December 2010 - 01:54 PM
Reason for edit:: tagged


Is This A Good Question/Topic? 0
  • +

Replies To: simple atm machine

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: simple atm machine

Posted 07 December 2010 - 04:35 PM

This might clear things up a little for you. Have a read and see how you go ;)
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: simple atm machine

Posted 07 December 2010 - 05:50 PM

All your for() loop but the one in moveTrans should be

for(int i = 0; i < transactions.length; i++)

not

for(int i = 0; i < transactions.length-1; i++)
Was This Post Helpful? 0
  • +
  • -

#4 Guest_galva*


Reputation:

Re: simple atm machine

Posted 07 December 2010 - 11:14 PM

View PostDaneAU, on 07 December 2010 - 03:35 PM, said:

This might clear things up a little for you. Have a read and see how you go ;)



Hello DaneAU.

I have looked in the link you have post to me. But I'am not sure about if
the link and my question. I think it has to be that my array wont't work to
rewrite the new transactions after the 10:th, 11:th, 12:th and so on...
thank you anyway.
Was This Post Helpful? 0

#5 Guest_galva*


Reputation:

Re: simple atm machine

Posted 07 December 2010 - 11:18 PM

View Postpbl, on 07 December 2010 - 04:50 PM, said:

All your for() loop but the one in moveTrans should be

for(int i = 0; i < transactions.length; i++)

not

for(int i = 0; i < transactions.length-1; i++)


Hello pbl.

I have tried to delete the -1 in the for loops, but a got error message.
I don't kno why.
The problem is that i can't rewrite the array, for example, the first 10 entered amount the program works fine, but after the 11:th, 12:th and so on, the
transactions wont be deleted for the new entrys. I don't know why?
Thank you anyway.
Was This Post Helpful? 0

#6 Guest_Brian*


Reputation:

Re: simple atm machine

Posted 08 December 2010 - 09:03 AM

Try creating a larger size array to store your transactions.
Was This Post Helpful? 0

#7 Guest_galva*


Reputation:

Re: simple atm machine

Posted 08 December 2010 - 10:09 AM

View PostBrian, on 08 December 2010 - 08:03 AM, said:

Try creating a larger size array to store your transactions.

Yes, but the problem is that i have to show only 10 indexes of transactions and there
1 2 3 4 5 6 7 8 9 10,
The 11:th transaction should replace the 1:st transaction.
the 12:th transaction should replace the 2:nd transaction when its printed out.
Someone who knows how I can solve the problem?
Was This Post Helpful? 0

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: simple atm machine

Posted 08 December 2010 - 10:40 AM

Use the modulus operator. Take transactionIndex%10, and that's your index for the transaction. Also, remember that arrays are indexed from 0 through length-1.
Was This Post Helpful? 0
  • +
  • -

#9 galva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-December 10

Re: simple atm machine

Posted 08 December 2010 - 11:13 AM

View Postmacosxnerd101, on 08 December 2010 - 09:40 AM, said:

Use the modulus operator. Take transactionIndex%10, and that's your index for the transaction. Also, remember that arrays are indexed from 0 through length-1.


hello again.

I tryed to change the method moveTrans()
I wrote the this instead:

public static void moveTrans(int[] transactions) {
for (int i = 0; i < transactions.length - 1; i++)
transactions[i] = transactions[i + 1];

transactions[transactions.length-1] = 0;

}

The problem of the arrays are fixed.
I found a new problem and it is when i do withdrawl and after withdrawl when i look the balance, there something wrong
in the calculation, it seems that the balance is wrong, for example. What wrong is with my code? It does't seem to be correct balance:
Can someone help me of this problem?


10 last transactions:

1
-10
10
10
10
10
10
9
-55

Balance: -5 kr
Was This Post Helpful? 0
  • +
  • -

#10 galva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-December 10

Re: simple atm machine

Posted 08 December 2010 - 11:15 PM

it seems strange. I think the array only save the 10 transactions. But what about the balance?
I Try to make about 15 transactions with deposit and withdrawl. Look att the balance. It seems strange to me????
Can you note something wrong that I don't understand, maybe is the array of only 10 saved index?
Anyway the balance should save all the transactions from the beginning...
Do you have any idea about that, so the balance will save all transactions from beginning?


this is what the program should do:
array of 10 index were the latest 10 transactions will be saved.
The balance should save all the deposit and withdrawl from the beginning.
the program should show the 10 latest transaction and show the final balance from all inputs of the user.

Can someone help me out how i can go thru this problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1