Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 507bb0797a | |||
| 126e77eec0 |
141
src/com/rhizome/example/access/DepartmentAccess.java
Normal file
141
src/com/rhizome/example/access/DepartmentAccess.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,20 +16,15 @@ public class EmployeeAccess extends BaseAccess {
|
|||||||
super(EMPDATA_FILEPATH);
|
super(EMPDATA_FILEPATH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<BaseData> empSelectAll() throws FileNotFoundException {
|
||||||
public List<BaseData> allSelect() throws FileNotFoundException {
|
|
||||||
|
|
||||||
//全件データ
|
//全件データ
|
||||||
List<BaseData> allList = new ArrayList<>();
|
List<BaseData> allList = new ArrayList<>();
|
||||||
|
|
||||||
//取得用リスト(CSV形式)
|
//取得用リスト(CSV形式)
|
||||||
List<String> csvList = readData();
|
List<String> csvList = readData();
|
||||||
//CSVデータを1行で出力
|
|
||||||
|
|
||||||
for (String line : csvList) {
|
for (String line : csvList) {
|
||||||
//インスタンス生成
|
|
||||||
EmployeeData employee = new EmployeeData();
|
EmployeeData employee = new EmployeeData();
|
||||||
//,=空白、[5]まで行ったら終了
|
|
||||||
String[] columns = line.split(",");
|
String[] columns = line.split(",");
|
||||||
employee.setIdEmployee(columns[0]);
|
employee.setIdEmployee(columns[0]);
|
||||||
employee.setNmEmployee(columns[1]);
|
employee.setNmEmployee(columns[1]);
|
||||||
@@ -37,13 +32,13 @@ public class EmployeeAccess extends BaseAccess {
|
|||||||
employee.setMailAddress(columns[3]);
|
employee.setMailAddress(columns[3]);
|
||||||
employee.setPassword(columns[4]);
|
employee.setPassword(columns[4]);
|
||||||
employee.setIdDepartment(columns[5]);
|
employee.setIdDepartment(columns[5]);
|
||||||
allList.add(employee);
|
|
||||||
}
|
|
||||||
return allList;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
allList.add(employee);
|
||||||
|
}
|
||||||
|
return allList;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<BaseData> selectById(String key) throws FileNotFoundException {
|
public List<BaseData> selectById(String key) throws FileNotFoundException {
|
||||||
//全件データ
|
//全件データ
|
||||||
@@ -154,5 +149,7 @@ public class EmployeeAccess extends BaseAccess {
|
|||||||
//ファイル上書き処理
|
//ファイル上書き処理
|
||||||
updateData(deletedList);
|
updateData(deletedList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
36
src/com/rhizome/example/data/DepartmentData.java
Normal file
36
src/com/rhizome/example/data/DepartmentData.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,10 @@ public class Main {
|
|||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("システムを開始します");
|
System.out.println("システムを開始します");
|
||||||
while (true) {
|
Main main = new Main();
|
||||||
|
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 +39,48 @@ 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": //検索
|
||||||
empSel();
|
if(employeeMode) {
|
||||||
|
start1();
|
||||||
|
}else {
|
||||||
|
start2();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "2": //登録
|
case "2": //登録
|
||||||
empInsert();
|
if(employeeMode) {
|
||||||
|
empInsert();
|
||||||
|
}else {
|
||||||
|
depInsert();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "3": //更新
|
case "3": //更新
|
||||||
empUpdate();
|
if(employeeMode) {
|
||||||
|
empUpdate();
|
||||||
|
}else {
|
||||||
|
depUpdate();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "4": //削除
|
case "4": //削除
|
||||||
empDelete();
|
if(employeeMode) {
|
||||||
|
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:
|
||||||
@@ -60,64 +88,32 @@ public class Main {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 検索方法を選択する
|
* 社員ファイルから情報を検索する
|
||||||
* @Override
|
*
|
||||||
* @throws FileNotFoundException
|
* @throws FileNotFoundException
|
||||||
*/
|
*/
|
||||||
public void empSel() throws FileNotFoundException {
|
|
||||||
//検索方法の選択
|
public void start1() throws IOException {
|
||||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
System.out.println();
|
||||||
String sel = sc.nextLine();
|
System.out.println("1:全検索 2:条件検索");
|
||||||
|
String ope = sc.nextLine();
|
||||||
switch (sel) {
|
|
||||||
case "1": //全検索
|
switch (ope) {
|
||||||
empList();
|
case "1": //全検索
|
||||||
|
//全出力
|
||||||
|
empSelectAll();
|
||||||
break;
|
break;
|
||||||
case "2": //条件検索
|
case "2": //条件検索
|
||||||
empSelect();
|
empSelect();
|
||||||
break;
|
break;
|
||||||
default: //それ以外:戻る
|
default:
|
||||||
|
//戻る
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void empSelect() throws FileNotFoundException {
|
||||||
/**
|
|
||||||
* 全検索で一覧を表示する
|
|
||||||
*
|
|
||||||
* @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 {
|
|
||||||
EmployeeAccess ea = new EmployeeAccess();
|
EmployeeAccess ea = new EmployeeAccess();
|
||||||
List<BaseData> list;
|
List<BaseData> list;
|
||||||
|
|
||||||
@@ -259,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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user