반응형
문제
package kr.or.ddit.homework;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HomeWork18 {
public static void main(String[] args) {
HomeWork18 obj = new HomeWork18();
obj.process();
}
public void process() {
List<Map> list = dataInput();
int maxSal = getMaxSalary(list);
System.out.println("최대 월급은 " + maxSal);
HashMap map = getDeptSalary2(list);
System.out.println("기획부 : "+ map.get("기획부"));
System.out.println("영업부 : "+ map.get("영업부"));
System.out.println("총무부 : "+ map.get("총무부"));
printEmp101(list);
}
public List<Map> dataInput(){
// emp_no emp_name dept salary
// 100 김기훈 영업부 300
// 101 홍성범 기획부 350
// 102 이만수 기획부 500
// 103 강나미 영업부 350
// 109 민병철 총무부 250
return null;
}
public int getMaxSalary(List<Map> l) {
/*
* 전체 직원중에 가장 높은 월급 구하기.
*/
for(int i=0; i<l.size(); i++) {
HashMap map = (HashMap) l.get(i);
String salary = (String) map.get("salary");
}
return 0;
}
public HashMap getDeptSalary2(List<Map> l) {
HashMap result = new HashMap();
// result.put("기획부", 300);
// 100 김기훈 영업부 300
// 101 홍성범 기획부 350
// 102 이만수 기획부 500
// 103 강나미 영업부 350
// 109 민병철 총무부 250
//result -> 영업부, 300
// -> 기획부, 350
// ->
for(int i=0; i<l.size(); i++) {
HashMap m = (HashMap) l.get(i);
// m 에서 부서 꺼내기
String dept = (String) m.get("dept");
// m 에서 월급 꺼내기
String salary = (String) m.get("salary");
int sal = Integer.parseInt(salary);
// 기획부
int sum = 0;
// 맵에 값이 포함되어 있는지
// if(result.containsKey(dept)) {
// sum = (int) result.get(dept);
// }
// m에서 꺼낸 월급.
sum+= sal ;
result.put(dept, sum);
}
return result;
}
public void printEmp101(List<Map> l ) {
/*
* emp_no 101인 사람의 모든 정보 출력하기.
*/
}
}
정답
package kr.or.ddit.homework;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class HomeWork18 {
public static void main(String[] args) {
HomeWork18 obj = new HomeWork18();
obj.process();
}
public void process() {
List<Map> list = dataInput();
int maxSal = getMaxSalary(list);
System.out.println("최대 월급은 " + maxSal);
System.out.println();
HashMap map = (HashMap)getDeptSalary(list);
System.out.println("기획부 : " + map.get("기획부"));
System.out.println("영업부 : " + map.get("영업부"));
System.out.println("총무부 : " + map.get("총무부"));
System.out.println();
printEmp101(list);
}
public List<Map> dataInput() {
// emp_no emp_name dept salary
// 100 김기훈 영업부 300
// 101 홍성범 기획부 350
// 102 이만수 기획부 500
// 103 강나미 영업부 350
// 109 민병철 총무부 250
HashMap map1 = new HashMap();
map1.put("emp_no", "100");
map1.put("emp_name", "김기훈");
map1.put("dept", "영업부");
map1.put("salary", "300");
HashMap map2 = new HashMap();
map2.put("emp_no", "101");
map2.put("emp_name", "홍성범");
map2.put("dept", "기획부");
map2.put("salary", "350");
HashMap map3 = new HashMap();
map3.put("emp_no", "102");
map3.put("emp_name", "이만수");
map3.put("dept", "기획부");
map3.put("salary", "500");
HashMap map4 = new HashMap();
map4.put("emp_no", "103");
map4.put("emp_name", "강나미");
map4.put("dept", "영업부");
map4.put("salary", "350");
HashMap map5 = new HashMap();
map5.put("emp_no", "109");
map5.put("emp_name", "민병철");
map5.put("dept", "총무부");
map5.put("salary", "250");
ArrayList list = new ArrayList();
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
return list;
}
public int getMaxSalary(List<Map> l) {
/*
* 전체 직위 중에 가장 높은 월급 구하기.
*/
int max = 0;
for(int i=0; i<l.size(); i++) {
HashMap map = (HashMap)l.get(i);
String salary = (String)map.get("salary");
int salary_int = Integer.valueOf(salary);
// System.out.println(salary_int);
if(max < salary_int) {
max = salary_int;
}
}
return max;
}
public HashMap getDeptSalary(List<Map> l) {
/*
* 각 부서별 연봉의 총합을 구하기.
*/
HashMap result = new HashMap();
// result.put("기획부", 300);
for(int i=0; i<l.size(); i++) {
HashMap m = (HashMap)l.get(i);
// m 에서 부서 꺼내기
String dept = (String)m.get("dept");
// System.out.println(dept);
// m 에서 월급 꺼내기
String salary = (String)m.get("salary");
int salary_int = Integer.valueOf(salary);
// System.out.println(salary_int);
int sum = 0;
// 맵에 값이 포함되어 있는지
if(result.containsKey(dept)) {
sum = (int) result.get(dept);
}
// m에서 꺼낸 월급
sum += salary_int;
result.put(dept, sum);
}
return result;
}
public void printEmp101(List<Map> l) {
/*
* emp_no 101인 사람의 모든 정보 출력하기.
*/
for(int i=0; i<l.size(); i++) {
HashMap m = (HashMap)l.get(i);
String emp_no = (String)m.get("emp_no");
if(emp_no.equals("101")) {
System.out.println("--------------------------------------");
System.out.println("emp_no : " + m.get("emp_no"));
System.out.println("emp_name : " + m.get("emp_name"));
System.out.println("dept : " + m.get("dept"));
System.out.println("salary : " + m.get("salary"));
}
}
}
}
반응형
'자바' 카테고리의 다른 글
[Java 초급] 22.5장 테스트 (0) | 2024.01.02 |
---|---|
[Java 초급] 22장 JDBC Utility 클래스 (0) | 2024.01.02 |
[Java 초급] 21장 컬렉션 프레임워크 (0) | 2023.12.30 |
[Java 초급] 20.5장 테스트 (0) | 2023.12.30 |
[Java 초급] 20장 달력 (0) | 2023.12.29 |