1 Commits

Author SHA1 Message Date
e1b74e4ad2 個人開発20261023 2026-10-23 16:25:33 +09:00
5 changed files with 261 additions and 94 deletions

View File

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

View File

@@ -0,0 +1,72 @@
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.DepartmentData;
public class DepartmentAccess extends BaseAccess {
private static final String DPT_FILEPATH = "file/DepartmentData.csv";
public DepartmentAccess() {
super("file/DepartmentData.csv");
}
public List<BaseData> selectAll() throws FileNotFoundException {
List<String> csvList = this.readData();
List<BaseData> datalist = new ArrayList();
for(String line : csvList) {// 26
DepartmentData dpt = new DepartmentData();
String[] columns = line.split(",");
dpt.setIdDepartment(columns[0]);
dpt.setNmDepartment(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 {
List<BaseData> updatedList = new ArrayList();
for(BaseData dpt : this.selectAll()) {
if (dpt.getPrimaryKeyValue().equals(updateData.getPrimaryKeyValue())) {// 72
updatedList.add(updateData);
} else {
updatedList.add(dpt);
}
}
this.updateData(updatedList);// 81
}// 83
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

@@ -15,34 +15,6 @@ public class EmployeeAccess extends BaseAccess {
public EmployeeAccess() {
super(EMPDATA_FILEPATH);
}
public List<BaseData> allSelect() throws FileNotFoundException {
//全件データ
List<BaseData> allList = new ArrayList<>();
//取得用リスト(CSV形式)
List<String> csvList = readData();
//CSVデータを1行で出力
for (String line : csvList) {
//インスタンス生成
EmployeeData employee = new EmployeeData();
//,=空白、[5]まで行ったら終了
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
public List<BaseData> selectById(String key) throws FileNotFoundException {

View File

@@ -0,0 +1,33 @@
package com.rhizome.example.data;
public class DepartmentData extends BaseData {
private String idDepartment;
private String 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;
}
public String toString() {
return this.getIdDepartment()
+ "\t" + this.getNmDepartment();
}
public String toCSV() {
return this.getIdDepartment()
+ "\t" + this.getNmDepartment();
}
public String getPrimaryKeyValue() {
return this.idDepartment;
}
}

View File

@@ -1,12 +1,16 @@
package com.rhizome.example.main;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import com.rhizome.example.access.DepartmentAccess;
import com.rhizome.example.access.EmployeeAccess;
import com.rhizome.example.data.BaseData;
import com.rhizome.example.data.DepartmentData;
import com.rhizome.example.data.EmployeeData;
public class Main {
@@ -18,7 +22,9 @@ public class Main {
* @param args
*/
public static void main(String[] args) {
System.out.println("システムを開始します");
//※1
while (true) {
try {
new Main().start();
@@ -36,88 +42,78 @@ public class Main {
*/
public void start() throws IOException {
System.out.println();
System.out.println("1:検索 2:登録 3:更新 4:削除 0:終了");
String ope = sc.nextLine();
System.out.println("1:検索 2:登録 3:更新 4:削除 9:データ切り替え 0:終了 (管理対象:Employee)");
String ope = sc.nextLine(); //キーボードから読み取り
switch (ope) {
case "1": //検索
empSel();
break;
System.out.println(" 1:全件検索 2:条件検索 それ以外:戻る");
String choose = sc.nextLine(); //キーボードから読み取り
if(choose.equals("1")) { //1の時
empSelect("all"); //引数all(文字列)を渡す
}else if(choose.equals("2")) { //2の時
empSelect(); //引数なし
}
break;
case "2": //登録
empInsert();
break;
break;
case "3": //更新
empUpdate();
break;
break;
case "4": //削除
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;
case "0": //修了
System.out.println("システムを終了します");
System.exit(0);
default:
System.out.println("指定の番号を入力してください");
break;
break;
}
}
/**
* 検索方法を選択する
* @Override
* @throws FileNotFoundException
*/
public void empSel() throws FileNotFoundException {
//検索方法の選択
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
String sel = sc.nextLine();
switch (sel) {
case "1": //全検索
empList();
break;
case "2": //条件検索
empSelect();
break;
default: //それ以外:戻る
break;
}
}
/**
* 全検索で一覧を表示する
*
* @throws FileNotFoundException
*/
public void empList() throws FileNotFoundException {
List<BaseData> allList;
//一覧データ取得
EmployeeAccess all = new EmployeeAccess();
allList = all.allSelect();
if (allList.size() == 0) { //取得結果が0件の場合
System.out.println("入力された値で情報がみつかりませんでした");
}
//for文を10回繰り返す
for(int) {
System.out.println(employee.toString());
}
//取得結果件数分、データをタブ区切りで出力する
/*for (BaseData employee : allList) {
System.out.println(employee.toString());
}*/
System.out.println(allList);
}
/**
* 社員ファイルから情報を検索する
*
* @throws FileNotFoundException
*/
public void empSelect() throws FileNotFoundException {
* 社員ファイルから情報を検索する
*
* @throws FileNotFoundException
*/
public void empSelect() throws FileNotFoundException {
EmployeeAccess ea = new EmployeeAccess();
List<BaseData> list;
@@ -135,6 +131,50 @@ public class Main {
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();
}
}
/**
* 社員ファイルに情報を登録する
@@ -164,6 +204,18 @@ public class Main {
ea.insert(employee);
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("登録が完了しました");
}
/**
* 社員ファイルの情報を更新する
@@ -201,6 +253,25 @@ public class Main {
ea.update(employee);
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("更新が完了しました");
}
/**
* 社員ファイルの情報を削除する
@@ -229,6 +300,25 @@ public class Main {
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("削除が完了しました");
}
/**
* 入力値を判定し、返却する
*