This commit is contained in:
2026-10-22 16:55:29 +09:00
parent 19425cd977
commit 126e77eec0
10 changed files with 473 additions and 9 deletions

7
.classpath Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="実行例/em_oop.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/bin/

23
.project Normal file
View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>development-empmanager</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

7
.settings/.jsdtscope Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>

View File

@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.JRE_CONTAINER

View File

@@ -0,0 +1 @@
Global

View File

@@ -0,0 +1,141 @@
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 DEPARTMENT_FILEPATH = "file/DepartmentData.csv";
public DepartmentAccess() {
super(DEPARTMENT_FILEPATH);
}
public List<BaseData> DepSelectAll() 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.setNmDepartment(columns[1]);
allList.add(department);
}
return allList;
}
@Override
public List<BaseData> selectById(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.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;
}
@Override
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.setNmDepartment(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.setNmDepartment(columns[1]);
allList.add(department);
}
for (BaseData department : allList) {
if(!department.getPrimaryKeyValue().equals(key)) {
deletedList.add(department);
}
}
//ファイル上書き処理
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

@@ -0,0 +1,36 @@
package com.rhizome.example.data;
public class DepartmentData extends BaseData{
/** 役職ID */
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;
}
@Override
public String toString() {
return getIdDepartment() + "\t" + getNmDepartment();
}
@Override
public String toCSV() {
return getIdDepartment() + "," + getNmDepartment();
}
@Override
public String getPrimaryKeyValue() {
return getIdDepartment();
}
}

View File

@@ -5,13 +5,16 @@ import java.io.IOException;
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);
private boolean employeeMode = true;
/** /**
* 処理開始位置 * 処理開始位置
* *
@@ -19,9 +22,12 @@ public class Main {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("システムを開始します"); System.out.println("システムを開始します");
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();
@@ -35,24 +41,52 @@ 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:削除 0:終了"); System.out.println("1:検索 2:登録 3:更新 4:削除 9:データ切り替え 0:終了 "
+ "(管理対象:"
+(employeeMode ? "Employee" : "Department")
+ ")");
String ope = sc.nextLine(); String ope = sc.nextLine();
switch (ope) { switch (ope) {
case "1": //検索 case "1": //検索
empSelect(); if(employeeMode) {
start1();
}else {
start2();
}
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();
}else {
depDelete();
}
break; break;
case "0": //修了 case "9": //データ切り替え
employeeMode = !employeeMode;
break;
case "0": //終了
System.out.println("システムを終了します"); System.out.println("システムを終了します");
System.exit(0); System.exit(0);
default: default:
@@ -66,6 +100,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;
@@ -208,4 +261,173 @@ 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("削除が完了しました");
}
/**
* 入力値を判定し、返却する
*
* @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;
}
*/
public void DepSelectAll()throws FileNotFoundException{
DepartmentAccess da = new DepartmentAccess();
List<BaseData> list = da.DepSelectAll();
for (BaseData department : list) {
System.out.println(department.toString());
}
}
} }