Last active
September 17, 2022 11:35
-
-
Save soeunkk/269c5693afd76e07983aae69da6146ae to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* 김소은 | |
* 과제8. 연소득 과세금액 계산 프로그램 | |
* | |
* 과세계산과 관련된 변수, 함수를 묶어 Taxation 클래스와 단계별 과세 정보를 담는 TaxationSection 클래스를 만들었습니다. | |
* 각 클래스는 메소드만 호출할 수 있도록 하여 캡슐화했습니다. | |
* 입력값이 잘못됐으면 재입력할 수 있도록 구현하였습니다. | |
*/ | |
import java.util.InputMismatchException; | |
import java.util.Scanner; | |
class Taxation { | |
private final TaxationSection[] taxationSections; | |
public Taxation() { | |
taxationSections = new TaxationSection[] { | |
new TaxationSection(12000000, 0.06f, 0), | |
new TaxationSection(46000000, 0.15f, 1080000), | |
new TaxationSection(88000000, 0.24f, 5220000), | |
new TaxationSection(150000000, 0.35f, 14900000), | |
new TaxationSection(300000000, 0.38f, 19400000), | |
new TaxationSection(500000000, 0.4f, 25400000), | |
new TaxationSection(1000000000, 0.42f, 35400000), | |
new TaxationSection(Long.MAX_VALUE, 0.45f, 65400000) | |
}; | |
} | |
public float getTariff(int level) { | |
return taxationSections[level].getTariff(); | |
} | |
public long calculateTaxByProgressiveDeduction(long annualIncome, int level) { | |
return taxationSections[level].calculateTaxByProgressiveDeduction(annualIncome); | |
} | |
public long calculateTaxBaseTerm(int level) { | |
if (level == 0) { | |
return taxationSections[level].getTaxBase(); | |
} else { | |
return taxationSections[level].getTaxBase() - taxationSections[level - 1].getTaxBase(); | |
} | |
} | |
} | |
class TaxationSection { | |
private final long taxBase; // 과세표준 | |
private final float tariff; // 세율 | |
private final long progressiveDeduction; // 누진공제액 | |
public TaxationSection(long taxBase, float tariff, long progressiveDeduction) { | |
this.taxBase = taxBase; | |
this.tariff = tariff; | |
this.progressiveDeduction = progressiveDeduction; | |
} | |
public long getTaxBase() { | |
return taxBase; | |
} | |
public float getTariff() { | |
return tariff; | |
} | |
public long calculateTaxByProgressiveDeduction(long annualIncome) { | |
return (long)(annualIncome * tariff) - progressiveDeduction; | |
} | |
} | |
public class Test8 { | |
private static Scanner scanner; | |
public static void main(String[] args) { | |
scanner = new Scanner(System.in); | |
System.out.println("[과세금액 계산 프로그램]"); | |
long annualIncome = inputAnnualIncome(); | |
Taxation taxation = new Taxation(); | |
int level = 0; | |
long totalTax = 0; | |
long annualIncomeRest = annualIncome; | |
while (annualIncomeRest > 0) { | |
// 단계 별 세금 계산 | |
long taxBaseTerm = taxation.calculateTaxBaseTerm(level); | |
long incomeByLevel = Math.min(annualIncomeRest, taxBaseTerm); | |
long tax = (long)(incomeByLevel * taxation.getTariff(level)); | |
printTaxByLevel(incomeByLevel, (int)(taxation.getTariff(level) * 100), tax); | |
totalTax += tax; | |
annualIncomeRest -= incomeByLevel; | |
level++; | |
} | |
level--; | |
System.out.println(); | |
System.out.println("[세율에 의한 세금]:\t\t\t" + totalTax); | |
System.out.println("[누진공제 계산에 의한 세금]:\t" + taxation.calculateTaxByProgressiveDeduction(annualIncome, level)); | |
closeResource(); | |
} | |
private static int inputAnnualIncome() { | |
int annualIncome = 0; | |
boolean isValid = false; | |
while (! isValid) { | |
System.out.print("연소득을 입력해 주세요.:"); | |
try { | |
checkIntType(); | |
annualIncome = scanner.nextInt(); | |
checkValidAnnualIncome(annualIncome); | |
isValid = true; | |
} catch (InputMismatchException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
return annualIncome; | |
} | |
private static void checkIntType() { | |
if (! scanner.hasNextInt()) { | |
removeBuffer(); | |
throw new InputMismatchException("숫자만 입력 가능합니다."); | |
} | |
} | |
private static void checkValidAnnualIncome(long annualIncome) { | |
if (annualIncome < 0) { | |
throw new InputMismatchException("소득 값은 0 이상이어야 합니다."); | |
} | |
} | |
private static void removeBuffer() { scanner.next(); } | |
private static void printTaxByLevel(long incomeByLevel, int tariffPercent, long taxByLevel) { | |
System.out.printf("%12d * %2d%% = %12d\n", incomeByLevel, tariffPercent, taxByLevel); | |
} | |
private static void closeResource() { scanner.close(); } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment