Spring/JAVA

์•ฑ ๋งŒ๋“ค๊ธฐ 2 - ๋ฐฐ๋‹น๊ธˆ ๊ณ„์‚ฐ๊ธฐ (์ œ์–ด๋ฌธ, ๋ฐฐ์—ด, ๋ฉ”์†Œ๋“œ, ํด๋ž˜์Šค, ์ธ์Šคํ„ด์Šค)

alsruds 2023. 9. 25. 00:26
๐Ÿฃ์ƒํ™œ์ฝ”๋”ฉ ๊ฐ•์˜๐Ÿฃ

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

 

๐Ÿพ ์ด์–ด์„œ ํ•˜๊ธฐ

2023.09.22 - [Spring/์ƒํ™œ์ฝ”๋”ฉ - JAVA1] - ์•ฑ ๋งŒ๋“ค๊ธฐ 1 - ๋ฐฐ๋‹น๊ธˆ ๊ณ„์‚ฐ๊ธฐ (๋ณ€์ˆ˜, ์ž…๋ ฅ๊ฐ’)

 

์•ฑ ๋งŒ๋“ค๊ธฐ 1 - ๋ฐฐ๋‹น๊ธˆ ๊ณ„์‚ฐ๊ธฐ (๋ณ€์ˆ˜, ์ž…๋ ฅ๊ฐ’)

๐Ÿฃ์ƒํ™œ์ฝ”๋”ฉ ๊ฐ•์˜๐Ÿฃ JAVA1 - 14.1. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ์˜ค๋ฆฌ์—”ํ…Œ์ด์…˜ https://www.youtube.com/watch?v=mphrv80VaQI&list=PLuHgQVnccGMAIluRRVsC1e79ri-dwnBmR&index=39 JAVA1 - 14.2. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ๊ธฐ๋ณธ ๊ธฐ๋Šฅ ๊ตฌํ˜„ https://www.youtube.c

alsrudalsrudalsrud.tistory.com

 

 

์ œ์–ด๋ฌธ ํ™œ์šฉํ•˜๊ธฐ

โ˜๏ธ ์กฐ๊ฑด๋ฌธ

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();
    }
}