2 Commits
ishikawa ... fu

Author SHA1 Message Date
507bb0797a 2日目 2026-10-23 16:34:44 +09:00
126e77eec0 1日目 2026-10-22 16:55:29 +09:00
5 changed files with 366 additions and 225 deletions

View File

@@ -6,18 +6,18 @@
</projects> </projects>
<buildSpec> <buildSpec>
<buildCommand> <buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name> <name>org.eclipse.jdt.core.javabuilder</name>
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand> <buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name> <name>org.eclipse.wst.validation.validationbuilder</name>
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature> <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

View File

@@ -9,64 +9,133 @@ import com.rhizome.example.data.BaseData;
import com.rhizome.example.data.DepartmentData; import com.rhizome.example.data.DepartmentData;
public class DepartmentAccess extends BaseAccess{ public class DepartmentAccess extends BaseAccess{
private static final String DPT_FILEPATH = "file/DepartmentData.csv";
private static final String DEPARTMENT_FILEPATH = "file/DepartmentData.csv";
public DepartmentAccess() { public DepartmentAccess() {
super("file/DepartmentData.csv"); super(DEPARTMENT_FILEPATH);
} }
public List<BaseData> selectAll() throws FileNotFoundException { public List<BaseData> DepSelectAll() throws FileNotFoundException {
List<String> csvList = this.readData(); //全件データ
List<BaseData> datalist = new ArrayList(); List<BaseData> allList = new ArrayList<>();
for(String line : csvList) {// 26 //取得用リスト(CSV形式)
DepartmentData dpt = new DepartmentData(); List<String> csvList = readData();
for (String line : csvList) {
DepartmentData department = new DepartmentData();
String[] columns = line.split(","); String[] columns = line.split(",");
department.setIdDepartment(columns[0]);
department.setNmDepartment(columns[1]);
dpt.setIdDepartment(columns[0]);
dpt.setNmDepartment(columns[1]); allList.add(department);
datalist.add(dpt);
} }
return datalist; return allList;
} }
@Override
public List<BaseData> selectById(String key) throws FileNotFoundException { public List<BaseData> selectById(String key) throws FileNotFoundException {
List<BaseData> allList = this.selectAll(); //全件データ
List<BaseData> selectList = new ArrayList(); List<BaseData> allList = new ArrayList<>();
for(BaseData dpt : allList) { //取得用リスト(CSV形式)
if (dpt.getPrimaryKeyValue().equals(key)) { List<String> csvList = readData();
selectList.add(dpt);
for (String line : csvList) {
DepartmentData department = new DepartmentData();
String[] columns = line.split(",");
department.setIdDepartment(columns[0]);
department.setNmDepartment(columns[1]);
allList.add(department);
}
//IDでの抽出データ
List<BaseData> selectList = new ArrayList<>();
for (BaseData department : allList) {
if (department.getPrimaryKeyValue().equals(key)) {
selectList.add(department);
} }
} }
return selectList; return selectList;
} }
@Override
public void insert(BaseData data) throws IOException { public void insert(BaseData data) throws IOException {
this.addData(data);// 61 //CSVファイルに追記する
}// 62 addData(data);
}
@Override
public void update(BaseData updateData) throws IOException { public void update(BaseData updateData) throws IOException {
List<BaseData> updatedList = new ArrayList(); //更新後データリスト
List<BaseData> updatedList = new ArrayList<>();
for(BaseData dpt : this.selectAll()) { //全件取得し、更新データを反映する
if (dpt.getPrimaryKeyValue().equals(updateData.getPrimaryKeyValue())) {// 72 List<BaseData> allList = new ArrayList<>();
//取得用リスト(CSV形式)
List<String> csvList = readData();
for (String line : csvList) {
DepartmentData department = new DepartmentData();
String[] columns = line.split(",");
department.setIdDepartment(columns[0]);
department.setNmDepartment(columns[1]);
allList.add(department);
}
for (BaseData department : allList) {
if(department.getPrimaryKeyValue().equals(updateData.getPrimaryKeyValue())) {
//指定されたデータとキーが一致するデータの場合、更新データを設定
updatedList.add(updateData); updatedList.add(updateData);
} else { } else {
updatedList.add(dpt); //上記以外は取得したデータをそのまま設定
updatedList.add(department);
} }
} }
//ファイル上書き処理
updateData(updatedList);
this.updateData(updatedList);// 81 }
}// 83
@Override
public void delete(String key) throws IOException { public void delete(String key) throws IOException {
List<BaseData> deletedList = new ArrayList();// 88 //全件から削除データを除いたリスト
List<BaseData> deletedList = new ArrayList<>();
for(BaseData employee : this.selectAll()) {// 91 93 //全件取得し、keyに該当するデータをリストから削除する
if (!employee.getPrimaryKeyValue().equals(key)) {// 94 //全件データ
deletedList.add(employee);// 95 List<BaseData> allList = new ArrayList<>();
//取得用リスト(CSV形式)
List<String> csvList = readData();
for (String line : csvList) {
DepartmentData department= new DepartmentData();
String[] columns = line.split(",");
department.setIdDepartment(columns[0]);
department.setNmDepartment(columns[1]);
allList.add(department);
}
for (BaseData department : allList) {
if(!department.getPrimaryKeyValue().equals(key)) {
deletedList.add(department);
} }
} }
//ファイル上書き処理
updateData(deletedList); updateData(deletedList);
} }
} }

View File

@@ -16,6 +16,29 @@ public class EmployeeAccess extends BaseAccess {
super(EMPDATA_FILEPATH); super(EMPDATA_FILEPATH);
} }
public List<BaseData> empSelectAll() throws FileNotFoundException {
//全件データ
List<BaseData> allList = new ArrayList<>();
//取得用リスト(CSV形式)
List<String> csvList = readData();
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]);
allList.add(employee);
}
return allList;
}
@Override @Override
public List<BaseData> selectById(String key) throws FileNotFoundException { public List<BaseData> selectById(String key) throws FileNotFoundException {
//全件データ //全件データ
@@ -127,4 +150,6 @@ public class EmployeeAccess extends BaseAccess {
updateData(deletedList); updateData(deletedList);
} }
} }

View File

@@ -1,9 +1,10 @@
package com.rhizome.example.data; package com.rhizome.example.data;
public class DepartmentData extends BaseData{ public class DepartmentData extends BaseData{
/** 役職ID */
private String idDepartment; private String idDepartment;
/** 役職名 */
private String nmDepartment; private String nmDepartment;
public String getIdDepartment() { public String getIdDepartment() {
return idDepartment; return idDepartment;
} }
@@ -17,17 +18,19 @@ public class DepartmentData extends BaseData {
this.nmDepartment = nmDepartment; this.nmDepartment = nmDepartment;
} }
@Override
public String toString() { public String toString() {
return this.getIdDepartment() return getIdDepartment() + "\t" + getNmDepartment();
+ "\t" + this.getNmDepartment();
} }
@Override
public String toCSV() { public String toCSV() {
return this.getIdDepartment() return getIdDepartment() + "," + getNmDepartment();
+ "\t" + this.getNmDepartment();
} }
@Override
public String getPrimaryKeyValue() { public String getPrimaryKeyValue() {
return this.idDepartment; return getIdDepartment();
} }
} }

View File

@@ -1,8 +1,6 @@
package com.rhizome.example.main; package com.rhizome.example.main;
import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
@@ -13,21 +11,21 @@ import com.rhizome.example.data.BaseData;
import com.rhizome.example.data.DepartmentData; import com.rhizome.example.data.DepartmentData;
import com.rhizome.example.data.EmployeeData; import com.rhizome.example.data.EmployeeData;
public class Main { public class Main {
static Scanner sc = new Scanner(System.in); static Scanner sc = new Scanner(System.in);
private boolean employeeMode = true;
/** /**
* 処理開始位置 * 処理開始位置
* *
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("システムを開始します"); System.out.println("システムを開始します");
//※1 Main main = new Main();
while (true) { while (true) {
try { try {
new Main().start(); main.start();
} catch (Exception e) { } catch (Exception e) {
System.out.println("予期せぬエラーが発生しました"); System.out.println("予期せぬエラーが発生しました");
e.printStackTrace(); e.printStackTrace();
@@ -41,67 +39,50 @@ public class Main {
* *
*/ */
public void start() throws IOException { public void start() throws IOException {
System.out.println(); System.out.println();
System.out.println("1:検索 2:登録 3:更新 4:削除 9:データ切り替え 0:終了 (管理対象:Employee)"); System.out.println("1:検索 2:登録 3:更新 4:削除 9:データ切り替え 0:終了 "
String ope = sc.nextLine(); //キーボードから読み取り + "(管理対象:"
+(employeeMode ? "Employee" : "Department")
+ ")");
String ope = sc.nextLine();
switch (ope) { switch (ope) {
case "1": //検索 case "1": //検索
System.out.println(" 1:全件検索 2:条件検索 それ以外:戻る"); if(employeeMode) {
String choose = sc.nextLine(); //キーボードから読み取り start1();
if(choose.equals("1")) { //1の時 }else {
empSelect("all"); //引数all(文字列)を渡す start2();
}else if(choose.equals("2")) { //2の時
empSelect(); //引数なし
} }
break; break;
case "2": //登録 case "2": //登録
if(employeeMode) {
empInsert(); empInsert();
}else {
depInsert();
}
break; break;
case "3": //更新 case "3": //更新
if(employeeMode) {
empUpdate(); empUpdate();
}else {
depUpdate();
}
break; break;
case "4": //削除 case "4": //削除
if(employeeMode) {
empDelete(); empDelete();
break; }else {
case "9": //切り替え depDelete();
dptloop:while(true) {
System.out.println("1:検索 2:登録 3:更新 4:削除 9:データ切り替え 0:終了 (管理対象:Department)");
String dope = sc.nextLine();
switch (dope) {
case "1": //検索
System.out.println(" 1:全件検索 2:条件検索 それ以外:戻る");
String dptchoose = sc.nextLine();
if(dptchoose.equals("1")) {
dptSelect("all"); //全検索
}else if(dptchoose.equals("2")) {
dptSelect(); //条件検索
} }
break; break;
case "2": //登録 case "9": //データ切り替え
dptInsert(); employeeMode = !employeeMode;
break; break;
case "3": //更新
dptUpdate();
break;
case "4": //削除
dptDelete();
break;
case "9": //切り替え
break dptloop;
case "0": //終了 case "0": //終了
System.out.println("システムを終了します"); System.out.println("システムを終了します");
System.exit(0); System.exit(0);
default:
System.out.println("指定の番号を入力してください");
continue; //※2戻る
}
}
break;
case "0": //修了
System.out.println("システムを終了します");
System.exit(0);
default: default:
System.out.println("指定の番号を入力してください"); System.out.println("指定の番号を入力してください");
break; break;
@@ -113,6 +94,25 @@ public class Main {
* *
* @throws FileNotFoundException * @throws FileNotFoundException
*/ */
public void start1() throws IOException {
System.out.println();
System.out.println("1:全検索 2:条件検索");
String ope = sc.nextLine();
switch (ope) {
case "1": //全検索
//全出力
empSelectAll();
break;
case "2": //条件検索
empSelect();
break;
default:
//戻る
break;
}
}
public void empSelect() throws FileNotFoundException { public void empSelect() throws FileNotFoundException {
EmployeeAccess ea = new EmployeeAccess(); EmployeeAccess ea = new EmployeeAccess();
List<BaseData> list; List<BaseData> list;
@@ -131,50 +131,6 @@ public class Main {
System.out.println(employee.toString()); System.out.println(employee.toString());
} }
} }
//throwsで例外が起きたときにメインメソッドで処理できるようにする
public void empSelect(String str)throws FileNotFoundException { //allを受け取るメソッド
//try-catchで例外処理+try-with-resourcesでファイルを自動で閉じる
//csvファイルを読み込みreaderの変数へ代入
try (BufferedReader reader = new BufferedReader(new FileReader("file/EmployeeData.csv"))) {
String line; //変数定義
//ファイルから読み込んだ1行をlineに代入しnullじゃない間ループ
while((line = reader.readLine())!= null) {
System.out.println(line); //lineを出力
}
}catch(IOException e) { //エラーの受け取りと処理
System.out.println("ファイルの読み込み中にエラーが発生しました");
e.printStackTrace(); //エラーの内容を詳しく表示する処理
}
}
public void dptSelect() throws FileNotFoundException {
DepartmentAccess da = new DepartmentAccess();
List<BaseData> list;
String id = inputCheck("ID", true);
list = da.selectById(id);
if (list.size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
}
for (BaseData department : list) {
System.out.println(department.toString());
}
}
public void dptSelect(String str) throws FileNotFoundException {
try (BufferedReader dptreader = new BufferedReader(new FileReader("file/DepartmentData.csv"))) {
String line;
while((line = dptreader.readLine())!= null) {
System.out.println(line);
}
}catch(IOException e) {
System.out.println("ファイルの読み込み中にエラーが発生しました");
e.printStackTrace();
}
}
/** /**
* 社員ファイルに情報を登録する * 社員ファイルに情報を登録する
@@ -205,18 +161,6 @@ public class Main {
System.out.println("登録が完了しました"); System.out.println("登録が完了しました");
} }
public void dptInsert() throws IOException {
String depId = inputCheck("部署ID", true);
String depName = inputCheck("部署名", false);
DepartmentData dpt = new DepartmentData();
dpt.setIdDepartment(depId);
dpt.setNmDepartment(depName);
DepartmentAccess da = new DepartmentAccess();
da.insert(dpt);
System.out.println("登録が完了しました");
}
/** /**
* 社員ファイルの情報を更新する * 社員ファイルの情報を更新する
* *
@@ -254,25 +198,6 @@ public class Main {
System.out.println("更新が完了しました"); System.out.println("更新が完了しました");
} }
public void dptUpdate() throws IOException {
DepartmentAccess da = new DepartmentAccess();
String dptid = inputCheck("ID", true);
if (da.selectById(dptid).size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
String depId = inputCheck("部署ID", true);
String depName = inputCheck("部署名前", true);
DepartmentData dpt = new DepartmentData();
dpt.setIdDepartment(depId);
dpt.setNmDepartment(depName);
da.update(dpt);
System.out.println("更新が完了しました");
}
/** /**
* 社員ファイルの情報を削除する * 社員ファイルの情報を削除する
* *
@@ -300,25 +225,6 @@ public class Main {
System.out.println("削除が完了しました"); System.out.println("削除が完了しました");
} }
public void dptDelete() throws IOException {
DepartmentAccess da = new DepartmentAccess();
String dptid = inputCheck("ID", true);
if (da.selectById(dptid).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;
}
da.delete(dptid);
System.out.println("削除が完了しました");
}
/** /**
* 入力値を判定し、返却する * 入力値を判定し、返却する
* *
@@ -349,4 +255,142 @@ public class Main {
} }
return str; return str;
} }
public void empSelectAll() throws FileNotFoundException {
EmployeeAccess ea = new EmployeeAccess();
List<BaseData> list = ea.empSelectAll();
for (BaseData employee : list) {
System.out.println(employee.toString());
}
}
//Department
public void start2() throws IOException {
System.out.println();
System.out.println("1:全検索 2:条件検索");
String ope = sc.nextLine();
switch (ope) {
case "1": //全検索
//全出力
DepSelectAll();
break;
case "2": //条件検索
depSelect();
break;
default:
//戻る
break;
}
}
public void depSelect() throws FileNotFoundException {
DepartmentAccess ea = new DepartmentAccess();
List<BaseData> list;
//IDの入力および入力チェック
String id = inputCheck("ID", true);
//idに一致する社員データ取得
list = ea.selectById(id);
if (list.size() == 0) { //取得結果が0件の場合
System.out.println("入力された値で情報がみつかりませんでした");
}
//取得結果件数分、データをタブ区切りで出力する
for (BaseData department : list) {
System.out.println(department.toString());
}
}
/**
* 社員ファイルに情報を登録する
*
* @throws IOException
*/
public void depInsert() throws IOException {
//各項目の入力および入力チェック処理
String depId = inputCheck("部署ID", true);
String depNm = inputCheck("部署名",false);
//社員データインスタンスを生成し、入力値を設定
DepartmentData department = new DepartmentData();
department.setIdDepartment(depId);
department.setNmDepartment(depNm);
DepartmentAccess ea = new DepartmentAccess();
//ファイルデータ追加処理
ea.insert(department);
System.out.println("登録が完了しました");
}
/**
* 社員ファイルの情報を更新する
*
* @throws IOException
*/
public void depUpdate() throws IOException {
DepartmentAccess ea = new DepartmentAccess();
//IDの入力および入力チェック
String id = inputCheck("ID", true);
//IDの存在チェック
if (ea.selectById(id).size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
//各項目の入力および入力チェック処理
String depId = inputCheck("部署ID", true);
String depNm = inputCheck("部署名", false);
//社員データインスタンスを生成し、入力値を設定
DepartmentData department = new DepartmentData();
department.setIdDepartment(depId);
department.setNmDepartment(depNm);
//ファイルデータ更新処理
ea.update(department);
System.out.println("更新が完了しました");
}
/**
* 社員ファイルの情報を削除する
*
* @throws IOException
*/
public void depDelete() throws IOException {
DepartmentAccess ea = new DepartmentAccess();
//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("削除が完了しました");
}
public void DepSelectAll()throws FileNotFoundException{
DepartmentAccess da = new DepartmentAccess();
List<BaseData> list = da.DepSelectAll();
for (BaseData department : list) {
System.out.println(department.toString());
}
}
} }