commit a22e021fe9218df44c344c33d4210b4079330f96 Author: 平良貴教 Date: Tue Aug 25 10:50:39 2026 +0900 EM-OOPの解答例を追加します。 diff --git a/file/DepartmentData.csv b/file/DepartmentData.csv new file mode 100644 index 0000000..649839f --- /dev/null +++ b/file/DepartmentData.csv @@ -0,0 +1,6 @@ +10,役員 +11,人事・総務部 +12,経理部 +13,システム開発事業部 +14,教育事業部 +15,営業企画部 diff --git a/file/EmployeeData.csv b/file/EmployeeData.csv new file mode 100644 index 0000000..60789e5 --- /dev/null +++ b/file/EmployeeData.csv @@ -0,0 +1,10 @@ +10000,山田龍也,ヤマダタツヤ,yamada@hoge.jp,aaa,10 +10001,高橋準,タカハシジュン,jtaka@hoge.jp,bbb,10 +10002,麻枝史明,マエダフミアキ,fmak@hoge.jp,ccc,11 +10003,丸戸章介,マルトショウスケ,marusyo@hoge.jp,ddd,12 +10004,三宅武,ミヤケタケシ,miya@hoge.jp,eee,13 +10005,丸井悠,マルイハルカ,haru@hoge.jp,fff,13 +10006,涼元香織,スズモトカオリ,suzumoto@hoge.jp,ggg,13 +10007,葉山稔,ハヤマミノル,mhayama@hoge.jp,hhh,14 +10008,川上朱里,カワカミアカリ,kawakami@hoge.jp,iii,14 +10009,杉井肇,スギイハジメ,sugi@hoge.jp,jjj,15 diff --git a/src/com/rhizome/example/access/BaseAccess.java b/src/com/rhizome/example/access/BaseAccess.java new file mode 100644 index 0000000..f345ded --- /dev/null +++ b/src/com/rhizome/example/access/BaseAccess.java @@ -0,0 +1,119 @@ +package com.rhizome.example.access; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import com.rhizome.example.data.BaseData; + +public abstract class BaseAccess { + + /** 対象ファイル */ + File targetFile; + + /** + * 引数のファイルパスでファイルオブジェクトを生成して、 + * 対象ファイルフィールドを初期化します + * @param filePath ファイルパス(例:"file/aaa.csv" 相対パス指定) + */ + protected BaseAccess(String filePath) { + this.targetFile = new File(filePath); + } + + /** + * 対象ファイルの中を全件読み込んで、 + * Listの形式で返却します + * @return 読み込み結果リスト(要素(String)の内容は対象ファイルの1行ごとの文字列) + * @throws FileNotFoundException 対象ファイルが見つからない場合 + */ + protected List readData() throws FileNotFoundException { + List lineList = new ArrayList<>(); + try (Scanner scanner = new Scanner(this.targetFile)) { + while (scanner.hasNext()) { + String lineData = scanner.nextLine(); + lineList.add(lineData); + } + } + return lineList; + } + + /** + * 対象ファイルにBaseDataの中身のCSV表現を追記します + * @param data 追加データ + * @throws IOException 対象ファイルが開けない場合や書き込めない場合 + */ + protected void addData(BaseData data) throws IOException { + //ファイル追記処理 + try (FileWriter fw = new FileWriter(this.targetFile, true); + BufferedWriter bw = new BufferedWriter(fw)) { + bw.write(data.toCSV()); + bw.newLine(); + bw.flush(); + } + } + + /** + * 対象ファイルの内容を更新対象リストの内容に上書きします + * @param list 更新対象リスト + * @throws IOException 対象ファイルが開けない場合や書き込めない場合 + */ + protected void updateData(List list) throws IOException { + //ファイル上書き処理 + try (FileWriter fw = new FileWriter(this.targetFile, false); + BufferedWriter bw = new BufferedWriter(fw)) { + for (BaseData data : list) { + bw.write(data.toCSV()); + bw.newLine(); + } + bw.flush(); + + } + + } + + /** + * ファイルの情報を全件取得する + * 利用メソッド:readData() + * @return 検索結果 + * @throws FileNotFoundException + */ + public abstract List selectAll() throws FileNotFoundException; + + /** + * ファイルの主キーが指定されたkeyに合致する情報を取得する + * 利用メソッド:readData() + * @param key 検索対象のキー + * @return 検索結果 + * @throws FileNotFoundException + */ + public abstract List selectById(String key) throws FileNotFoundException; + + /** + * ファイルに指定データを追加する + * 利用メソッド:addData() + * @param data 追加対象データ + * @throws IOException + */ + public abstract void insert(BaseData data) throws IOException; + + /** + * ファイルの指定データと主キーが一致する行を更新する + * 利用メソッド:updateData() + * @param data 更新対象データ + * @throws IOException + */ + public abstract void update(BaseData data) throws IOException; + + /** + * ファイルから主キーが指定されたkeyに合致する行を削除する + * 利用メソッド:updateData() + * @param key 削除対象のキー + * @throws IOException + */ + public abstract void delete(String key) throws IOException; +} diff --git a/src/com/rhizome/example/access/EmployeeAccess.java b/src/com/rhizome/example/access/EmployeeAccess.java new file mode 100644 index 0000000..4c572ab --- /dev/null +++ b/src/com/rhizome/example/access/EmployeeAccess.java @@ -0,0 +1,102 @@ +package com.rhizome.example.access; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.rhizome.example.data.BaseData; +import com.rhizome.example.data.EmployeeData; + +public class EmployeeAccess extends BaseAccess { + + private static final String EMPDATA_FILEPATH = "file/EmployeeData.csv"; + + public EmployeeAccess() { + super(EMPDATA_FILEPATH); + } + + @Override + public List selectAll() throws FileNotFoundException { + //取得用リスト(CSV形式) + List csvList = readData(); + //返却用リスト + List datalist = new ArrayList<>(); + + for (String line : csvList) { + EmployeeData employee = new EmployeeData(); + String[] columns = line.split(","); + employee.setIdEmployee(columns[0]); + employee.setNmEmployee(columns[1]); + employee.setKnEmployee(columns[2]); + employee.setMailAddress(columns[3]); + employee.setPassword(columns[4]); + employee.setIdDepartment(columns[5]); + + datalist.add(employee); + } + + return datalist; + } + + @Override + public List selectById(String key) throws FileNotFoundException { + //全件データ + List allList = selectAll(); + //IDでの抽出データ + List selectList = new ArrayList<>(); + + for (BaseData employee : allList) { + if (employee.getPrimaryKeyValue().equals(key)) { + selectList.add(employee); + } + } + + return selectList; + } + + @Override + public void insert(BaseData data) throws IOException { + //CSVファイルに追記する + addData(data); + } + + @Override + public void update(BaseData updateData) throws IOException { + //更新後データリスト + List updatedList = new ArrayList<>(); + + //全件取得し、更新データを反映する + List allList = selectAll(); + for (BaseData employee : allList) { + if(employee.getPrimaryKeyValue().equals(updateData.getPrimaryKeyValue())) { + //指定されたデータとキーが一致するデータの場合、更新データを設定 + updatedList.add(updateData); + } else { + //上記以外は取得したデータをそのまま設定 + updatedList.add(employee); + } + } + //ファイル上書き処理 + updateData(updatedList); + + } + + @Override + public void delete(String key) throws IOException { + //全件から削除データを除いたリスト + List deletedList = new ArrayList<>(); + + //全件取得し、keyに該当するデータをリストから削除する + List allList = selectAll(); + + for (BaseData employee : allList) { + if(!employee.getPrimaryKeyValue().equals(key)) { + deletedList.add(employee); + } + } + //ファイル上書き処理 + updateData(deletedList); + } + +} diff --git a/src/com/rhizome/example/data/BaseData.java b/src/com/rhizome/example/data/BaseData.java new file mode 100644 index 0000000..3a5ac18 --- /dev/null +++ b/src/com/rhizome/example/data/BaseData.java @@ -0,0 +1,22 @@ +package com.rhizome.example.data; + +public abstract class BaseData { + /** + * インスタンスのフィールドの値をタブ区切りで連結して返却 + * @return タブ区切り文字列 + */ + public abstract String toString(); + + /** + * インスタンスのフィールドの値をカンマ区切りで連結して返却 + * @return CSV形式文字列 + */ + public abstract String toCSV(); + + /** + * ファイルの主キーの値を取得 + * @return 主キーの値 + */ + public abstract String getPrimaryKeyValue(); + +} diff --git a/src/com/rhizome/example/data/EmployeeData.java b/src/com/rhizome/example/data/EmployeeData.java new file mode 100644 index 0000000..a1dab6d --- /dev/null +++ b/src/com/rhizome/example/data/EmployeeData.java @@ -0,0 +1,123 @@ +package com.rhizome.example.data; + +public class EmployeeData extends BaseData { + /** 社員ID */ + private String idEmployee; + /** 社員名 */ + private String nmEmployee; + /** 社員名カナ */ + private String knEmployee; + /** メールアドレス */ + private String mailAddress; + /** パスワード */ + private String password; + /** 役職ID */ + private String idDepartment; + + /** + * @return 社員ID + */ + public String getIdEmployee() { + return idEmployee; + } + + /** + * @param idEmployee 社員ID + */ + public void setIdEmployee(String idEmployee) { + this.idEmployee = idEmployee; + } + + /** + * @return 社員名 + */ + public String getNmEmployee() { + return nmEmployee; + } + + /** + * @param nmEmployee 社員名 + */ + public void setNmEmployee(String nmEmployee) { + this.nmEmployee = nmEmployee; + } + + /** + * @return 社員名カナ + */ + public String getKnEmployee() { + return knEmployee; + } + + /** + * @param knEmployee 社員名カナ + */ + public void setKnEmployee(String knEmployee) { + this.knEmployee = knEmployee; + } + + /** + * @return メールアドレス + */ + public String getMailAddress() { + return mailAddress; + } + + /** + * @param mailAddress メールアドレス + */ + public void setMailAddress(String mailAddress) { + this.mailAddress = mailAddress; + } + + /** + * @return パスワード + */ + public String getPassword() { + return password; + } + + /** + * @param password パスワード + */ + public void setPassword(String password) { + this.password = password; + } + + /** + * @return 役職ID + */ + public String getIdDepartment() { + return idDepartment; + } + + /** + * @param idDepartment 役職ID + */ + public void setIdDepartment(String idDepartment) { + this.idDepartment = idDepartment; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return getIdEmployee() + "\t" + getNmEmployee() + "\t" + getKnEmployee() + "\t" + + getMailAddress() + "\t" + getPassword() + "\t" + getIdDepartment(); + + } + + @Override + public String toCSV() { + return getIdEmployee() + "," + getNmEmployee() + "," + getKnEmployee() + "," + + getMailAddress() + "," + getPassword() + "," + getIdDepartment(); + } + + @Override + public String getPrimaryKeyValue() { + return idEmployee; + + } + +} diff --git a/src/com/rhizome/example/main/Main.java b/src/com/rhizome/example/main/Main.java new file mode 100644 index 0000000..2f12876 --- /dev/null +++ b/src/com/rhizome/example/main/Main.java @@ -0,0 +1,226 @@ +package com.rhizome.example.main; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; +import java.util.Scanner; + +import com.rhizome.example.access.EmployeeAccess; +import com.rhizome.example.data.BaseData; +import com.rhizome.example.data.EmployeeData; + +public class Main { + static Scanner sc = new Scanner(System.in); + + /** + * 処理開始位置 + * + * @param args + */ + public static void main(String[] args) { + System.out.println("システムを開始します"); + while (true) { + try { + new Main().start(); + } catch (Exception e) { + System.out.println("予期せぬエラーが発生しました"); + e.printStackTrace(); + } + } + } + + /** + * 処理選択を行う + * @throws IOException + * + */ + public void start() throws IOException { + System.out.println(); + System.out.println("1:検索 2:登録 3:更新 4:削除 0:終了"); + String ope = sc.nextLine(); + + switch (ope) { + case "1": //検索 + empSelect(); + break; + case "2": //登録 + empInsert(); + break; + case "3": //更新 + empUpdate(); + break; + case "4": //削除 + empDelete(); + break; + case "0": //修了 + System.out.println("システムを終了します"); + System.exit(0); + default: + System.out.println("指定の番号を入力してください"); + break; + } + } + + /** + * 社員ファイルから情報を検索する + * + * @throws FileNotFoundException + */ + public void empSelect() throws FileNotFoundException { + EmployeeAccess ea = new EmployeeAccess(); + List list; + + System.out.println("1:全検索 2:条件検索 それ以外:戻る"); + String select = sc.nextLine(); + + switch (select) { + case "1": //全検索 + //ファイル全件取得処理 + list = ea.selectAll(); + break; + + case "2": //条件検索 + //IDの入力および入力チェック + String id = inputCheck("ID", true); + //idに一致する社員データ取得 + list = ea.selectById(id); + + if (list.size() == 0) { //取得結果が0件の場合 + System.out.println("入力された値で情報がみつかりませんでした"); + } + break; + + default: + return; + } + + //取得結果件数分、データをタブ区切りで出力する + for (BaseData employee : list) { + System.out.println(employee.toString()); + } + } + + /** + * 社員ファイルに情報を登録する + * + * @throws IOException + */ + public void empInsert() throws IOException { + //各項目の入力および入力チェック処理 + String id = inputCheck("社員ID", true); + String name = inputCheck("社員名", false); + String kana = inputCheck("フリガナ", false); + String mail = inputCheck("メールアドレス", false); + String pass = inputCheck("パスワード", false); + String depId = inputCheck("部署ID", true); + + //社員データインスタンスを生成し、入力値を設定 + EmployeeData employee = new EmployeeData(); + employee.setIdEmployee(id); + employee.setNmEmployee(name); + employee.setKnEmployee(kana); + employee.setMailAddress(mail); + employee.setPassword(pass); + employee.setIdDepartment(depId); + + EmployeeAccess ea = new EmployeeAccess(); + //ファイルデータ追加処理 + ea.insert(employee); + System.out.println("登録が完了しました"); + } + + /** + * 社員ファイルの情報を更新する + * + * @throws IOException + */ + public void empUpdate() throws IOException { + EmployeeAccess ea = new EmployeeAccess(); + + //IDの入力および入力チェック + String id = inputCheck("ID", true); + //IDの存在チェック + if (ea.selectById(id).size() == 0) { + System.out.println("入力された値で情報がみつかりませんでした"); + return; + } + + //各項目の入力および入力チェック処理 + String name = inputCheck("社員名", false); + String kana = inputCheck("フリガナ", false); + String mail = inputCheck("メールアドレス", false); + String pass = inputCheck("パスワード", false); + String depId = inputCheck("部署ID", true); + + //社員データインスタンスを生成し、入力値を設定 + EmployeeData employee = new EmployeeData(); + employee.setIdEmployee(id); + employee.setNmEmployee(name); + employee.setKnEmployee(kana); + employee.setMailAddress(mail); + employee.setPassword(pass); + employee.setIdDepartment(depId); + + //ファイルデータ更新処理 + ea.update(employee); + System.out.println("更新が完了しました"); + } + + /** + * 社員ファイルの情報を削除する + * + * @throws IOException + */ + public void empDelete() throws IOException { + EmployeeAccess ea = new EmployeeAccess(); + + //IDの入力および入力チェック + String id = inputCheck("ID", true); + //IDの存在チェック + if (ea.selectById(id).size() == 0) { + System.out.println("入力された値で情報がみつかりませんでした"); + return; + } + System.out.println("指定されたIDの情報を削除します? Y/y"); + System.out.println("(Y/y以外の入力であればキャンセル)"); + String yY = sc.nextLine(); + if (!"y".equals(yY) && !"Y".equals(yY)) { + System.out.println("削除をキャンセルしました"); + return; + } + //ファイルデータ削除処理 + ea.delete(id); + System.out.println("削除が完了しました"); + } + + /** + * 入力値を判定し、返却する + * + * @param titleName 入力項目名 + * @param flg 数値チェック可否フラグ + * @return チェック済み入力値 + */ + public static String inputCheck(String titleName, boolean flg) { + String str = null; + while (true) { + System.out.println(titleName + "を入力してください"); + str = sc.nextLine(); + if (str.trim().length() == 0) { + System.out.println(titleName + "は入力必須です。"); + continue; + } + if (flg) { + if (str.trim().length() > 0) { + try { + Integer.parseInt(str); + } catch (NumberFormatException e) { + System.out.println(titleName + "は数値入力です"); + continue; + } + } + } + break; + } + return str; + } +}