From 58d5a77873e39241b4846dde93bfe1a6462933d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=88=B4=E6=9C=A8=E3=80=80=E7=9A=93=E4=BB=8B?= <26-k-suzuki-avnz@it-college.co.jp> Date: Tue, 20 Oct 2026 16:28:39 +0900 Subject: [PATCH] =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E5=95=8F=E9=A1=8C=E3=81=BE?= =?UTF-8?q?=E3=81=A7=E5=AE=9F=E8=A3=85=E5=AE=8C=E4=BA=86=E6=B8=88=E3=81=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file/DepartmentData.csv | 6 + file/EmployeeData.csv | 6 + src/main/java/BaseData.java | 8 + src/main/java/DepartmentData.java | 49 ++++ src/main/java/EmployeeData.java | 33 ++- src/main/java/Main.java | 125 +++++---- src/main/java/MainNew.java | 280 +++++++++++++++++++ 実行例(追加問題含め)/file/EmployeeData.csv | 4 +- 8 files changed, 450 insertions(+), 61 deletions(-) create mode 100644 file/DepartmentData.csv create mode 100644 file/EmployeeData.csv create mode 100644 src/main/java/BaseData.java create mode 100644 src/main/java/DepartmentData.java create mode 100644 src/main/java/MainNew.java diff --git a/file/DepartmentData.csv b/file/DepartmentData.csv new file mode 100644 index 0000000..f2986b3 --- /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..0289f59 --- /dev/null +++ b/file/EmployeeData.csv @@ -0,0 +1,6 @@ +10000,山田龍也,ヤマダタツヤ,yamada@hoge.jp,aaa,10 +10001,高橋準,タカハシジュン,jtaka@hoge.jp,bbb,10 +10002,麻枝史明,マエダフミアキ,fmak@hoge.jp,ccc,11 +1,1,1,1,1,1 +3,2,2,2,2,2 +4,4,4,4,4,4 diff --git a/src/main/java/BaseData.java b/src/main/java/BaseData.java new file mode 100644 index 0000000..278d58a --- /dev/null +++ b/src/main/java/BaseData.java @@ -0,0 +1,8 @@ + +public abstract class BaseData { + + public abstract String toString(); + public abstract String toCSV(); + public abstract String getPrimaryKeyValue(); + +} diff --git a/src/main/java/DepartmentData.java b/src/main/java/DepartmentData.java new file mode 100644 index 0000000..c3a7943 --- /dev/null +++ b/src/main/java/DepartmentData.java @@ -0,0 +1,49 @@ + +public class DepartmentData extends BaseData { + + private String idDepartment; + private String nmDepartment; + + public DepartmentData() { + + } + + public DepartmentData(String idDepartment,String nmDepartment) { + this.setIdDepartment(idDepartment); + this.setNmDepartment(nmDepartment); + } + + public String getIdDepartment() { + return idDepartment; + } + + public void setIdDepartment(String idDepartment) { + this.idDepartment = idDepartment; + } + + public String getNmDepartment() { + return nmDepartment; + } + + public void setNmDepartment(String nmDepartment) { + this.nmDepartment = nmDepartment; + } + + @Override + public String toString() { + return this.getIdDepartment()+"\t" + +this.getNmDepartment(); + } + + @Override + public String toCSV() { + return this.getIdDepartment()+"," + +this.getNmDepartment(); + } + + @Override + public String getPrimaryKeyValue() { + return this.getIdDepartment(); + } + +} diff --git a/src/main/java/EmployeeData.java b/src/main/java/EmployeeData.java index 33ca983..4f168a2 100644 --- a/src/main/java/EmployeeData.java +++ b/src/main/java/EmployeeData.java @@ -1,5 +1,5 @@ -public class EmployeeData { +public class EmployeeData extends BaseData { static final String COMPANY_NAME = "Rhizome"; private static int empCount = 0; @@ -93,18 +93,31 @@ public class EmployeeData { @Override public String toString() { - return this.getIdEmployee()+"\t" - +this.getNmEmployee()+"\t" - +this.getKnEmployee()+"\t" - +this.getMailAddress()+"\t" - +this.getPassword()+"\t" - +this.getIdDepartment(); - + +this.getNmEmployee()+"\t" + +this.getKnEmployee()+"\t" + +this.getMailAddress()+"\t" + +this.getPassword()+"\t" + +this.getIdDepartment(); } - public boolean isMatchId(String id) { - return id.equals(this.getIdEmployee()) ; +// public boolean isMatchId(String id) { +// return id.equals(this.getIdEmployee()) ; +// } + + @Override + public String toCSV() { + return this.getIdEmployee()+"," + +this.getNmEmployee()+"," + +this.getKnEmployee()+"," + +this.getMailAddress()+"," + +this.getPassword()+"," + +this.getIdDepartment(); + } + + @Override + public String getPrimaryKeyValue() { + return this.getIdEmployee(); } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 66853c9..5ec3187 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -7,62 +7,91 @@ public class Main { //====================設定==================== - EmployeeData[] employees = new EmployeeData[5]; + BaseData[] employees = new BaseData[5]; -// EmployeeData employee0 = new EmployeeData(); -// employee0.setIdEmployee(""); // 社員IDが空文字のとき -// employee0.setNmEmployee(""); // 社員名が空文字のとき - - EmployeeData employee1 = new EmployeeData("10000", "山田龍也", "ヤマダタツヤ", "yamada@hoge.jp", "aaa", "10"); -// EmployeeData employee1 = new EmployeeData(); -// employee1.setIdEmployee("10000"); -// employee1.setNmEmployee("山田龍也"); -// employee1.setKnEmployee("ヤマダタツヤ"); -// employee1.setMailAddress("yamada@hoge.jp"); -// employee1.setPassword("aaa"); -// employee1.setIdDepartment("10"); + BaseData employee1 = new EmployeeData("10000", "山田龍也", "ヤマダタツヤ", "yamada@hoge.jp", "aaa", "10"); employees[1-1] = employee1; - - EmployeeData employee2 = new EmployeeData(); - employee2.setIdEmployee("10001"); - employee2.setNmEmployee("高橋準"); - employee2.setKnEmployee("タカハシジュン"); - employee2.setMailAddress("jtaka@hoge.jp"); - employee2.setPassword("bbb"); - employee2.setIdDepartment("10"); + BaseData employee2 = new EmployeeData("10001", "高橋準", "タカハシジュン", "jtaka@hoge.jp", "bbb", "10"); employees[2-1] = employee2; - - EmployeeData employee3 = new EmployeeData(); - employee3.setIdEmployee("10002"); - employee3.setNmEmployee("麻枝史明"); - employee3.setKnEmployee("マエダフミアキ"); - employee3.setMailAddress("fmak@hoge.jp"); - employee3.setPassword("ccc"); - employee3.setIdDepartment("11"); + BaseData employee3 = new EmployeeData("10002", "麻枝史明", "マエダフミアキ", "fmak@hoge.jp", "ccc", "11"); employees[3-1] = employee3; - - EmployeeData employee4 = new EmployeeData(); - employee4.setIdEmployee("10003"); - employee4.setNmEmployee("丸戸章介"); - employee4.setKnEmployee("マルトショウスケ"); - employee4.setMailAddress("marusyo@hoge.jp"); - employee4.setPassword("ddd"); - employee4.setIdDepartment("12"); + BaseData employee4 = new EmployeeData("10003", "丸戸章介", "マルトショウスケ", "marusyo@hoge.jp", "ddd", "12"); employees[4-1] = employee4; - - EmployeeData employee5 = new EmployeeData(); - employee5.setIdEmployee("10004"); - employee5.setNmEmployee("三宅武"); - employee5.setKnEmployee("ミヤケタケシ"); - employee5.setMailAddress("miya@hoge.jp"); - employee5.setPassword("eee"); - employee5.setIdDepartment("13"); + BaseData employee5 = new EmployeeData("10004", "三宅武", "ミヤケタケシ", "miya@hoge.jp", "eee", "13"); employees[5-1] = employee5; + BaseData[] allData = new BaseData[11]; + + allData[0] = employees[0]; + allData[1] = employees[1]; + allData[2] = employees[2]; + allData[3] = employees[3]; + allData[4] = employees[4]; + + DepartmentData department1 = new DepartmentData(); + department1.setIdDepartment("10"); + department1.setNmDepartment("役員"); + allData[5] = department1; + DepartmentData department2 = new DepartmentData(); + department2.setIdDepartment("11"); + department2.setNmDepartment("人事"); + allData[6] = department2; + DepartmentData department3 = new DepartmentData(); + department3.setIdDepartment("12"); + department3.setNmDepartment("経理部"); + allData[7] = department3; + DepartmentData department4 = new DepartmentData(); + department4.setIdDepartment("13"); + department4.setNmDepartment("システム開発事業部"); + allData[8] = department4; + DepartmentData department5 = new DepartmentData(); + department5.setIdDepartment("14"); + department5.setNmDepartment("教育事業部"); + allData[9] = department5; + DepartmentData department6 = new DepartmentData(); + department6.setIdDepartment("15"); + department6.setNmDepartment("営業企画部"); + allData[10] = department6; + + // 段階7+8 + int nEmployeeData = 0; + int nDepartmentData = 0; + for ( BaseData data : allData ) { +// System.out.println(data); + if ( data instanceof EmployeeData ) { + System.out.println(((EmployeeData)data).getMailAddress()); + nEmployeeData++; + continue; + } + if ( data instanceof DepartmentData ) { + nDepartmentData++; + continue; + } + } + System.out.println("社員データ:"+nEmployeeData+"件"); + System.out.println("部署データ:"+nDepartmentData+"件"); + + // 段階9 + String ID = ""; + for ( BaseData data : allData ) { + if ( data instanceof DepartmentData ) { + if ( ((DepartmentData)data).getNmDepartment().equals("システム開発事業部") ) { + ID = data.getPrimaryKeyValue(); + } + } + } + for ( BaseData data : allData ) { + if ( data instanceof EmployeeData ) { + if ( ((EmployeeData)data).getIdDepartment().equals(ID) ) { + System.out.println(((EmployeeData)data).getNmEmployee()+" "+((EmployeeData)data).getMailAddress()); + } + } + } + //====================表示==================== System.out.println(EmployeeData.COMPANY_NAME); - for ( EmployeeData employee : employees ) { + for ( BaseData employee : employees ) { System.out.println(employee); } System.out.println("現在の社員データ件数:"+EmployeeData.getEmpCount()+"件"); @@ -81,8 +110,8 @@ public class Main { } boolean matchId = false; - for ( EmployeeData employee : employees ) { - if ( employee.isMatchId(id) ) { + for ( BaseData employee : employees ) { + if ( employee.getPrimaryKeyValue().equals(id) ) { matchId = true; System.out.println(employee);; } diff --git a/src/main/java/MainNew.java b/src/main/java/MainNew.java new file mode 100644 index 0000000..048f367 --- /dev/null +++ b/src/main/java/MainNew.java @@ -0,0 +1,280 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Scanner; + +public class MainNew { + + public static void main(String[] args) { + // TODO 自動生成されたメソッド・スタブ + + + Scanner sc = new Scanner(System.in); + while (true) { + + // csvファイルからデータを読み込む + ArrayList allData = readCSV(); + + // 会社情報表示 + //System.out.println(EmployeeData.COMPANY_NAME); + + + //====================管理機能==================== + + System.out.println("1:検索 2:登録 3:更新 4:削除 0:終了"); + // 操作項目入力 + String index = sc.nextLine(); + + // 入力1=検索 + if (index.equals("1")) { + System.out.println("1:全検索 2:条件検索 それ以外:戻る"); + String indexSearch = sc.nextLine(); + if (indexSearch.equals("1")) { + // 全検索 + displayEmployee(allData); + continue; + } else if (indexSearch.equals("2")) { + // 条件検索 + searchEmployee(allData,sc); + continue; + } else { + continue; + } + } + + // 入力2=登録 + if (index.equals("2")) { + registerEmployee(allData,sc); + } + + // 入力3=更新 + if (index.equals("3")) { + updateEmployee(allData,sc); + } + + // 入力4=削除 + if (index.equals("4")) { + removeEmployee(allData,sc); + } + + // 入力0=終了 + if ( index.equals("0") ) { + System.out.println("システムを終了します"); + break; + } + + } + + } + + + // csvファイルから情報を読み込む + public static ArrayList readCSV() { + + ArrayList allData = new ArrayList(); + try { + + // 社員情報を読み込む + File empFile = new File("/Users/鈴木 皓介/Documents/em-oop/file/EmployeeData.csv"); + String empline = ""; + BufferedReader empbr = new BufferedReader(new InputStreamReader(new FileInputStream(empFile),"UTF-8")); + while ((empline=empbr.readLine())!=null) { + String[] data = empline.split(",",0); + BaseData employee = new EmployeeData(data[0],data[1],data[2],data[3],data[4],data[5]); + allData.add(employee); + } + + // 部署情報を読み込む + File depFile = new File("/Users/鈴木 皓介/Documents/em-oop/file/DepartmentData.csv"); + String depline = ""; + BufferedReader depbr = new BufferedReader(new InputStreamReader(new FileInputStream(depFile),"UTF-8")); + while ((depline=depbr.readLine())!=null) { + String[] data = depline.split(",",0); + BaseData department = new DepartmentData(data[0],data[1]); + allData.add(department); + } + + } catch (IOException err) { + System.exit(0); + } + return allData; + + } + + + // csvファイルに社員データを書き込む + public static void writeEmployee(ArrayList allData) { + + try { + + // 社員情報を書き込む + File empFile = new File("/Users/鈴木 皓介/Documents/em-oop/file/EmployeeData.csv"); + FileWriter empfr = new FileWriter(empFile); + for ( BaseData data : allData ) { + if ( data instanceof EmployeeData ) { + empfr.append(data.toCSV()+"\n"); + } + } + empfr.close(); + + } catch (IOException err) { + System.exit(0); + } + + } + + + // 全検索(社員情報表示) + public static void displayEmployee(ArrayList allData) { + + for ( BaseData data : allData ) { + if ( data instanceof EmployeeData ) { + System.out.println(data); + } + } + System.out.println(); + + } + + + // 社員ID照合 + public static boolean matchIdEmployee(ArrayList allData,String id) { + + boolean isMatched = false; + for ( BaseData data : allData ) { + if ( data instanceof EmployeeData ) { + if ( data.getPrimaryKeyValue().equals(id) ) { + isMatched = true; + break; + } + } + } + return isMatched; + + } + + + // ID照合→該当BaseData配列インデックス取得 + public static int getIndexArray(ArrayList allData,String id) { + + int index = 0; + for ( BaseData data : allData ) { + if ( data instanceof EmployeeData ) { + if ( data.getPrimaryKeyValue().equals(id) ) { + break; + } + } + index++; + } + return index; + + } + + + // 条件検索 + public static void searchEmployee(ArrayList allData,Scanner sc) { + + System.out.println("検索したい社員IDを入力してください"); + String id = sc.nextLine(); + + boolean isMatched = matchIdEmployee(allData,id); + if (isMatched==false) { + System.out.println("入力されたIDに該当する社員が見つかりませんでした"); + } + if (isMatched==true) { + System.out.println(allData.get(getIndexArray(allData,id))); + } + System.out.println(); + + } + + + // 登録 + public static void registerEmployee(ArrayList allData,Scanner sc) { + + System.out.println("登録したい社員IDを入力してください"); + String id = sc.nextLine(); + + boolean isMatched = matchIdEmployee(allData,id); + if (isMatched==false) { + String[] newdata = new String[6]; + newdata[0] = id; + System.out.println("社員名を入力してください"); + newdata[1] = sc.nextLine(); + System.out.println("フリガナを入力してください"); + newdata[2] = sc.nextLine(); + System.out.println("メールアドレスを入力してください"); + newdata[3] = sc.nextLine(); + System.out.println("パスワードを入力してください"); + newdata[4] = sc.nextLine(); + System.out.println("部署IDを入力してください"); + newdata[5] = sc.nextLine(); + System.out.println("登録が完了しました"); + allData.add(new EmployeeData(newdata[0],newdata[1],newdata[2],newdata[3],newdata[4],newdata[5])); + writeEmployee(allData); + } + if (isMatched==true) { + System.out.println("入力されたIDは既に登録されています"); + } + + } + + + // 更新 + public static void updateEmployee(ArrayList allData,Scanner sc) { + + System.out.println("更新したい社員IDを入力してください"); + String id = sc.nextLine(); + + boolean isMatched = matchIdEmployee(allData,id); + if (isMatched==false) { + System.out.println("入力されたIDに該当する社員が見つかりませんでした"); + } + if (isMatched==true) { + BaseData data = allData.get(getIndexArray(allData,id)); + System.out.println("社員名を入力してください"); + ((EmployeeData)data).setNmEmployee(sc.nextLine()); + System.out.println("フリガナを入力してください"); + ((EmployeeData)data).setKnEmployee(sc.nextLine()); + System.out.println("メールアドレスを入力してください"); + ((EmployeeData)data).setMailAddress(sc.nextLine()); + System.out.println("パスワードを入力してください"); + ((EmployeeData)data).setPassword(sc.nextLine()); + System.out.println("部署IDを入力してください"); + ((EmployeeData)data).setIdDepartment(sc.nextLine()); + System.out.println("更新が完了しました"); + writeEmployee(allData); + } + + } + + + // 消去 + public static void removeEmployee(ArrayList allData,Scanner sc) { + + System.out.println("削除したい社員IDを入力してください"); + String id = sc.nextLine(); + + boolean isMatched = matchIdEmployee(allData,id); + if (isMatched==false) { + System.out.println("入力されたIDに該当する社員が見つかりませんでした"); + } + if (isMatched==true) { + System.out.println("指定されたIDに該当する社員の情報を削除しますか? Y/y"); + System.out.println("Y/y以外:キャンセル"); + String flag = sc.nextLine(); + if (flag.equals("Y")||flag.equals("y")) { + allData.remove(getIndexArray(allData,id)); + System.out.println("削除が完了しました"); + writeEmployee(allData); + } + } + + } + + +} diff --git a/実行例(追加問題含め)/file/EmployeeData.csv b/実行例(追加問題含め)/file/EmployeeData.csv index 60789e5..e75eec7 100644 --- a/実行例(追加問題含め)/file/EmployeeData.csv +++ b/実行例(追加問題含め)/file/EmployeeData.csv @@ -1,10 +1,8 @@ -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 +2,2,2,2,2,2