Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e1b74e4ad2 |
6
.project
6
.project
@@ -6,18 +6,18 @@
|
|||||||
</projects>
|
</projects>
|
||||||
<buildSpec>
|
<buildSpec>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.wst.validation.validationbuilder</name>
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
</buildSpec>
|
</buildSpec>
|
||||||
<natures>
|
<natures>
|
||||||
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||||
</natures>
|
</natures>
|
||||||
</projectDescription>
|
</projectDescription>
|
||||||
|
|||||||
72
src/com/rhizome/example/access/DepartmentAccess.java
Normal file
72
src/com/rhizome/example/access/DepartmentAccess.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,34 +15,6 @@ public class EmployeeAccess extends BaseAccess {
|
|||||||
public EmployeeAccess() {
|
public EmployeeAccess() {
|
||||||
super(EMPDATA_FILEPATH);
|
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
|
@Override
|
||||||
public List<BaseData> selectById(String key) throws FileNotFoundException {
|
public List<BaseData> selectById(String key) throws FileNotFoundException {
|
||||||
|
|||||||
33
src/com/rhizome/example/data/DepartmentData.java
Normal file
33
src/com/rhizome/example/data/DepartmentData.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +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.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 {
|
||||||
@@ -18,7 +22,9 @@ 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();
|
||||||
@@ -36,88 +42,78 @@ 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:終了 (管理対象:Employee)");
|
||||||
String ope = sc.nextLine();
|
String ope = sc.nextLine(); //キーボードから読み取り
|
||||||
|
|
||||||
switch (ope) {
|
switch (ope) {
|
||||||
case "1": //検索
|
case "1": //検索
|
||||||
empSel();
|
System.out.println(" 1:全件検索 2:条件検索 それ以外:戻る");
|
||||||
break;
|
String choose = sc.nextLine(); //キーボードから読み取り
|
||||||
|
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);
|
||||||
default:
|
default:
|
||||||
System.out.println("指定の番号を入力してください");
|
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
|
* @throws FileNotFoundException
|
||||||
*/
|
*/
|
||||||
public void empSelect() throws FileNotFoundException {
|
public void empSelect() throws FileNotFoundException {
|
||||||
EmployeeAccess ea = new EmployeeAccess();
|
EmployeeAccess ea = new EmployeeAccess();
|
||||||
List<BaseData> list;
|
List<BaseData> list;
|
||||||
|
|
||||||
@@ -135,6 +131,50 @@ 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社員ファイルに情報を登録する
|
* 社員ファイルに情報を登録する
|
||||||
@@ -164,6 +204,18 @@ 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("登録が完了しました");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社員ファイルの情報を更新する
|
* 社員ファイルの情報を更新する
|
||||||
@@ -201,6 +253,25 @@ 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("更新が完了しました");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社員ファイルの情報を削除する
|
* 社員ファイルの情報を削除する
|
||||||
@@ -229,6 +300,25 @@ 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("削除が完了しました");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 入力値を判定し、返却する
|
* 入力値を判定し、返却する
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user