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! simple ATM

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

    Default help! simple ATM

    /*Hello. I wonder if someone could help me with this simple atm machine.
    I need to show 10 transactions in 10 arrays. Show the last 10 transactions and when the user comes
    to the 11:eth transaction, the program shold erase the first index array and show the last transaction.
    So the program need to show highly only 10 transaction and follow to overwrite the first transaction and so on. the balance should always be saved with all the transfers. I can't understand wath im doing wrong. Maybe someone can help me on the way...
    I will appriciate all help. Thanks.*/

    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("Vilken summa vill du sätta in? :");
             				sum = keyboard.nextInt();
             					if(sum == 0)
             					{
             						System.out.print("Du har angett ett felaktigt belopp.");
             						System.out.println();
             						System.out.println();
             					}
             						else
             						{
             							amount = (int) + sum;
             							makeTransactions(amount, transactions);
             						}
             						break;
     
             		case 2:
             				System.out.print("Vilken summa vill du ta ut?: ");
             				sum = keyboard.nextInt();
             					if(sum == 0)
             					{
             						System.out.print("Du har angett ett felaktigt belopp.");
             						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("du valde att avsluta ");
             				break;
             	}	
     
             }
        }
     
     
        	public static int menu()
        	{
        		Scanner keyboard = new Scanner(System.in);
        		int choice = 0;
     
        		System.out.println("Enkel bankomat ");
        		System.out.println();
        		System.out.println("1. Insättning ");
        		System.out.println("2. Uttag ");
        		System.out.println("3. Saldobesked ");
        		System.out.println("4. Avsluta ");
        		System.out.println();
        		System.out.println("Ditt val: ");
     
        		choice = keyboard.nextInt();
        		return choice;
        	}
     
    		public static void showTransactions(int [] transactions, int balance)
    		{
    			System.out.println();
    			System.out.println("Tidigare transaktioner :");
    			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("Saldo: " + 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] ;
     
    		}
     
     
    }


  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! simple ATM

    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("Vilken summa vill du sätta in? :");
    sum = keyboard.nextInt();
    if(sum == 0)
    {
    System.out.print("Du har angett ett felaktigt belopp.");
    System.out.println();
    System.out.println();
    }
    else
    {
    amount = (int) + sum;
    makeTransactions(amount, transactions);
    }
    break;
     
    case 2:
    System.out.print("Vilken summa vill du ta ut?: ");
    sum = keyboard.nextInt();
    if(sum == 0)
    {
    System.out.print("Du har angett ett felaktigt belopp.");
    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("du valde att avsluta ");
    break;
    }
     
    }
    }
     
     
    public static int menu()
    {
    Scanner keyboard = new Scanner(System.in);
    int choice = 0;
     
    System.out.println("Enkel bankomat ");
    System.out.println();
    System.out.println("1. Insättning ");
    System.out.println("2. Uttag ");
    System.out.println("3. Saldobesked ");
    System.out.println("4. Avsluta ");
    System.out.println();
    System.out.println("Ditt val: ");
     
    choice = keyboard.nextInt();
    return choice;
    }
     
    public static void showTransactions(int [] transactions, int balance)
    {
    System.out.println();
    System.out.println("Tidigare transaktioner :");
    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("Saldo: " + 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] ;
     
    }
     
     
    }

    Let's see what's going on.

  3. #3
    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! simple ATM

    If sum is the amount of money they are putting in, shouldn't it be double?

    What if someone puts in $200.95?

  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! simple ATM

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

    It appears that you're skipping the last index in the array.

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

    Default Re: help! simple ATM

    yes, you are right. It should be double.

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

    Default Re: help! simple ATM

    I don't understand what you mean with, that I have skipped the last index of the array?
    Could you explain more? I don't really know hao to solve the program.
    Thanks for your attention.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,629
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: help! simple ATM

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

    You are starting at 0, which is the first index. You are ending at 1 index before the index before the last. Why is this happening? Because of your use of the < sign and then the length-1 call.

    You have to remember that the length of the array is 1 value greater than the last index. There are two ways to write a conditional statement that goes through every spot in the array. The first is to go while you are less than the length:
    for(int i = 0; i < transactions.length; i++)
    The second is to go while you are less than or equal to the length of the array, minus 1:
    for(int i = 0; i <= transactions.length-1; i++)

    You combined the two ways of doing it, resulting in you ignoring the last index.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. The Following 2 Users Say Thank You to aussiemcgr For This Useful Post:

    javapenguin (December 8th, 2010), JavaPF (December 8th, 2010)

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

    Default Re: help! simple ATM

    it seems strange. I think the array only save the 10 transactions. But what about the balance?
    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 program 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?
    Can someone help me to solve this problem. I want to save all the balance from the beginning and only show the 10 latest
    transactions in the program. Is this possible?

Similar Threads

  1. [SOLVED] Should be simple.
    By Time in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 22nd, 2010, 02:23 AM
  2. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  3. simple program
    By Memb in forum Paid Java Projects
    Replies: 0
    Last Post: March 17th, 2010, 01:47 PM
  4. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 02:47 AM
  5. Simple Form Help
    By dsen in forum Web Frameworks
    Replies: 1
    Last Post: September 8th, 2009, 11:32 PM