Java ATM arrays error -


i'm trying make atm can make deposit, withdrawal , show balance, problem comes when i'm trying make 11th transaction (the size of transaction records 10).

here how program should work:

earlier transactions:  =====================  1   2  3  4  5  6  7  8  9  10  =======================  balance: 55   kr  earlier transactions:  =====================  2  3  4  5  6  7  8  9  10  11  =======================  balance: 65   kr 

i have use methods , haven't translated program english.

import java.util.scanner;  public class bankomat  {     public static void main(string[] args)     {         scanner in = new scanner(system.in);          // declarer variables         int[] trans = new int[10];           int amount = 0;         int balance = 0;         int sum = 0;         int thechoice = 1;          while(thechoice != 4)         {             thechoice= menu();             switch(thechoice)             {             case 1:                 system.out.println("\ndu valde \"deposit\"");                  system.out.print("\nstate value want take in: ");                 sum = in.nextint();                  if(sum == 0)                 {                     system.out.print("\nyou have given wrong value.\n");                 }                 else                 {                     amount = (int) + sum;                     maketransactions(trans,amount);                 }                     break;              case 2:                 system.out.println("\ndu valde \"withdrawal\"");                  system.out.print("\nstate value want take in: ");                 sum = in.nextint();                  if(sum == 0)                 {                     system.out.print("\ndu har angett ett felaktigt belopp.\n");                 }                 else                 {                     amount = (int) - sum;                     maketransactions(trans,amount);                 }                     break;              case 3:                 system.out.println("\ndu valde \"balance\"");                 showtransactions(trans,balance);                 break;             case 4:                 system.out.println("\ndu valde \"quit\"");                 break;             }         }     }      /**      * menu      * @return val  skickar tillbaka input värdet      */     public static int menu()     {         scanner in = new scanner(system.in);          int choice = 0;          // den här delen kommer att skriva ut menu         system.out.println("1. deposit");         system.out.println("2. withdrawal");         system.out.println("3. balance");                            system.out.println("4. quit");                                            system.out.print("your choice: ");          choice = in.nextint();          return choice;     }      /**      *  method sum ten latest transaction , show balance       * @param trans   array saves latest transactions       * @param balance int sums values      */     public static void showtransactions(int[] trans, int balance )     {         system.out.println();         system.out.println("tidigare transaktioner: ");         system.out.println("-------------------------------------------\n");          for(int = 0; < trans.length; i++)         {             if(trans[i] == 0)             {                 system.out.print("");             }              else             {                 system.out.print(trans[i] + "\n");                 balance = balance + trans[i];             }         }         system.out.println("-------------------------------------------\n");         system.out.println("saldo: " + balance + "kr" + "\n" );     }      /**      * method saves latest transaction      * @param trans array saves latest transactions      * @param amount int saves latest transaction      */     public static void maketransactions(int[] trans, int amount)     {         int position = findnr(trans);         if(position == -1)         {             movetrans(trans);         }         else         {             trans[position] = amount;         }     }      /**      * metod empty position       * @param trans array saves latest transactions      * @return position       */     private static int findnr(int[] trans)      {         int position = -1;          for(int = 0; < trans.length; i++)         {             if (trans[i] == 0)             {                 position = i;                 break;             }         }         return position;     }      /**      * method move transaction       * @param trans array saves latest transactions      */     private static void movetrans(int[] trans)     {         for(int = 0; < trans.length; i++)         {             trans[0] = trans[i + 1];         }        } } 

edit:

exception in thread "main" java.lang.arrayindexoutofboundsexception: 3     @ stackoverflow.testing.bankomat.movetrans(bankomat.java:171)     @ stackoverflow.testing.bankomat.maketransactions(bankomat.java:135)     @ stackoverflow.testing.bankomat.main(bankomat.java:41) 

line 171:

private static void movetrans(int[] trans) {     for(int = 0; < trans.length; i++)     {         trans[0] = trans[i + 1]; // line 171     }    } 

updated code:

import java.util.scanner;  public class bankomat {     public static void main(string[] args)     {         scanner in = new scanner(system.in);          // declarer variables         int[] trans = new int[10];         int amount = 0;         int balance = 0;         int sum = 0;         int thechoice = 1;          while(thechoice != 4)         {             thechoice = menu();             switch(thechoice)             {             case 1:                 system.out.println("\ndu valde \"deposit\"");                 system.out.print("state value want take in: ");                 sum = in.nextint();                  if(sum == 0)                 {                     system.out.print("\nyou have given wrong value.\n");                 }                 else                 {                     amount = (int) + sum;                     maketransactions(trans, amount);                 }                     break;              case 2:                 system.out.println("\ndu valde \"withdrawal\"");                 system.out.print("\nstate value want take in: ");                 sum = in.nextint();                  if(sum == 0)                 {                     system.out.print("\ndu har angett ett felaktigt belopp.\n");                 }                 else                 {                     amount = (int) - sum;                     maketransactions(trans, amount);                 }                     break;              case 3:                 system.out.println("\ndu valde \"balance\"");                 showtransactions(trans,balance);                 break;             case 4:                 system.out.println("\ndu valde \"quit\"");                 break;             }         }     }      /**      * menu      * @return val  skickar tillbaka input värdet      */     public static int menu()     {         scanner in = new scanner(system.in);         int choice = 0;          // den här delen kommer att skriva ut menu         system.out.print("\n1. deposit\t");         system.out.print("2. withdrawal\t");         system.out.print("3. balance\t");                            system.out.println("4. quit");                                            system.out.print("your choice: ");          choice = in.nextint();         return choice;     }      /**      *  method sum ten latest transaction , show balance       * @param trans   array saves latest transactions       * @param balance int sums values      */     public static void showtransactions(int[] trans, int balance )     {         system.out.println();         system.out.println("tidigare transaktioner: ");         system.out.println("-------------------------------------------\n");          for(int = 0; < trans.length; i++)         {             if(trans[i] == 0)             {                 system.out.print("");             }              else             {                 system.out.print(trans[i] + "\n");                 balance = balance + trans[i];             }         }         system.out.println("-------------------------------------------\n");         system.out.println("saldo: " + balance + "kr" + "\n" );     }      /**      * method saves latest transaction      * @param trans array saves latest transactions      * @param amount int saves latest transaction      */     public static void maketransactions(int[] trans, int amount)     {         int position = findnr(trans);         if(position == -1)         {             movetrans(trans);             trans[trans.length - 1] = amount;         }         else         {             trans[position] = amount;         }     }      /**      * metod empty position       * @param trans array saves latest transactions      * @return position       */     private static int findnr(int[] trans)      {         int position = -1;          for(int = 0; < trans.length; i++)         {             if (trans[i] == 0)             {                 position = i;                 break;             }         }         return position;     }      /**      * method move transaction       * @param trans array saves latest transactions      */     private static void movetrans(int[] trans)     {         for(int = 0; < (trans.length - 1); i++)         {             trans[i] = trans[i + 1];         }     } } 

changes:

  • updated movetrans() correct logic
    • i < (trans.length - 1) , trans[i] = trans[i + 1];
  • updated maketrans() such inserts transaction @ right position when array full
    • trans[trans.length - 1] = amount;

input/output:

1. deposit  2. withdrawal   3. balance  4. quit choice: 1  du valde "deposit" state value want take in: 10  1. deposit  2. withdrawal   3. balance  4. quit choice: 1  du valde "deposit" state value want take in: 20  1. deposit  2. withdrawal   3. balance  4. quit choice: 1  du valde "deposit" state value want take in: 30  1. deposit  2. withdrawal   3. balance  4. quit choice: 3  du valde "balance"  tidigare transaktioner:  -------------------------------------------  10 20 30 -------------------------------------------  saldo: 60kr   1. deposit  2. withdrawal   3. balance  4. quit choice: 1  du valde "deposit" state value want take in: 40  1. deposit  2. withdrawal   3. balance  4. quit choice: 3  du valde "balance"  tidigare transaktioner:  -------------------------------------------  20 30 40 -------------------------------------------  saldo: 90kr   1. deposit  2. withdrawal   3. balance  4. quit choice: 4  du valde "quit"  

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -