(사진출처)https://ko.ac-illust.com/clip-art/680998/%EC%95%BC%EA%B5%AC
Member Management Project
- 구현 기능 -
1. 메뉴선택 입력값 제어
2018/05/28 - [Java/Open source] - ASCII 코드와 replace함수
2. 프로젝트 패키징 (mainClass, daoClass, dtoClass, fileClass)
3. ArrayList & Generic tunction
# 후에 업로드 하겠습니다.
4. Export & Import file
2018/05/31 - [Java/Project] - C.R.U.D filework version project
mainClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | package main; import java.io.IOException; import java.util.Scanner; import dao.daoClass; public class mainClass { // Input controller public static boolean controller(String conversion) { boolean b = true; for (int i = 0; i < conversion.length(); i++) { char c = conversion.charAt(i); if (conversion.length() != 1) { b = false; break; } if ((int) c < 49 || (int) c > 53) { b = false; break; } } return b; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); // Create instance of daoClass daoClass dao = new daoClass(); // 변수선언 String _chNum, ExImport; int chNum; while (true) { System.out.println(); // 개행 System.out.println("# 실행 하실 작업의 번호를 입력하시오"); System.out.println(" 1. 선수등록 - 추가 "); System.out.println(" 2. 등록삭제 - 삭제 "); System.out.println(" 3. 선수검색 - 검색 "); System.out.println(" 4. 정보수정 - 수정 "); System.out.println(" 5. 모두출력 - 출력 "); System.out.print(" 6. Exit - 프로그램 종료\n>>>"); if (!controller(_chNum = sc.next())) { System.out.println("\n입력값이 올바르지 않습니다.\n"); continue; } chNum = Integer.parseInt(_chNum); switch (chNum) { case 1: // 1. 선수등록 - 추가 dao.insert(); break; case 2: // 2. 등록삭제 - 삭제 dao.delete(); break; case 3: // 3. 선수검색 - 검색 dao.search(); break; case 4: // 4. 정보수정 - 수정 dao.update(); break; case 5: // 5. 모두출력 - 출력 dao.PrintAll(); break; default: // 6. Exit - 프로그램 종료 System.out.print("\n파일을 저장하시겠습니까?(y/n)\n>>>"); ExImport = sc.next(); if (ExImport.equals("y") || ExImport.equals("Y")) { dao._export(); } System.out.println("\n프로그램을 종료합니다"); System.exit(0); } } } } | cs |
daoClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | package dao; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import dto.batter; import dto.pitcher; import dto.playerClass; import file.fileClass; public class daoClass { Scanner sc = new Scanner(System.in); // Table of total data ArrayList<playerClass> player = new ArrayList<playerClass>(); private fileClass file = new fileClass(); // Member variable private int entryNum = 1; private playerClass aPlayer; // getter & setter public ArrayList<playerClass> getPlayer() { return player; } public void setPlayer(ArrayList<playerClass> player) { this.player = player; } // Initialize in mainProcess public daoClass() { _import(); } // Insert - 추가 public void insert() { String backNum, name, age, team, pos, AB = null, H = null, AVG = null, WIN = null, LOSE = null, WPCT = null; System.out.println(); // 개행 while (true) { System.out.println("등록하실 선수의 정보를 입력하시오"); System.out.print("# 등번호\n>>>"); backNum = sc.next(); System.out.print("# 이름\n>>>"); name = sc.next(); System.out.print("# 나이\n>>>"); age = sc.next(); System.out.print("# 소속팀\n>>>"); team = sc.next(); System.out.print("# 포지션(bat/pit)\n>>>"); pos = sc.next(); if (pos.equals("bat")) { System.out.print("# 타수\n>>>"); AB = sc.next(); System.out.print("# 안타\n>>>"); H = sc.next(); System.out.print("# 타율\n>>>"); AVG = sc.next(); break; } else if (pos.equals("pit")) { System.out.print("# 승리\n>>>"); WIN = sc.next(); System.out.print("# 패수\n>>>"); LOSE = sc.next(); System.out.print("# 승률\n>>>"); WPCT = sc.next(); break; } else { System.out.println("\n선수입력 정보가 올바르지 않습니다."); continue; } } if (pos.equals("bat")) { aPlayer = new batter(backNum, name, age, team, pos, entryNum, AB, H, AVG); player.add(aPlayer); } else if (pos.equals("pit")) { aPlayer = new pitcher(backNum, name, age, team, pos, entryNum, WIN, LOSE, WPCT); player.add(aPlayer); } entryNum = player.size() + 1; System.out.println("\n" + player.size() + " 번째 선수가 등록되었습니다"); } // Delete - 삭제 public void delete() { System.out.print("\n등록삭제를 할 선수의 등록번호를 입력하시오\n>>>"); entryNum = sc.nextInt(); if (player.get(entryNum - 1) == null) { System.out.println("\n등록되지 않은 번호입니다."); return; } if (player.get(entryNum - 1).getPos().equals("bat")) { System.out.println("#BackNum\t#Name\t#Age\t#Team\t#Pos\t#EntryNum\t#AB\t#H\t#AVG"); System.out.println(player.get(entryNum - 1)); } else if (player.get(entryNum - 1).getPos().equals("pit")) { System.out.println("#BackNum\t#Name\t#Age\t#Team\t#Pos\t#EntryNum\t#WIN\t#LOSE\t#WPCT"); System.out.println(player.get(entryNum - 1)); } System.out.print("\n찾으신 정보를 삭제하고 싶으시면 'Y'아니면 'N'를 입력하시오\n>>>"); String check = sc.next(); if (check.equals("y") || check.equals("Y")) { player.remove(entryNum - 1); entryNum = player.size() - 1; System.out.println("\n삭제되었습니다."); } else { System.out.println("\n삭제를 취소합니다."); return; } } // Search - 검색 public void search() { System.out.println(); // 개행 System.out.print("검색하고 싶은 선수의 등록번호를 입력하시오\n>>>"); int entryNum = sc.nextInt(); if (player.get(entryNum - 1) == null) { System.out.println("\n등록되지 않은 번호입니다.\n"); return; } if (player.get(entryNum - 1).getPos().equals("bat")) { System.out.println("#BackNum\t#Name\t#Age\t#Team\t#Pos\t#EntryNum\t#AB\t#H\t#AVG"); System.out.println(player.get(entryNum - 1)); } else if (player.get(entryNum - 1).getPos().equals("pit")) { System.out.println("#BackNum\t#Name\t#Age\t#Team\t#Pos\t#EntryNum\t#WIN\t#LOSE\t#WPCT"); System.out.println(player.get(entryNum - 1)); } } // Update - 수정 public void update() { System.out.println(); // 개행 System.out.print("검색하고 싶은 선수의 등록번호를 입력하시오\n>>>"); int entryNum = sc.nextInt(); if (player.get(entryNum - 1) == null) { System.out.println("\n등록되지 않은 번호입니다.\n"); return; } if (player.get(entryNum - 1).getPos().equals("bat")) { System.out.println("#BackNum\t#Name\t#Age\t#Team\t#Pos\t#EntryNum\t#AB\t#H\t#AVG"); System.out.println(player.get(entryNum - 1)); } else if (player.get(entryNum - 1).getPos().equals("pit")) { System.out.println("#BackNum\t#Name\t#Age\t#Team\t#Pos\t#EntryNum\t#WIN\t#LOSE\t#WPCT"); System.out.println(player.get(entryNum - 1)); } System.out.print("\n찾으신 정보를 수정하고 싶으시면 'Y'아니면 'N'를 입력하시오\n>>>"); String check = sc.next(); if (check.equals("y") || check.equals("Y")) { String backNum, name, age, team, pos, AB = null, H = null, AVG = null, WIN = null, LOSE = null, WPCT = null; while (true) { System.out.print("# 등번호\n>>>"); backNum = sc.next(); System.out.print("# 이름\n>>>"); name = sc.next(); System.out.print("# 나이\n>>>"); age = sc.next(); System.out.print("# 소속팀\n>>>"); team = sc.next(); System.out.print("# 포지션(bat/pit)\n>>>"); pos = sc.next(); if (pos.equals("bat")) { System.out.print("# 타수\n>>>"); AB = sc.next(); System.out.print("# 안타\n>>>"); H = sc.next(); System.out.print("# 타율\n>>>"); AVG = sc.next(); break; } else if (pos.equals("pit")) { System.out.print("# 승리\n>>>"); WIN = sc.next(); System.out.print("# 패수\n>>>"); LOSE = sc.next(); System.out.print("# 승률\n>>>"); WPCT = sc.next(); break; } else { System.out.println("선수입력 정보가 올바르지 않습니다."); continue; } } if (pos.equals("bat")) { aPlayer = new batter(backNum, name, age, team, pos, entryNum, AB, H, AVG); player.add(aPlayer); } else if (pos.equals("pit")) { aPlayer = new pitcher(backNum, name, age, team, pos, entryNum, WIN, LOSE, WPCT); player.add(aPlayer); } System.out.println("\n" + entryNum + " 번째 선수가 수정되었습니다"); } } // PrintAll - 모두출력 public void PrintAll() { System.out.println(); // 개행 for (int i = 0; i < player.size(); i++) { if (player.get(i).getPos().equals("bat")) { // System.out.println("#BackNum\t#Name\t#Age\t#Team\t#Pos\t#EntryNum\t#AB\t#H\t#AVG"); System.out.println(player.get(i)); } else if (player.get(i).getPos().equals("pit")) { // System.out.println("#BackNum\t#Name\t#Age\t#Team\t#Pos\t#EntryNum\t#WIN\t#LOSE\t#WPCT"); System.out.println(player.get(i)); } } } // Export - 내보내기 public void _export() throws IOException { file.set_player(player); file.exportData(); } // Import - 덮어쓰기 public void _import() { file.importData(); player = file.get_player(); } } | cs |
playerClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | package dto; public class playerClass { // Parent of member variable private String backNum, name, age, team, pos; private int entryNum; public playerClass() { } public String getBackNum() { return backNum; } public void setBackNum(String backNum) { this.backNum = backNum; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getTeam() { return team; } public void setTeam(String team) { this.team = team; } public String getPos() { return pos; } public void setPos(String pos) { this.pos = pos; } public int getEntryNum() { return entryNum; } public void setEntryNum(int entryNum) { this.entryNum = entryNum; } @Override public String toString() { return backNum + "\t-" + name + "\t-" + age + "\t-" + team + "\t-" + pos + "\t-" + entryNum; } public playerClass(String backNum, String name, String age, String team, String pos, int entryNum) { super(); this.backNum = backNum; this.name = name; this.age = age; this.team = team; this.pos = pos; this.entryNum = entryNum; } } | cs |
batterClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package dto; public class batter extends playerClass { // Child of member variable private String AB, H, AVG; public batter() { } public String getAB() { return AB; } public void setAB(String aB) { AB = aB; } public String getH() { return H; } public void setH(String h) { H = h; } public String getAVG() { return AVG; } public void setAVG(String aVG) { AVG = aVG; } @Override public String toString() { return super.toString() + "\t-" + AB + "\t-" + H + "\t-" + AVG; } public batter(String backNum, String name, String age, String team, String pos, int entryNum, String aB, String h, String aVG) { super(backNum, name, age, team, pos, entryNum); AB = aB; H = h; AVG = aVG; } } | cs |
pitcherClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package dto; public class pitcher extends playerClass { // Child of member variable private String WIN, LOSE, WPCT; public pitcher() { } public String getWIN() { return WIN; } public void setWIN(String wIN) { WIN = wIN; } public String getLOSE() { return LOSE; } public void setLOSE(String lOSE) { LOSE = lOSE; } public String getWPCT() { return WPCT; } public void setWPCT(String wPCT) { WPCT = wPCT; } @Override public String toString() { return super.toString() + "\t-" + WIN + "\t-" + LOSE + "\t-" + WPCT; } public pitcher(String backNum, String name, String age, String team, String pos, int entryNum, String wIN, String lOSE, String wPCT) { super(backNum, name, age, team, pos, entryNum); WIN = wIN; LOSE = lOSE; WPCT = wPCT; } } | cs |
fileClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | package file; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; import dto.batter; import dto.playerClass; public class fileClass { Scanner sc = new Scanner(System.in); // Member variable ArrayList<playerClass> _player = new ArrayList<playerClass>(); private String fileName; // getter & setter public ArrayList<playerClass> get_player() { return _player; } public void set_player(ArrayList<playerClass> _player) { this._player = _player; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } // Export ---------------------------------------- // ExportData(in findedFile, writeFile) public void exportData() throws IOException { while (true) { System.out.print("\n새로운 파일을 생성하시겠습니까?(y/n)\n>>>"); String check = sc.next(); if (check.equals("n") || check.equals("N")) { File newFile = new File("d:\\tmp\\" + fileName + ".txt"); writeFile(newFile); System.out.println("\n기존 파일에 저장하였습니다."); break; } System.out.print("\n새로운 파일명을 입력하시오\n>>>"); fileName = sc.next(); boolean b = findedFile(fileName); if (b == true) { System.out.println("\n같은 파일이 존재하여 파일생성에 실패하였습니다\n"); continue; } File newFile = new File("d:\\tmp\\" + fileName + ".txt"); try { if (newFile.createNewFile() == true) { writeFile(newFile); System.out.println("\n파일을 생성하였습니다."); break; } } catch (IOException e) { System.out.println("\n예외가 발생하였음.. 파일 생성실패"); } } } // FindedFile public boolean findedFile(String fileName) { File dirFile = new File("d:\\tmp"); String fileList[] = dirFile.list(); boolean b = false; for (int i = 0; i < fileList.length; i++) { if (fileList[i].equals(fileName + ".txt") == true) { b = true; break; } } return b; } // WriteFile(in checkBeforeWriteFile) public void writeFile(File newFile) throws IOException { boolean b; b = checkBeforeWriteFile(newFile); if (b == false) { System.out.println("파일에 기입할 수 없습니다."); return; } PrintWriter pw; String temp; pw = new PrintWriter(new BufferedWriter(new FileWriter(newFile))); for (int i = 0; i < _player.size(); i++) { temp = _player.get(i).toString(); pw.println(temp); } pw.close(); } // CheckBeforWriteFile public boolean checkBeforeWriteFile(File fileName) { if (fileName.canWrite()) { return true; } return false; } // Import ---------------------------------------- // ImportData(in readFile) public void importData() { String backNum, name, age, team, pos, AB = null, H = null, AVG = null, WIN = null, LOSE = null, WPCT = null; String ExImport; int entryNum; while (true) { System.out.print("\n이전 파일을 불러오겠습니까?(y/n)\n>>>"); ExImport = sc.next(); if (ExImport.equals("y") || ExImport.equals("Y")) { String fileImport[] = readFile(); if (fileImport == null) { continue; } for (int i = 0; i < fileImport.length; i++) { String splits[] = fileImport[i].split("\t-"); backNum = splits[0]; name = splits[1]; age = splits[2]; team = splits[3]; pos = splits[4]; entryNum = Integer.parseInt(splits[5]); if (pos.equals("bat")) { AB = splits[6]; H = splits[7]; AVG = splits[8]; playerClass aPlayer = new batter(backNum, name, age, team, pos, entryNum, AB, H, AVG); _player.add(aPlayer); } else if (pos.equals("pit")) { WIN = splits[6]; LOSE = splits[7]; WPCT = splits[8]; playerClass aPlayer = new batter(backNum, name, age, team, pos, entryNum, WIN, LOSE, WPCT); _player.add(aPlayer); } } } else if (ExImport.equals("n") || ExImport.equals("N")) { return; } else { System.out.println("\n입력값이 잘 못 되었습니다\n"); continue; } break; } } // ReadFile(in checkBeforeReadFile) public String[] readFile() { String datas[] = null; System.out.print("불러올 파일명을 입력하시오\n>>>"); fileName = sc.next(); setFileName(fileName); File readFile = new File("d:\\tmp\\" + fileName + ".txt"); boolean b = checkBeforeReadFile(readFile); if (!b) { System.out.println("파일이 존재하지 않거나 읽을 수 없다"); return null; } try { BufferedReader br = new BufferedReader(new FileReader(readFile)); int count = 0; String str; while ((str = br.readLine()) != null) { count++; } br.close(); datas = new String[count]; int w = 0; br = new BufferedReader(new FileReader(readFile)); while ((str = br.readLine()) != null) { datas[w] = str; w++; } br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return datas; } // CheckBeforeReadFile static boolean checkBeforeReadFile(File f) { if (f.exists()) { if (f.isFile() && f.canRead()) { return true; } } return false; } } | cs |
'오랜된 포스팅 > Java' 카테고리의 다른 글
Lotto program ver_01 (0) | 2018.06.03 |
---|---|
List Management Functions in java (0) | 2018.06.01 |
C.R.U.D filework version project (0) | 2018.05.31 |
문자열(String)과 함께 사용되는 함수(method) (0) | 2018.05.28 |
ASCII 코드와 replace함수 (0) | 2018.05.28 |