Created
March 6, 2023 16:22
-
-
Save zhyunk/212a3dd8e0f71426525935b63906a2f6 to your computer and use it in GitHub Desktop.
과제 8 피드백 반영!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
/* | |
* [과제 8. 연소득 과세금액 계산 프로그램] 의 피드백 반영! | |
* | |
* 작성자 : 김지현 | |
* 작성일 : 2023-03-06 | |
* */ | |
public class MiniAssignment08InFeedback { | |
public static void main(String[] args) { | |
CalcTax tax = new CalcTax(); | |
MyScanner my = new MyScanner(); | |
ArrayList list; | |
int annualIncome; | |
System.out.println("[과세금액 계산 프로그램]"); | |
annualIncome = my.getScannerReturnInt("연소득을 입력해 주세요.:"); | |
my.close(); | |
list = tax.getListTax(annualIncome); | |
tax.printTax(list); | |
System.out.println(); | |
System.out.println(String.format("[세율에 의한 세금]:\t%19d", tax.getSumTaxWithTaxPer(list))); | |
System.out.println(String.format("[누진공제 계산에 의한 세금]:%15d", tax.getNuJinGongJe(annualIncome))); | |
} | |
// 세금 계산 class | |
static class CalcTax { | |
int[] arrTax; | |
float[] arrTaxPer; | |
int[] arrNuJinGongJe; | |
CalcTax() { | |
// 구간별 과세 상한 금액 | |
arrTax = new int[]{ | |
12000000, 46000000, 88000000, 150000000, 300000000, 500000000, 1000000000 | |
}; | |
// 구간별 세율 | |
arrTaxPer = new float[]{ | |
6 / 100.0f, 15 / 100.0f, 24 / 100.0f, 35 / 100.0f, 38 / 100.0f, 40 / 100.0f, 42 / 100.0f, 45 / 100.0f | |
}; | |
// 구간별 누진공제 | |
arrNuJinGongJe = new int[]{ | |
0, 1080000, 5220000, 14900000, 19400000, 25400000, 35400000, 65400000 | |
}; | |
} | |
// 누진공제 계산후 결과 반환 | |
public int getNuJinGongJe(int annualIncome) { | |
// 연소득 * 세율 - 해당구간 누진공제 | |
int nuJinGongJe = 0; | |
for (int i = 0; i < arrNuJinGongJe.length; i++) { | |
if (i == 0 && annualIncome < arrTax[0] + 1) { | |
nuJinGongJe = 0; | |
break; | |
} else if ((i == arrTax.length) || (annualIncome < arrTax[i] + 1)) { | |
nuJinGongJe = (int)(annualIncome * arrTaxPer[i]) - arrNuJinGongJe[i]; | |
break; | |
} | |
} | |
return nuJinGongJe; | |
} | |
// 연소득을 세금 구간별로 계산한 각각의 값들 ArrayList에 저장하고 반환 | |
public ArrayList getListTax(int annualIncome) { | |
ArrayList<Integer> listMyTax = new ArrayList<>(); | |
int temp = 0; | |
int income = 0; | |
boolean isArrLast = false; | |
for (int i = 0; i < arrTaxPer.length; i++) { | |
// 처음 시작 혹은 연소득(1200만원 이하 또는 10억 초과) | |
if ((i == arrTax.length) || (arrTax[i] + 1 > annualIncome)) { | |
income = annualIncome; | |
isArrLast = true; | |
} else { | |
income = arrTax[i]; | |
} | |
if (i > 0) { | |
income -= arrTax[i - 1]; | |
} | |
temp = (int) (income * arrTaxPer[i]); | |
listMyTax.add(i, temp); | |
if (isArrLast) { | |
break; | |
} | |
} | |
return listMyTax; | |
} | |
// 세금 구간별로 계산된 금액 화면에 출력 | |
public void printTax(ArrayList listTax) { | |
for (int i = 0; i < listTax.size(); i++) { | |
if (i == arrTax.length) { | |
System.out.println(String.format("%11d 초과 * %2d%%\t= %10d", arrTax[i - 1], (int)(arrTaxPer[i] * 100), listTax.get(i))); | |
} else { | |
System.out.println(String.format("%15d * %2d%%\t= %10d", arrTax[i], (int)(arrTaxPer[i] * 100), listTax.get(i))); | |
} | |
} | |
} | |
// 세금 구간별로 계산된 금액을 모두 더하여 세율에 의한 세금 합계 | |
public int getSumTaxWithTaxPer(ArrayList<Integer> listTax) { | |
int sum = 0; | |
for (int i = 0; i < listTax.size(); i++) { | |
sum += listTax.get(i); | |
} | |
return sum; | |
} | |
} | |
static class MyScanner { | |
Scanner scan; | |
MyScanner() { | |
scan = new Scanner(System.in); | |
} | |
public int getScannerReturnInt(String text) { | |
String str; | |
while (true) { | |
System.out.print(text); | |
str = scan.nextLine(); | |
if (!str.replaceAll(" ", "").isEmpty() && str.replaceAll("[0-9]", "").isEmpty()) { | |
break; | |
} | |
} | |
return Integer.parseInt(str); | |
} | |
public void close() { | |
scan.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment