Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

help! simple ATM, problem with printing the last current transactions.

printing

  • Please log in to reply
9 replies to this topic

#1 galva

galva

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 08 December 2010 - 04:51 AM

*/ I neep help with this simnple atm machine. I need to show the ten last transactions in order to if I enter the 11:th, 12:th 13:th, 14:th transaction the array should rewrite the old transactins but still have the balance of all transfers. It works good until the 10:th transaction but later there will be problem, the program won't rewrite the older transaction. The program should only print 10
transactions or the 10 current transaction and keep the balance status of all made transactions.
Please coild someone help me?, i am stuck!*/


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("How much will you 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("How 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("End program");
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. Balance ");
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("10 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] ;

}


}
  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 08 December 2010 - 09:36 AM

Your moveTrans method should be:
public static void moveTrans(int[] transactions) {
        for (int i = 0; i < transactions.length - 1; i++)
            transactions[[COLOR="red"][B][SIZE="4"]i[/SIZE][/B][/COLOR]] = transactions[i + 1];

        [COLOR="red"][B]transactions[transactions.length-1] = 0;[/B][/COLOR]

    }

Also, your showTransactions doesn't print the last transaction because the for-loop's condition is transactions.length -1^_^.gif' class='bbc_emoticon' alt=':c-^_^:' />
  • -1

#3 galva

galva

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 08 December 2010 - 10:00 AM

hello again.

I tryed to change the metod code with your code of the method moveTrans()
I wrote the saqme as you written:

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;

}

thanks for your help, 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? :


10 last transactions:

1
-10
10
10
10
10
10
9
-55

Balance: -5 kr
  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 08 December 2010 - 11:36 AM

What's wrong with the outcome?
1 -10 +10 +10 +10 +10 +10 +9 -55 = -5
Seems correct to me.
1+9 and 5 times 10 = 60
60 - 55 -10 = -5
  • 0

#5 galva

galva

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 08 December 2010 - 11:52 AM

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 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?
  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 08 December 2010 - 12:01 PM

So if you type in 10 times deposit 2 then balans is 20
If you type in 2 an 11th time, should it then show 22, or 20 because it only remembers the last 10 times?

noticed another
for (int i = 0; i < transactions.length -1; i++) {
in the showtransactions method, by the way.
  • 0

#7 galva

galva

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 08 December 2010 - 09:49 PM

So if you type in 10 times deposit 2 then balans is 20
If you type in 2 an 11th time, should it then show 22, or 20 because it only remembers the last 10 times?

noticed another
for (int i = 0; i < transactions.length -1; i++) {
in the showtransactions method, by the way.


What about if I make a method that remebers all the balance from the beginning?
What can you suggest that I would write in the code?
  • 0

#8 galva

galva

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 08 December 2010 - 10:17 PM

t 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.
  • 0

#9 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 09 December 2010 - 12:22 AM

What you want to do is place
int balance = 0;
Outside the main method, and just in the class as a field (and make it static cause you do everythign with static methods):
public class BankoMat{
  private static int balance = 0;

In makeTransaction you change the balance too:
public static void makeTransactions(int amount, int [] transactions)
{
  int position = findNr(transactions);
  if(position == -1)
  {
    moveTrans(transactions);
    position = findNr(transactions);
  }
  transactions[position] = amount;
  balance += amount;
}

And because the balance should be up to date all the time now, it shouldn't be calculated in the showtransactions
So remove
balance = balance + transactions[i];

  • 0

#10 galva

galva

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 09 December 2010 - 08:13 AM

Tanks wimDC.

You really helped me out of this....
I appriciate your help. Now I have learnd a good lesson from you!

Thank you again......:c-smile:
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download