๐ฃ์ํ์ฝ๋ฉ ๊ฐ์๐ฃ
JAVA1 - 14.6. ๋์ ์ฑ ๋ง๋ค๊ธฐ - ์กฐ๊ฑด๋ฌธ
https://www.youtube.com/watch?v=y-qUWhanlkM&list=PLuHgQVnccGMAIluRRVsC1e79ri-dwnBmR&index=44
JAVA1 = 14.7. ๋์ ์ฑ ๋ง๋ค๊ธฐ - ๋ฐฐ์ด
https://www.youtube.com/watch?v=sMBu20ZZwzE&list=PLuHgQVnccGMAIluRRVsC1e79ri-dwnBmR&index=45
JAVA1 - 14.8. แแ กแแ ด แแ ขแธ แแ กแซแแ ณแฏแแ ต - แแ กแซแแ ฉแจแแ ฎแซ
https://www.youtube.com/watch?v=4-5tmh5Krk8&list=PLuHgQVnccGMAIluRRVsC1e79ri-dwnBmR&index=46
JAVA - 14.9. แแ กแแ ด แแ ขแธ แแ กแซแแ ณแฏแแ ต - แแ ฆแแ ฉแแ ณ
https://www.youtube.com/watch?v=XXFaCUcwWIk&list=PLuHgQVnccGMAIluRRVsC1e79ri-dwnBmR&index=47
JAVA - 14.10. แแ กแแ ด แแ ขแธ แแ กแซแแ ณแฏแแ ต - แแ ณแฏแ แ ขแแ ณ
https://www.youtube.com/watch?v=l8hrA4VSIlA&list=PLuHgQVnccGMAIluRRVsC1e79ri-dwnBmR&index=48
JAVA1 - 14.11. แแ กแแ ด แแ ขแธ แแ กแซแแ ณแฏแแ ต - แแ ตแซแแ ณแแ ฅแซแแ ณ
https://www.youtube.com/watch?v=TOptBzGgJOw&list=PLuHgQVnccGMAIluRRVsC1e79ri-dwnBmR&index=49
๐พ ์ด์ด์ ํ๊ธฐ
์ ์ด๋ฌธ ํ์ฉํ๊ธฐ
โ๏ธ ์กฐ๊ฑด๋ฌธ
public class Main {
public static void main(String[] args) {
...
double dividend1;
double dividend2;
double dividend3;
// ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ฐฐ๋น๊ธ ๊ณ์ฐํ๊ธฐ
if (income > 10000.0) {
dividend1 = income * 0.5;
dividend2 = income * 0.3;
dividend3 = income * 0.2;
} else {
dividend1 = income * 1.0;
dividend2 = income * 0;
dividend3 = income * 0;
}
...
System.out.println("Dividend 1: "+dividend1);
System.out.println("Dividend 2: "+dividend2);
System.out.println("Dividend 3: "+dividend3);
}
}
โ๏ธ ๋ฐ๋ณต๋ฌธ (+ ๋ฐฐ์ด)
public class Main {
public static void main(String[] args) {
...
// ๋ณ์๊ฐ ๋ง์์ง์๋ก ๋ณต์กํด์ง ๊ฒ -> ๋ฐฐ์ด ๋์
double[] dividendRates = new double[3]
dividendRates[0] = 0.5;
dividendRates[1] = 0.3;
dividendRates[2] = 0.2;
int i = 0;
while(i < dividendRates.length) {
System.out.println("Dividend: "+ (income * dividendRates[i]));
i += 1;
}
}
}
๋ฉ์๋ & ํด๋์ค & ์ธ์คํด์ค ํ์ฉํ๊ธฐ
- ๋ฉ์๋ : ์๋ก ์ฐ๊ด๋ ์ฝ๋๋ฅผ grouping ํ์ฌ ์ด๋ฆ ๋ถ์ธ ๊ฒ
- ํด๋์ค : ์๋ก ์ฐ๊ด๋ ๋ณ์์ ๋ฉ์๋๋ฅผ grouping ํ์ฌ ์ด๋ฆ ๋ถ์ธ ๊ฒ
- ์ธ์คํด์ค : ํ๋์ ํด๋์ค๋ฅผ ๋ณต์ ํ์ฌ ์๋ก ๋ค๋ฅธ ๋ฐ์ดํฐ์ ๊ฐ๊ณผ ์๋ก ๊ฐ์ ๋ฉ์๋๋ฅผ ๊ฐ์ง ๋ณต์ ๋ณธ์ ๊ฐ๋ ๊ฒ
โ๏ธ ๋ฉ์๋ & ํด๋์ค ์์ฑํ๊ธฐ
class Accounting {
public static double valueOfSupply;
public static double vatRate;
public static double expenseRate;
public static void print() {
System.out.println("VAT: " + getVAT());
System.out.println("Total: " + getTotal());
System.out.println("Expense: " + getExpense());
System.out.println("Income: " + getIncome());
System.out.println("Dividend 1: " + getDividend1());
System.out.println("Dividend 2: " + getDividend2());
System.out.println("Dividend 3: " + getDividend3());
}
public static double getDividend3() {
return getIncome() * 0.2;
}
public static double getDividend2() {
return getIncome() * 0.3;
}
public static double getDividend1() {
return getIncome() * 0.5;
}
public static double getIncome() {
return valueOfSupply - getExpense();
}
public static double getExpense() {
return valueOfSupply * expenseRate;
}
public static double getTotal() {
return valueOfSupply + getVAT();
}
public static double getVAT() {
return valueOfSupply * vatRate;
}
}
public class Main {
public static void main(String[] args) {
Accounting.valueOfSupply = 10000.0;
Accounting.vatRate = 0.1;
Accounting.expenseRate = 0.3;
// Refactor -> Extract Method
Accounting.print();
// anotherVariable = ...;
// anotherMethod = ...;
}
}
โ๏ธ ์ธ์คํด์ค ์ ์ฉํ๊ธฐ
→ static ์ญ์ ํ๊ธฐ
class Accounting {
public double valueOfSupply;
public double vatRate;
public double expenseRate;
public void print() {
...
}
public double getDividend3() {
...
}
public double getDividend2() {
...
}
public double getDividend1() {
...
}
public double getIncome() {
...
}
public double getExpense() {
...
}
public double getTotal() {
...
}
public double getVAT() {
...
}
}
public class Main {
public static void main(String[] args) {
// ์ธ์คํด์ค ์์ฑ
Accounting a1 = new Accounting();
al.valueOfSupply = 10000.0;
a1.vateRate = 0.1;
a1.expenseRate = 0.3;
a1.print();
Accounting a2 = new Accounting();
a2.valueOfSupply = 20000.0;
a2.vateRate = 0.2;
a2.expenseRate = 0.5;
a2.print();
}
}
'Spring > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Java Method (0) | 2023.09.28 |
---|---|
Java ์์ธ (Try-Catch / Try-Catch-Finally / Try with Resource Statements / Throws) (0) | 2023.09.26 |
์ฑ ๋ง๋ค๊ธฐ 1 - ๋ฐฐ๋น๊ธ ๊ณ์ฐ๊ธฐ (๋ณ์, ์ ๋ ฅ๊ฐ) (0) | 2023.09.22 |
๋ณ์ (0) | 2023.09.21 |
๋ฐ์ดํฐ (์ซ์, ๋ฌธ์์ด) ๋ค๋ฃจ๊ธฐ (0) | 2023.09.20 |