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] ;
}
}
23 replies to this topic
#1
Posted 05 January 2011 - 12:52 PM
|
|
|
#2
Posted 05 January 2011 - 02:28 PM
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]);
}
}
I think you just want:
for(int i = 0; i <= transactions.length-1; i++)
{
if(transactions[i] == 0){
System.out.print("");
} else {
System.out.println(transactions[i]);
}
}
#3
Posted 06 January 2011 - 05:11 AM
Sorry Wim DC I tryed your code snippet but i it doesn't work, it still make the same
ithe program still prints 9 latest transactions and not 10.
I need that the program prints 10 tlatest transactions.
Please help me again.
How do I get this program to print out 10 latest transations nstead of 9?
ithe program still prints 9 latest transactions and not 10.
I need that the program prints 10 tlatest transactions.
Please help me again.
How do I get this program to print out 10 latest transations nstead of 9?
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
{
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] ;
}
}
#4
Posted 06 January 2011 - 07:11 AM
It could be because your findNr will never return the last position, even if it's empty. try chaning
into
for(int i = 0; i < transactions.length-1; i++)
into
for(int i = 0; i < transactions.length; i++)in findNr()
#5
Posted 06 January 2011 - 08:25 AM
You should check your for loop conditions. If you set i < transactions.length - 1, meaning i < (10 - 1), so i < 9 which is until 8 and the loop will not reach the last element.
#6
Posted 06 January 2011 - 09:19 AM
wim DC said:
It could be because your findNr will never return the last position, even if it's empty. try chaning
into
for(int i = 0; i < transactions.length-1; i++)
into
for(int i = 0; i < transactions.length; i++)in findNr()
Thank you very much WimDC!!!
#7
Posted 06 January 2011 - 10:14 AM
wim DC said:
It could be because your findNr will never return the last position, even if it's empty. try chaning
into
for(int i = 0; i < transactions.length-1; i++)
into
for(int i = 0; i < transactions.length; i++)in findNr()
No wimDC.
It doesnt work when i the program will show the 11:th transaction.
It gives me:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Atm.makeTransactions(Atm.java:150)
at Atm.main(Atm.java:49)
Whats wrong?
#8
Posted 06 January 2011 - 10:20 AM
That may be because your moveTrans is wrong, you should set the last number in the array to 0 in there.
#9
Posted 06 January 2011 - 10:29 AM
how do you mean??
sorry for my bad understanding....
sorry for my bad understanding....
#10
Posted 06 January 2011 - 10:32 AM
array: [10,11,12,1,2,3,4,5,54,70]
>>>MoveTrans<<<
array: [11,12,1,2,3,4,5,54,70,0]
You don't put the 0 in the end.
>>>MoveTrans<<<
array: [11,12,1,2,3,4,5,54,70,0]
You don't put the 0 in the end.
#11
Posted 06 January 2011 - 10:37 AM
hmmmm.....
Its a little difficult for me to get this explain..
How should i solve this in the code?
How can I put this 0 in the end?
Sorry for my bad understanding.
Its a little difficult for me to get this explain..
How should i solve this in the code?
How can I put this 0 in the end?
Sorry for my bad understanding.
#12
Posted 06 January 2011 - 10:39 AM
I am very sorry but if you don't know how to set the last number of the array to 0 then you really need to take a second look at whatever the teachers have given / told you.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









