1 Commits

Author SHA1 Message Date
c18a8fe81c 2026/10/23 2026-10-23 16:26:44 +09:00
7 changed files with 519 additions and 238 deletions

View File

@@ -108,4 +108,12 @@ public abstract class BaseAccess {
* @throws IOException * @throws IOException
*/ */
public abstract void delete(String key) throws IOException; public abstract void delete(String key) throws IOException;
public List<BaseData> selectByidDepartment(String key) throws FileNotFoundException {
// TODO 自動生成されたメソッド・スタブ
return null;
}
} }

View File

@@ -8,65 +8,152 @@ import java.util.List;
import com.rhizome.example.data.BaseData; 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() {
super(DEPARTMENT_FILEPATH);
}
@Override
//条件検索用メソッド
public List<BaseData> selectByidDepartment(String key) throws FileNotFoundException {
//全件データ
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.setNameDepartment(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;
}
public DepartmentAccess() { //全件検索用メソッド
super("file/DepartmentData.csv"); public List<BaseData> selectByallDepartment(String key) throws FileNotFoundException {
} //全件データ
List<BaseData> allList = new ArrayList<>();
public List<BaseData> selectAll() throws FileNotFoundException {
List<String> csvList = this.readData(); //取得用リスト(CSV形式)
List<BaseData> datalist = new ArrayList(); List<String> csvList = readData();
for(String line : csvList) {// 26
DepartmentData dpt = new DepartmentData(); for (String line : csvList) {
String[] columns = line.split(","); DepartmentData department = new DepartmentData();
String[] columns = line.split(",");
dpt.setIdDepartment(columns[0]); department.setIdDepartment(columns[0]);
dpt.setNmDepartment(columns[1]); department.setNameDepartment(columns[1]);
datalist.add(dpt);
}
return datalist;
}
public List<BaseData> selectById(String key) throws FileNotFoundException {
List<BaseData> allList = this.selectAll();
List<BaseData> selectList = new ArrayList();
for(BaseData dpt : allList) {
if (dpt.getPrimaryKeyValue().equals(key)) {
selectList.add(dpt);
}
}
return selectList;
}
public void insert(BaseData data) throws IOException {
this.addData(data);// 61
}// 62
public void update(BaseData updateData) throws IOException { allList.add(department);
List<BaseData> updatedList = new ArrayList(); }
return allList;
}
for(BaseData dpt : this.selectAll()) {
if (dpt.getPrimaryKeyValue().equals(updateData.getPrimaryKeyValue())) {// 72
updatedList.add(updateData);
} else {
updatedList.add(dpt);
}
}
this.updateData(updatedList);// 81 @Override
}// 83 public void insert(BaseData data) throws IOException {
//CSVファイルに追記する
addData(data);
}
@Override
public void update(BaseData updateData) throws IOException {
//更新後データリスト
List<BaseData> updatedList = new ArrayList<>();
//全件取得し、更新データを反映する
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.setNameDepartment(columns[1]);
allList.add(department);
}
for (BaseData department : allList) {
if(department.getPrimaryKeyValue().equals(updateData.getPrimaryKeyValue())) {
//指定されたデータとキーが一致するデータの場合、更新データを設定
updatedList.add(updateData);
} else {
//上記以外は取得したデータをそのまま設定
updatedList.add(department);
}
}
//ファイル上書き処理
updateData(updatedList);
}
@Override
public void delete(String key) throws IOException {
//全件から削除データを除いたリスト
List<BaseData> deletedList = new ArrayList<>();
//全件取得し、keyに該当するデータをリストから削除する
//全件データ
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.setNameDepartment(columns[1]);
allList.add(department);
}
for (BaseData department : allList) {
if(!department.getPrimaryKeyValue().equals(key)) {
deletedList.add(department);
}
}
//ファイル上書き処理
updateData(deletedList);
}
@Override
public List<BaseData> selectById(String key) throws FileNotFoundException {
// TODO 自動生成されたメソッド・スタブ
return null;
}
public void delete(String key) throws IOException {
List<BaseData> deletedList = new ArrayList();// 88
for(BaseData employee : this.selectAll()) {// 91 93
if (!employee.getPrimaryKeyValue().equals(key)) {// 94
deletedList.add(employee);// 95
}
}
updateData(deletedList);
}
} }

View File

@@ -17,13 +17,15 @@ public class EmployeeAccess extends BaseAccess {
} }
@Override @Override
//条件検索用メソッド
public List<BaseData> selectById(String key) throws FileNotFoundException { public List<BaseData> selectById(String key) throws FileNotFoundException {
//全件データ //全件データ
List<BaseData> allList = new ArrayList<>(); List<BaseData> allList = new ArrayList<>();
//取得用リスト(CSV形式) //取得用リスト(CSV形式)
List<String> csvList = readData(); List<String> csvList = readData();
for (String line : csvList) { for (String line : csvList) {
EmployeeData employee = new EmployeeData(); EmployeeData employee = new EmployeeData();
String[] columns = line.split(","); String[] columns = line.split(",");
@@ -35,8 +37,8 @@ public class EmployeeAccess extends BaseAccess {
employee.setIdDepartment(columns[5]); employee.setIdDepartment(columns[5]);
allList.add(employee); allList.add(employee);
} }
//IDでの抽出データ //IDでの抽出データ
List<BaseData> selectList = new ArrayList<>(); List<BaseData> selectList = new ArrayList<>();
@@ -47,7 +49,35 @@ public class EmployeeAccess extends BaseAccess {
} }
return selectList; return selectList;
} }
//全件検索用メソッド
public List<BaseData> selectByall(String key) 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 void insert(BaseData data) throws IOException { public void insert(BaseData data) throws IOException {

View File

@@ -19,4 +19,5 @@ public abstract class BaseData {
*/ */
public abstract String getPrimaryKeyValue(); public abstract String getPrimaryKeyValue();
} }

View File

@@ -1,33 +1,53 @@
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 nameDepartment;
/**
* @return 部署ID
*/
public String getIdDepartment() { public String getIdDepartment() {
return idDepartment; return idDepartment;
} }
/**
* @param idDepartment 部署ID
*/
public void setIdDepartment(String idDepartment) { public void setIdDepartment(String idDepartment) {
this.idDepartment = idDepartment; this.idDepartment = idDepartment;
} }
public String getNmDepartment() {
return nmDepartment;
}
public void setNmDepartment(String nmDepartment) {
this.nmDepartment = nmDepartment;
}
/**
* @return 部署名
*/
public String getNameDepartment() {
return nameDepartment;
}
/**
* @param nameDepartment 部署名
*/
public void setNameDepartment(String nameDepartment) {
this.nameDepartment = nameDepartment;
}
@Override
public String toString() { public String toString() {
return this.getIdDepartment() // TODO 自動生成されたメソッド・スタブ
+ "\t" + this.getNmDepartment(); return getIdDepartment() + "\t" + getNameDepartment();
} }
@Override
public String toCSV() { public String toCSV() {
return this.getIdDepartment() // TODO 自動生成されたメソッド・スタブ
+ "\t" + this.getNmDepartment(); return getIdDepartment() + "," + getNameDepartment();
}
public String getPrimaryKeyValue() {
return this.idDepartment;
} }
@Override
public String getPrimaryKeyValue() {
// TODO 自動生成されたメソッド・スタブ
return idDepartment;
}
} }

View File

@@ -0,0 +1,234 @@
package com.rhizome.example.main;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.rhizome.example.access.DepartmentAccess;
import com.rhizome.example.data.BaseData;
import com.rhizome.example.data.DepartmentData;
public class DepartmentMain {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("システムを開始します");
while (true) {
try {
new DepartmentMain ().dpData();
} catch (Exception e) {
System.out.println("予期せぬエラーが発生しました");
e.printStackTrace();
}
}
}
/**
* 処理選択を行う
* @throws IOException
*
*/
public void dpData() throws IOException {
System.out.println();
System.out.println("1:検索 2:登録 3:更新 4:削除 9:データ切り替え 0:終了");
String ope = sc.nextLine();
DepartmentMain dp = new DepartmentMain();
switch (ope) {
case "1": //検索
dpSelect();
break;
case "2": //登録
dpInsert();
break;
case "3": //更新
dpUpdate();
break;
case "4": //削除
dpDelete();
break;
case "0": //修了
System.out.println("システムを終了します");
System.exit(0);
case "9":
dp.dpData();
default:
System.out.println("指定の番号を入力してください");
break;
}
}
/**
* 社員ファイルから情報を検索する
*
* @throws FileNotFoundException
*/
public void dpSelect() throws FileNotFoundException {
System.out.println();
System.out.println("1:全件検索 2:条件検索");
String search = sc.nextLine();
switch(search) {
case "1": //全件検索
//DepartmentData.csvファイルに記載されている情報が一覧で表示
DepartmentAccess all = new DepartmentAccess();
List<BaseData> allList = new ArrayList<>();
allList = all.selectByallDepartment(search);
for (BaseData department : allList) {
System.out.println(department.toString());
}
break;
case "2": //条件検索
//IDの入力および入力チェック
DepartmentAccess ea = new DepartmentAccess();
List<BaseData> list;
//IDの入力および入力チェック
String id = inputCheck("ID", true);
//idに一致する社員データ取得
list = ea.selectByidDepartment(id);
if (list.size() == 0) { //取得結果が0件の場合
System.out.println("入力された値で情報がみつかりませんでした");
}
//取得結果件数分、データをタブ区切りで出力する
for (BaseData department : list) {
System.out.println(department.toString());
}
break;
default: //1.2以外なら戻る
try {
dpData();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
} //メインメニューへ戻る
break;
}
}
/**
* 社員ファイルに情報を登録する
*
* @throws IOException
*/
public void dpInsert() throws IOException {
//各項目の入力および入力チェック処理
String id = inputCheck("部署ID", true);
String name = inputCheck("部署名", false);
//社員データインスタンスを生成し、入力値を設定
DepartmentData department = new DepartmentData();
department.setIdDepartment(id);
department.setNameDepartment(name);
DepartmentAccess ea = new DepartmentAccess();
//ファイルデータ追加処理
ea.insert(department);
System.out.println("登録が完了しました");
}
/**
* 社員ファイルの情報を更新する
*
* @throws IOException
*/
public void dpUpdate() throws IOException {
DepartmentAccess ea = new DepartmentAccess();
//IDの入力および入力チェック
String id = inputCheck("ID", true);
//IDの存在チェック
if (ea.selectById(id).size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
//各項目の入力および入力チェック処理
String name = inputCheck("部署名", false);
//社員データインスタンスを生成し、入力値を設定
DepartmentData department = new DepartmentData();
department.setIdDepartment(id);
department.setNameDepartment(name);
//ファイルデータ更新処理
ea.update(department);
System.out.println("更新が完了しました");
}
/**
* 社員ファイルの情報を削除する
*
* @throws IOException
*/
public void dpDelete() 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("削除が完了しました");
}
/**
* 入力値を判定し、返却する
*
* @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;
}
}

View File

@@ -1,18 +1,16 @@
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.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import com.rhizome.example.access.DepartmentAccess;
import com.rhizome.example.access.EmployeeAccess; import com.rhizome.example.access.EmployeeAccess;
import com.rhizome.example.data.BaseData; import com.rhizome.example.data.BaseData;
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);
@@ -22,9 +20,7 @@ public class Main {
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("システムを開始します"); System.out.println("システムを開始します");
//※1
while (true) { while (true) {
try { try {
new Main().start(); new Main().start();
@@ -42,139 +38,91 @@ 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(); //キーボードから読み取り String ope = sc.nextLine();
DepartmentMain dp = new DepartmentMain();
switch (ope) { switch (ope) {
case "1": //検索 case "1": //検索
System.out.println(" 1:全件検索 2:条件検索 それ以外:戻る"); empSelect();
String choose = sc.nextLine(); //キーボードから読み取り break;
if(choose.equals("1")) { //1の時
empSelect("all"); //引数all(文字列)を渡す
}else if(choose.equals("2")) { //2の時
empSelect(); //引数なし
}
break;
case "2": //登録 case "2": //登録
empInsert(); empInsert();
break; break;
case "3": //更新 case "3": //更新
empUpdate(); empUpdate();
break; break;
case "4": //削除 case "4": //削除
empDelete(); empDelete();
break;
case "9": //切り替え
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;
case "2": //登録
dptInsert();
break;
case "3": //更新
dptUpdate();
break;
case "4": //削除
dptDelete();
break;
case "9": //切り替え
break dptloop;
case "0": //終了
System.out.println("システムを終了します");
System.exit(0);
default:
System.out.println("指定の番号を入力してください");
continue; //※2戻る
}
}
break; break;
case "0": //修了 case "0": //修了
System.out.println("システムを終了します"); System.out.println("システムを終了します");
System.exit(0); System.exit(0);
case "9":
dp.dpData();
break;
default: default:
System.out.println("指定の番号を入力してください"); System.out.println("指定の番号を入力してください");
break; break;
} }
} }
/** /**
* 社員ファイルから情報を検索する * 社員ファイルから情報を検索する
* *
* @throws FileNotFoundException * @throws FileNotFoundException
*/ */
public void empSelect() throws FileNotFoundException { public void empSelect() throws FileNotFoundException {
EmployeeAccess ea = new EmployeeAccess(); System.out.println();
List<BaseData> list; System.out.println("1:全件検索 2:条件検索");
String search = sc.nextLine();
//IDの入力および入力チェック
String id = inputCheck("ID", true);
//idに一致する社員データ取得
list = ea.selectById(id);
if (list.size() == 0) { //取得結果が0件の場合
System.out.println("入力された値で情報がみつかりませんでした");
}
//取得結果件数分、データをタブ区切りで出力する
for (BaseData employee : list) {
System.out.println(employee.toString());
}
}
//throwsで例外が起きたときにメインメソッドで処理できるようにする
public void empSelect(String str)throws FileNotFoundException { //allを受け取るメソッド
//try-catchで例外処理+try-with-resourcesでファイルを自動で閉じる switch(search) {
//csvファイルを読み込みreaderの変数へ代入 case "1": //全件検索
try (BufferedReader reader = new BufferedReader(new FileReader("file/EmployeeData.csv"))) { //EmployeeData.csvファイルに記載されている情報が一覧で表示
String line; //変数定義 EmployeeAccess all = new EmployeeAccess();
//ファイルから読み込んだ1行をlineに代入しnullじゃない間ループ List<BaseData> allList = new ArrayList<>();
while((line = reader.readLine())!= null) { allList = all.selectByall(search);
System.out.println(line); //lineを出力
} for (BaseData employee : allList) {
}catch(IOException e) { //エラーの受け取りと処理 System.out.println(employee.toString());
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("ファイルの読み込み中にエラーが発生しました"); break;
e.printStackTrace();
case "2": //条件検索
//IDの入力および入力チェック
EmployeeAccess ea = new EmployeeAccess();
List<BaseData> list;
//IDの入力および入力チェック
String id = inputCheck("ID", true);
//idに一致する社員データ取得
list = ea.selectById(id);
if (list.size() == 0) { //取得結果が0件の場合
System.out.println("入力された値で情報がみつかりませんでした");
}
//取得結果件数分、データをタブ区切りで出力する
for (BaseData employee : list) {
System.out.println(employee.toString());
}
break;
default: //1.2以外なら戻る
try {
start();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
} //メインメニューへ戻る
break;
} }
} }
/** /**
* 社員ファイルに情報を登録する * 社員ファイルに情報を登録する
@@ -204,18 +152,6 @@ public class Main {
ea.insert(employee); ea.insert(employee);
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("登録が完了しました");
}
/** /**
* 社員ファイルの情報を更新する * 社員ファイルの情報を更新する
@@ -253,25 +189,6 @@ public class Main {
ea.update(employee); ea.update(employee);
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 +217,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 +247,7 @@ public class Main {
} }
return str; return str;
} }
} }