diff --git a/em-oop/departments.csv b/em-oop/departments.csv new file mode 100644 index 0000000..a32416b --- /dev/null +++ b/em-oop/departments.csv @@ -0,0 +1,6 @@ +10,役員 +11,人事・総務部 +12,経理部 +13,システム開発事業部 +14,教育事業部 +15,営業企画部 \ No newline at end of file diff --git a/em-oop/employees.csv b/em-oop/employees.csv new file mode 100644 index 0000000..1dbd9de --- /dev/null +++ b/em-oop/employees.csv @@ -0,0 +1,5 @@ +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 \ No newline at end of file diff --git a/em-oop/src/Sub.java b/em-oop/src/Sub.java new file mode 100644 index 0000000..c515ad3 --- /dev/null +++ b/em-oop/src/Sub.java @@ -0,0 +1,165 @@ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + +public class Sub { + + public static void main(String[] args) { + final String employeesCSVPath = "employees.csv"; + final String departmentsCSVPath = "departments.csv"; + + ArrayList employees = new ArrayList(); + ArrayList departments = new ArrayList(); + + //社員CSVの読み込み + Path pathEmp = Path.of(employeesCSVPath); + ArrayList content = new ArrayList(); + try { + // content = Files.readString(path); + content = (ArrayList) (Files.readAllLines(pathEmp)); + } catch (IOException e) { + // TODO 自動 生成された catch ブロック + e.printStackTrace(); + } + content.stream().forEach(data -> { + + ArrayList content2 = new ArrayList(Arrays.asList(data.split(","))); + employees.add( + new EmployeeData(content2.get(0), content2.get(1), content2.get(2), content2.get(3), + content2.get(4), content2.get(5))); + }); + + //部門CSVの読み込み + content.clear();//使いまわしたいから + + Path pathDept = Path.of(departmentsCSVPath); + + try { + // content = Files.readString(path); + content = (ArrayList) (Files.readAllLines(pathDept)); + } catch (IOException e) { + // TODO 自動 生成された catch ブロック + e.printStackTrace(); + } + content.stream().forEach(data -> { + + ArrayList content2 = new ArrayList(Arrays.asList(data.split(","))); + employees.add( + new DepartmentData(content2.get(0), content2.get(1))); + }); + + ArrayList allData = new ArrayList(); + allData.addAll(employees); + allData.addAll(departments); + + //intを配列にしたらエラーが出なかった 理由不明 + int employeeCount = 0; + int deptCount = 0; + int[] counter = { 0, 0 }; + + allData.stream().forEach(data -> { + System.out.println(data); //一覧表示 + if (data instanceof EmployeeData) { + counter[0]++; + } else if (data instanceof DepartmentData) { + counter[1]++; + } + + }); + + System.out.println("社員データ:" + counter[0] + "件\n部署データ:" + counter[1] + "件"); + System.out.println(); + + //検索 + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("検索したい項目を入力してください\n社員ID:1 部署名:2 終了:0"); + System.out.print(">>>>"); + String str = scanner.nextLine().strip(); + switch (str) { + case "1": + EmployeeSeach(allData); + break; + case "2": + DepartmentSearch(allData); + break; + case "0": + System.out.println("システムを終了します"); + return; + default: + break; + } + + } + } + + private static void DepartmentSearch(ArrayList allData) { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("検索したい部署名を入力してください(戻る場合は0)"); + System.out.print(">>>>"); + String str = scanner.nextLine().strip(); + String searchDeptId = ""; + switch (str) { + case "0": + System.out.println("検索一覧に戻ります\n"); + return; + default: + for (BaseData data : allData) { + if (data instanceof DepartmentData) { + if (((DepartmentData) data).getNmDepartment().equals(str)) { + searchDeptId = ((DepartmentData) data).getIdDepartment(); + } + } + } + if (searchDeptId.equals("")) { + System.out.println("該当する部署が見つかりませんでした\n"); + continue; + } + for (BaseData data : allData) { + if (data instanceof EmployeeData) { + if (((EmployeeData) data).getIdDepartment().equals(searchDeptId)) { + System.out.println( + ((EmployeeData) data).getNmEmployee() + "\t" + + ((EmployeeData) data).getMailAddress()); + } + } + } + System.out.println(); + break; + } + } + } + + private static void EmployeeSeach(ArrayList allData) { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("検索したい社員IDを入力してください(戻る場合は0)"); + System.out.print(">>>>"); + String str = scanner.nextLine().strip(); + boolean matchFlag = false; + switch (str) { + case "0": + System.out.println("検索一覧に戻ります\n"); + return; + default: + for (BaseData e : allData) { + if (e.getPrimaryKeyValue().equals(str)) { + System.out.println(e); + matchFlag = true; + break; + } + } + if (!matchFlag) { + System.out.println("該当する社員が見つかりませんでした"); + } + System.out.println(); + break; + + } + } + } +}