csvファイルを読み込んで中に自分の名前があるか判別する機能です。
入出力の責務と値の保持という責務を分けたいと思ったので
とりあえず読み込む機能を別クラスに分けたのですが同じ動作になりませんでした。
どのようにしたら同じ動作になるでしょうか。
csvファイル
ai,0,2
ken,3,2
yu,1,0
ryo,4,4
変更前
java
1import java.io.BufferedWriter; 2import java.io.FileNotFoundException; 3import java.io.FileReader; 4import java.io.IOException; 5import java.nio.charset.StandardCharsets; 6import java.nio.file.Files; 7import java.nio.file.Paths; 8import java.nio.file.StandardOpenOption; 9import java.util.ArrayList; 10import java.util.List; 11 12import org.supercsv.io.CsvListReader; 13import org.supercsv.io.ICsvListReader; 14import org.supercsv.prefs.CsvPreference; 15 16/** 17 * じゃんけんの結果を記録する 18 * @author Gaku Sakurai 19 */ 20public class RecordJyankenResults { 21 22 /** csvファイルを1行ずつ読み込むリスト */ 23 private List<String> list = new ArrayList<String>(); 24 25 /** 入力された名前以外のリスト */ 26 private List<String> removedList = new ArrayList<String>(); 27 28 /** csvファイルの中に入力された名前があるか */ 29 private boolean existsTheName = false; 30 31 /** 自分の名前 */ 32 private String playerName; 33 34 /** 自分の勝利数の合計 */ 35 private int totalWin; 36 37 /** 自分の敗北数の合計 */ 38 private int totalLose; 39 40 /** 41 * コンストラクタ 42 * @param playerName 自分の名前 43 * @param playerWinCount 自分の勝ち数 44 * @param playerLoseCount 自分の負け数 45 */ 46 public RecordJyankenResults( String playerName, int playerWinCount, int playerLoseCount) { 47 this.setPlayerName( playerName); 48 this.setTotalWin( playerWinCount); 49 this.setTotalLose( playerLoseCount); 50 } 51 52 /** 53 * CSVファイルの読み込み 54 * @return 名前が存在するか 55 */ 56 public boolean readResultCsv() { 57 try (ICsvListReader reader = new CsvListReader( new FileReader( "./result/result.csv"), CsvPreference.STANDARD_PREFERENCE)) { 58 while ( (setList( reader.read())) != null) { 59 judgeWhetherNameExists(); 60 } 61 } 62 catch ( FileNotFoundException e) { 63 System.err.println( e); 64 } 65 catch ( IOException e) { 66 System.err.println( e); 67 } 68 return isExist(); 69 } 70 71 /** 72 * 同じプレイヤー名が保存されているかの判定 73 */ 74 public void judgeWhetherNameExists() { 75 76 if ( getList().get( 0).equals( getPlayerName())) { 77 setExist( true); 78 setTotalWin( Integer.parseInt( getList().get( 1)) + getTotalWin()); 79 setTotalLose( Integer.parseInt( getList().get( 2)) + getTotalLose()); 80 } 81 else { 82 getRemovedList().add( getList().get( 0) + ","); 83 getRemovedList().add( getList().get( 1) + ","); 84 getRemovedList().add( getList().get( 2) + "\n"); 85 } 86 } 87 88//以下書き込み用メソッドとgetterとsetter………
変更後
java
1public class RecordJyankenResults { 2 3 /** csvファイルを1行ずつ読み込むリスト */ 4 private List<String> list = new ArrayList<String>(); 5 6 /** 入力された名前以外のリスト */ 7 private List<String> removedList = new ArrayList<String>(); 8 9 /** csvファイルの中に入力された名前があるか */ 10 private boolean existsTheName = false; 11 12 /** 自分の名前 */ 13 private String playerName; 14 15 /** 自分の勝利数の合計 */ 16 private int totalWin; 17 18 /** 自分の敗北数の合計 */ 19 private int totalLose; 20 21 /** 22 * コンストラクタ 23 * @param playerName 自分の名前 24 * @param playerWinCount 自分の勝ち数 25 * @param playerLoseCount 自分の負け数 26 */ 27 public RecordJyankenResults( String playerName, int playerWinCount, int playerLoseCount) { 28 this.setPlayerName( playerName); 29 this.setTotalWin( playerWinCount); 30 this.setTotalLose( playerLoseCount); 31 } 32 33 /** 34 * CSVファイルの読み込み 35 * @return 名前が存在するか 36 */ 37 public boolean readResultCsv() { 38 39 ReadResultCsv2 readResultCsv2 = new ReadResultCsv2(); 40 while (setList(readResultCsv2.readResultCsv2(getList())) != null ) { 41 judgeWhetherNameExists(); 42 } 43 44 return isExist(); 45 } 46 47 /** 48 * 同じプレイヤー名が保存されているかの判定 49 */ 50 public void judgeWhetherNameExists() { 51 52 if ( getList().get( 0).equals( getPlayerName())) { 53 setExist( true); 54 setTotalWin( Integer.parseInt( getList().get( 1)) + getTotalWin()); 55 setTotalLose( Integer.parseInt( getList().get( 2)) + getTotalLose()); 56 } 57 else { 58 getRemovedList().add( getList().get( 0) + ","); 59 getRemovedList().add( getList().get( 1) + ","); 60 getRemovedList().add( getList().get( 2) + "\n"); 61 } 62//以下書き込み用メソッドとgetterとsetter……… 63
java
1public class ReadResultCsv2 { 2 3 public List<String> readResultCsv2(List<String> list) { 4 try (ICsvListReader reader = new CsvListReader( new FileReader( "./result/result.csv"), CsvPreference.STANDARD_PREFERENCE)) { 5 while ( (list = reader.read()) != null) { 6 } 7 } 8 catch ( FileNotFoundException e) { 9 System.err.println( e); 10 } 11 catch ( IOException e) { 12 System.err.println( e); 13 } 14 return list; 15 } 16} 17