Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

help! simple ATM, problem with printing the last current transactions.
Started by galva, Dec 08 2010 04:51 AM
printing
9 replies to this topic
#1
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] ;
}
}
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] ;
}
}
#2
Posted 08 December 2010 - 09:36 AM
Your moveTrans method should be:
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-^_^:' />
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
#3
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
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
#4
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
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
#5
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?
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?
#6
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.
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.
#7
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?
#8
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.
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.
#9
Posted 09 December 2010 - 12:22 AM
What you want to do is place
In makeTransaction you change the balance too:
And because the balance should be up to date all the time now, it shouldn't be calculated in the showtransactions
So remove
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];
#10
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......
You really helped me out of this....
I appriciate your help. Now I have learnd a good lesson from you!
Thank you again......

Also tagged with one or more of these keywords: printing
![]() Printing a string after If statementStarted by Bitlynk, 15 Apr 2013 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
Keeping content together when printingStarted by hinkeltje, 21 Feb 2013 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
Force printing on A4 landscape formatStarted by hinkeltje, 21 Feb 2013 ![]() |
|
![]() |
||
Language Forums →
PHP →
how to shuffle contents fetched from database before printingStarted by alamin, 13 Feb 2013 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
[C++] Printing a whole text file in separate linesStarted by qyler, 01 Feb 2013 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download