티스토리 뷰

주어진 엑셀 파일을 이용하여 1번부터 3번까지 문제에 대한 정답을 HTML 파일로 만들어 출력하는 프로젝트이다.

각 인구의 합계는 개별 데이터를 가져와서 합해주는 연산을 해주면 되니, 총 7개의 열만 가져와 작업을 했다.

 

ReadExcel 메서드:

@Override
	public void excelReader(String uri) {
		File file = new File(uri);
		Workbook wb = null;
		ArrayList<String> tempArray = new ArrayList<>();
		
		try {
			wb = Workbook.getWorkbook(file);
			Sheet sheet = wb.getSheet(0);
			Cell cell = null;
			int n = 4;
			while (true) {
				try {
					for (int i = 1; i < 13; i++) {
						if ((i==1)||(i==2)||(i==7)||(i==8)||(i==10)||(i==11)||(i==12)) {
							cell = sheet.getCell(i, n);
							tempArray.add(cell.getContents());
						}
					}
					DataClass.popList.add(new PopulationClass(tempArray.get(0), 
							tempArray.get(1), tempArray.get(2), tempArray.get(3), tempArray.get(4), tempArray.get(5), tempArray.get(6)));
					tempArray.clear();
					n++;
				} catch (Exception e) { 
					break;
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

이후 저장한 결과 값을 이용하여 1번부터 3번 문제를 푸는 메서드를 각각 구성하여 출력하면 된다.

 

각 문제에 필요한 메서드:

@Override
	public String getMostPopulous_Foreigner() {
		String name = "";
		int greatest = 0;
		for(PopulationClass pc : DataClass.popList) {
			if(pc.getTotalForeigner() > greatest) {
				greatest = pc.getTotalForeigner();
				name = pc.getNameOfTown();
			}
		}
		return name;
	}

	@Override
	public ArrayList<String> getKoreanTown() {
		ArrayList<String> names = new ArrayList<>();
		for(PopulationClass pc : DataClass.popList) {
			if(pc.getTotalKorean()>pc.getTotalForeigner()) {
				names.add(pc.getNameOfTown());
			}
		}
		return names;
	}
	
	@Override
	public String findThePlace() { //전체 인구 대비 외국인 인구의 비율이 1% 이하인 구 중에서 세대당 인구 평균이 가장 낮은 곳이 적합할 것이라고 판단
		
		String name = "";
		float min = 3.0f;
		for(PopulationClass pc : DataClass.popList) {
			float f =(float)pc.getTotalForeigner();
			float p = (float)pc.getTotalPopulation();
			if(((f)/p)*100 <= 1) {
				if(pc.getPopPerF() <= min) {
					name = pc.getNameOfTown();
					min = pc.getPopPerF();
				}
			}
		}
		return name;
		
	}

프로젝트의 깃 허브 주소:

github.com/Choonham/Choonham-2020.03.10-Spring-Class/tree/master/population_nyj

 

Choonham/Choonham-2020.03.10-Spring-Class

Contribute to Choonham/Choonham-2020.03.10-Spring-Class development by creating an account on GitHub.

github.com

 

끝!

Comments