Skip to content

Instantly share code, notes, and snippets.

@YongWanJin
Last active July 8, 2023 09:47
Show Gist options
  • Save YongWanJin/823253064adbf5a33c20c18ba6c13bcf to your computer and use it in GitHub Desktop.
Save YongWanJin/823253064adbf5a33c20c18ba6c13bcf to your computer and use it in GitHub Desktop.
제로베이스 백엔드 1주차 미니과제 8번
/*
진용완
제로베이스 백엔드 스쿨 15기
*/
import java.util.*;
class ExceptionNotIncome extends RuntimeException{}
class TaxYear {
// [ 변수 ]
long income; // 입력값 : 연소득
long sectionTax; // 출력값 : 구간 내에서 계산된 세금
long allTax; // 출력값 : 총 납부해야할 세금
Scanner sc;
TaxYear(){}
// [ public 메서드 ]
// # 입력을 받는 메서드
public void getInput(){
System.out.println("[과세금액 계산 프로그램]");
while (true) {
// 입력받기
try{
sc = new Scanner(System.in);
System.out.print("연소득을 입력해 주세요.:");
income = sc.nextLong();
// 적합한 입력 값이 아닌 것들 걸러내기
if (income<0){
throw new ExceptionNotIncome();
}
} catch (InputMismatchException | ExceptionNotIncome e) {
continue;
} break;
}
}
// # 출력값들 계산하고 출력하는 메서드
public void printResult(){
// * 지역변수들
// 세율이 달라지는 구간(section)의 기준점이 되는 연소득 Array
int[] secArr = {0,12000000,46000000,88000000,150000000,300000000,500000000,1000000000};
// 구간별 세율(ratio) Array
double[] rArr = {0.06, 0.15, 0.24, 0.35, 0.38, 0.4, 0.42, 0.45};
// 각 구간별 누진(cumulative)공제 금액 Array
int[] cArr = {0,1080000,5220000,14900000,19400000,25400000,35400000,65400000};
// 각 구간별 출력할 문자열(string) Array
String s06 = "";
String s15 = " 12000000 * 6% = 720000\n";
String s24 = " 34000000 * 15% = 5100000\n";
String s35 = " 42000000 * 24% = 10080000\n";
String s38 = " 62000000 * 35% = 21700000\n";
String s40 = " 150000000 * 38% = 57000000\n";
String s42 = " 200000000 * 40% = 80000000\n";
String s45 = " 500000000 * 42% = 210000000\n";
String[] sArr = {s06,s15, s24, s35, s38, s40, s42, s45};
// 세율 구간을 표기하는 지시변수
int section;
if (income <= secArr[1]) section = 1; // 세율 6% 구간
else if (income <= secArr[2]) section = 2; // 세율 15% 구간
else if (income <= secArr[3]) section = 3; // 세율 24% 구간
else if (income <= secArr[4]) section = 4; // 세율 35% 구간
else if (income <= secArr[5]) section = 5; // 세율 38% 구간
else if (income <= secArr[6]) section = 6; // 세율 40% 구간
else if (income <= secArr[7]) section = 7; // 세율 42% 구간
else section = 8; // 세율 45% 구간
// * 계산과 출력을 담당하는 지역 클래스와 메서드
class Cal{
// 해당 세율 구간에서의 세금 계산 메서드
private long calSectionTax(long income, int section){
long tax;
if (section == 1) return 0L;
tax = Math.round((income - secArr[section-1]) * rArr[section-1]);
return tax;
}
// 누적공제계산 메서드
private long calAllTax(long income, int section){
long tax;
tax = Math.round(income * rArr[section-1] - cArr[section-1]);
return tax;
}
// 계산 과정 출력 메서드
private void printPreTax(long income, int section, long sectionTax, long allTax){
for (int i=0; i<section; i++){
System.out.print(sArr[i]);
}
int temp = 0;
if (section>=2) {
for (int i = 0; i < section; i++) {
temp += secArr[i];
}
}
if (section == 1) {
System.out.printf("%10d * %2d%% = %10d",
income-secArr[section-1], Math.round(rArr[section-1]*100), allTax);
} else {
System.out.printf("%10d * %2d%% = %10d",
income-secArr[section-1], Math.round(rArr[section-1]*100), sectionTax);
}
}
}
// 구간별 세금, 전체 세금 게산
Cal cal = new Cal();
sectionTax = cal.calSectionTax(income, section);
allTax = cal.calAllTax(income, section);
// 결과 출력
cal.printPreTax(income, section, sectionTax, allTax);
System.out.printf("\n\n%-24s %d", "[세율에 의한 세금]:", allTax);
System.out.printf("\n%-20s %d", "[누진공제 계산에 의한 세금]:", allTax);
}
}
// 메인부
public class Mini8Tax {
public static void main(String[] args) {
TaxYear taxYear = new TaxYear();
taxYear.getInput();
taxYear.printResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment