arraylist - Java BufferedReader Switch case same line stored in array -


i'm trying loop through file read "accounts" file arraylist called accounts. file:

c 1111 1234 703.92 x 2222 1234 100.00 s 3333 1234 200.08 

i know it's looping next line since print id , goes 1111 2222 3333, @ bottom when print account's tostring, they're same 1 (the "s" line in file). why happening?

bank.main:

private static arraylist<account> accounts = new arraylist<account>(); public static void main(string[] args) {     //if incorrect # or bankfile can't opened, print system error     //usage: java bank bankfile [batchfile]      if(args.length > 2 || args.length == 0){         system.err.println("usage: java bank bankfile [batchfile]");     }     else{         if(args.length == 2){ //batch mode             processaccounts(args[0]);             //bankbatch.processbatch(args);             close(args[0]);         }          else if(args.length == 1){ // gui mode          }     }  } 

bank.processaccounts:

private static void processaccounts(string filepath){     bufferedreader accountreader;     file accountfile = new file(filepath);     if(accountfile.isdirectory()){         system.err.println("usage: java bank bankfile [batchfile]");         system.exit(1);     }      if(accountfile.exists()){         try{             accountreader = new bufferedreader(new filereader(accountfile));              string line = accountreader.readline();             while(line != null && line != ""){//in case there empty lines                 string[] s = line.split(" "); // parts of account line: type, id, pin, balance                 if(s.length != 4) break; //incorrect format, shouldn't happen                 int id = integer.parseint(s[1]);                 system.out.println(line);                 int pin = integer.parseint(s[2]);                 double balance = double.parsedouble(s[3]);                  switch(s[0]){ //account type                     case "x":                          system.out.println("case x");                         accounts.add(new checkingaccount(id, pin, balance));                         break;                      case "c":                         system.out.println("case c");                         accounts.add(new cdaccount(id, pin, balance));                         break;                      case "s":                         system.out.println("case s");                         accounts.add(new savingsaccount(id, pin, balance));                         break;                 }                 line = accountreader.readline();             }             system.out.println(accounts.get(0).tostring());             system.out.println(accounts.get(1).tostring());             system.out.println(accounts.get(2).tostring());             accountreader.close();         }         catch(filenotfoundexception e){             //this shouldn't happen since check see if exists         }         catch (ioexception e) {         }      } } 

account.java:

public abstract class account { public account(int id, int pin, double bal){     account.balance = bal;     account.pin = pin;     account.id = id; }  public string tostring(){     return (accttype + " " + id + " " + pin + " " + balance); } 

example of extending account, accounts similar:

public class checkingaccount extends account {  public checkingaccount(int id, int pin, double bal){     super(id, pin, bal);     setaccttype('x');     setmin(50); }   /**  * apply montly interest  * checkings accounts have no interest, penalties having less minimum  */ @override public double applyinterest() { //only penalty checking account     if (getbalance() > 5){         withdraw(5);         return -5;     }     else{         double change =  math.round((getbalance() * 0.10) * 100.0) / 100.0; //10% of balance, round 2 decimal places         withdraw(change);         return -change;     } } } 

this output i'm getting:

c 1111 1234 703.92 case c x 2222 1234 100.00 case x s 3333 1234 200.08 case s s 3333 1234 200.08 s 3333 1234 200.08 s 3333 1234 200.08 ========= final bank data ==========  acount type   account   balance    -----------   -------   -------  saving      3333    200.08  saving      3333    200.08  saving      3333    200.08  ==================================== 

looking @ account.java, looks may have made variables in there static (but i'm not sure because declarations missing snippet).

meaning, every time construct account, static variables set new ones. therefore, every time access variables in every object, same.

if of these assumptions i've made true, have remove static keyword account variable declarations , change accessors account.something this.something.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -