๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ์ธ๊ณต๋ถ€/JAVA

[์ƒํ™œ์ฝ”๋”ฉ] JAVA1 - 14. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ 1

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2022. 9. 18.

 

 

์ƒํ™œ์ฝ”๋”ฉ JAVA1

- 1. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ์˜ค๋ฆฌ์—”ํ…Œ์ด์…˜ -

- 2. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ๊ธฐ๋ณธ ๊ธฐ๋Šฅ ๊ตฌํ˜„ -

- 3. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ๋ณ€์ˆ˜๋„์ž… -

- 4. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ์ž…๋ ฅ๊ฐ’ ๋„์ž… -

 


 

1. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ์˜ค๋ฆฌ์—”ํ…Œ์ด์…˜

์ผ๋‹จ ๋‚˜๋งŒ์˜ ์•ฑ์„ ๋งŒ๋“ค๊ธฐ์— ์•ž์„œ ์šฐ๋ฆฌ๊ฐ€ ๋ฌด์—‡์„ ๋งŒ๋“ค๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด ์ธ์ง€ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

 

๋‚ด๊ฐ€ ๋ฌผ๊ฑด์„ ํŒ๋งคํ•˜๋Š” ํŒ๋งค์ž๋ผ๊ณ  ๊ฐ€์ •ํ–ˆ์„๋•Œ, ์šฐ๋ฆฌ์—๊ฒŒ ๋ฌผ๊ฑด์„ ๊ตฌ์ž…ํ•˜๋Š” ์†Œ๋น„์ž๊ฐ€ ์žˆ๋‹ค.

์ด๋•Œ, ์†Œ๋น„์ž์—๊ฒŒ ๋ฌผ๊ฑด์„ ํŒ๋งคํ•  ๋•Œ ๋ฌผ๊ฑด์„ ๊ณต๊ธ‰ํ•˜๋Š” ๊ฐ€๊ฒฉ์ด ์žˆ๋Š”๋ฐ ์ด๋ฅผ ๊ณต๊ธ‰๊ฐ€๋ผ๊ณ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ตญ์„ธ์ฒญ์ด ์†Œ๋น„์ž์—๊ฒŒ ์†Œ๋น„์— ๋Œ€ํ•œ ์„ธ๊ธˆ์„ ๋ถ€๊ฐ€ํ•˜๋Š”๋ฐ ์ด๋ฅผ ๋ถ€๊ฐ€๊ฐ€์น˜์„ธ๋ผ๊ณ  ํ•œ๋‹ค.

์ด ๋ถ€๊ฐ€๊ฐ€์น˜์„ธ๋Š” ์†Œ๋น„์ž๊ฐ€ ๋”ฐ๋กœ ๊ตญ์„ธ์ฒญ์—๊ฒŒ ๋‚ฉ๋ถ€ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํŒ๋งค์ž๊ฐ€ ๊ณต๊ธ‰๊ฐ€์™€ ๋ถ€๊ฐ€๊ฐ€์น˜์„ธ๋ฅผ ํ•ฉ์นœ ๋ฌผ๊ฑด๊ฐ’์„ ๋‚ด๋†“์œผ๋ฉด ์†Œ๋น„์ž๊ฐ€ ์ด๋ฅผ ๊ตฌ์ž…ํ•ด ๋ถ€๊ฐ€๊ฐ€์น˜์„ธ๋ฅผ ํฌํ•จํ•œ ๊ธˆ์•ก์„ ์ง€๋ถˆํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ํŒ๋งค์ž๋Š” ๋ฌผ๊ฑด์— ํฌํ•จ๋œ ๋ถ€๊ฐ€๊ฐ€์น˜์„ธ๋ฅผ ๊ตญ์„ธ์ฒญ์— ๋ชจ์•„์„œ ๋‚ฉ๋ถ€ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋ฅผ ๋ถ€๊ฐ€์„ธ๋ผ๊ณ  ํ•œ๋‹ค.

์ด๊ฒŒ ๋๋‚œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํŒ๋งค์ž๊ฐ€ ์–ผ๋งˆ๋ฅผ ๋ฒŒ์—ˆ๋Š”์ง€ ์•Œ๊ธฐ ์œ„ํ•ด ๊ณต๊ธ‰๊ฐ€์—์„œ ๋ฌผ๊ฑด์„ ํŒ๋งคํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ ๋น„์šฉ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ด์ต์„ ๋™์—…์ž์™€ ๋ฐฐ๋ถ„ํ•˜๋Š” ๊ณผ์ •๋„ ์žˆ๋‹ค.

 

์ด๋Ÿฌํ•œ ๊ณผ์ •์€ ์šฐ๋ฆฌ๋Š” ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๋งŒ๋“ค์–ด๋‚˜๊ฐˆ ๊ฒƒ์ด๋ฉฐ ์ฐจ๊ทผ์ฐจ๊ทผ ๋”ฐ๋ผ๊ฐ€ ๋ณด๋„๋ก ํ•˜์ž.

 


2. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ๊ธฐ๋ณธ ๊ธฐ๋Šฅ ๊ตฌํ˜„

public class AccountingApp {

	public static void main(String[] args) {
		
		System.out.println("Value of supply : " + 12345.0); // ๊ณต๊ธ‰๊ฐ€
		System.out.println("VAT : " + (12345.0 * 0.1)); // ๋ถ€๊ฐ€์„ธ
		System.out.println("Total : " + (12345.0 + 12345.0 * 0.1)); // ๊ณต๊ธ‰๊ฐ€ + ๋ถ€๊ฐ€์„ธ 
		System.out.println("Expense : " + (12345.0*0.3)); // ๋น„์šฉ 
		System.out.println("Income : " + (12345.0 - 12345.0*0.3)); // ์ด์ต 
		System.out.println("Dividend 1 : " + (12345.0 - 12345.0*0.3) * 0.5 ); // ๋ฐฐ๋‹น1
		System.out.println("Dividend 2 : " + (12345.0 - 12345.0*0.3) * 0.3 ); // ๋ฐฐ๋‹น2
		System.out.println("Dividend 3 : " + (12345.0 - 12345.0*0.3) * 0.2 ); // ๋ฐฐ๋‹น3
	}

}

๊ฐ€์žฅ๋จผ์ € ํ•ด์•ผํ• ์ผ์€ ์–ด๋ ต๊ณ  ๊ตฌ์ฒด์ ์ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์ž„์˜์˜ ๊ฐ’์„ ์ง€์ •ํ•ด์„œ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ๊ฐ’๋“ค์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

 

 


3. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ๋ณ€์ˆ˜๋„์ž…

public class AccountingApp {

	public static void main(String[] args) {
		
		double valueOfSupply = 10000.0;
		double vatRate = 0.1;
		double expenseRate = 0.3;
		double vat = valueOfSupply * vatRate;
		double total = valueOfSupply + vat;
		double expense = valueOfSupply*expenseRate;
		double income = valueOfSupply - expense;
		double dividend1 = income * 0.5;
		double dividend2 = income * 0.3;
		double dividend3 = income * 0.2;
		
		System.out.println("Value of supply : " + valueOfSupply); // ๊ณต๊ธ‰๊ฐ€
		System.out.println("VAT : " + vat); // ๋ถ€๊ฐ€์„ธ
		System.out.println("Total : " + total); // ๊ณต๊ธ‰๊ฐ€ + ๋ถ€๊ฐ€์„ธ 
		System.out.println("Expense : " + expense); // ๋น„์šฉ 
		System.out.println("Income : " + income); // ์ด์ต 
		System.out.println("Dividend 1 : " + dividend1 ); // ๋ฐฐ๋‹น1
		System.out.println("Dividend 2 : " + dividend2 ); // ๋ฐฐ๋‹น2
		System.out.println("Dividend 3 : " + dividend3 ); // ๋ฐฐ๋‹น3
	}

}

๊ธฐ์กด์— ๋‹จ์ˆœํžˆ ์ˆ˜์น˜๋กœ๋งŒ ์ ํ˜€์ ธ์žˆ์–ด์„œ ์–ด๋– ํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์–ด๋ ค์› ์ง€๋งŒ ๊ฐ๊ฐ์˜ ๋ฐ์ดํ„ฐ๋“ค์—๊ฒŒ ์–ด๋– ํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์ด๋ฆ„์„ ๋ถ™์—ฌ์คŒ์œผ๋กœ์จ ํ•œ๋ˆˆ์— ๋ณด๊ธฐ ๋” ํŽธํ•œ ์ฝ”๋“œ๊ฐ€ ๋˜์—ˆ๋‹ค.

 

๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ธฐ์กด์˜ ๊ณต๊ธ‰๊ฐ’์„ ์ž…๋ ฅํ•  ๋•Œ ๋งค๋ฒˆ ์ˆ˜์ •ํ•ด์ฃผ์–ด์•ผ ํ–ˆ๋Š”๋ฐ ์ด์ œ๋Š” ๋ณ€์ˆ˜ valueOfSupply์•ˆ์— ์žˆ๋Š” ๊ฐ’๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋”์šฑ ํŽธ๋ฆฌํ•ด์กŒ๋‹ค.

 

๋งŒ์•ฝ ํŠน์ •๊ฐ’์„ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋กœ ๋งŒ๋“ค์–ด ๋ฒ„๋ฆฌ๊ณ  ์‹ถ๋‹ค๋ฉด ๋ณ€์ˆ˜๋กœ ๋Œ€์ฒดํ•˜๊ธฐ๋ฅผ ์›ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋“œ๋ž˜๊ทธ๋ฅผ ํ•œ๋’ค Extract Local Variable๋ฅผ ๋ˆ„๋ฅด๊ฑฐ๋‚˜ option + command + L ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ๋ณ€์ˆ˜๋ช…์„ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 


4. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ์ž…๋ ฅ๊ฐ’ ๋„์ž…

์ž…๋ ฅ๊ฐ’์„ ์ฃผ๋ฉด ๊ทธ์— ๋”ฐ๋ผ์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ถœ๋ ฅ๊ฐ’์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์•ฑ์„ ๋งŒ๋“ค์–ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

๊ทธ์ „์— ์šฐ๋ฆฌ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ๋ฐฐ์› ๊ธฐ ๋•Œ๋ฌธ์— ๋งค๋ณ€์ˆ˜์ธ args๊ฐ’์•ˆ์— ์–ด๋– ํ•œ ๊ฐ’์„ ๋„ฃ์„ ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด Runํ•˜๊ธฐ ์ „์— ๋ฏธ๋ฆฌ Arguments์— ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.

 

public class AccountingApp {

	public static void main(String[] args) {
		// Double.parseDouble(์‹ค์ˆ˜ํ˜•์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ์€ ๋ฌธ์žํ˜•์œผ๋กœ ๋˜์–ด์žˆ๋Š” ์ˆซ์ž);
		double valueOfSupply = Double.parseDouble(args[0]);
		double vatRate = 0.1;
		double expenseRate = 0.3;
		double vat = valueOfSupply * vatRate;
		double total = valueOfSupply + vat;
		double expense = valueOfSupply*expenseRate;
		double income = valueOfSupply - expense;
		double dividend1 = income * 0.5;
		double dividend2 = income * 0.3;
		double dividend3 = income * 0.2;
		
		System.out.println("Value of supply : " + valueOfSupply); // ๊ณต๊ธ‰๊ฐ€
		System.out.println("VAT : " + vat); // ๋ถ€๊ฐ€์„ธ
		System.out.println("Total : " + total); // ๊ณต๊ธ‰๊ฐ€ + ๋ถ€๊ฐ€์„ธ 
		System.out.println("Expense : " + expense); // ๋น„์šฉ 
		System.out.println("Income : " + income); // ์ด์ต 
		System.out.println("Dividend 1 : " + dividend1 ); // ๋ฐฐ๋‹น1
		System.out.println("Dividend 2 : " + dividend2 ); // ๋ฐฐ๋‹น2
		System.out.println("Dividend 3 : " + dividend3 ); // ๋ฐฐ๋‹น3
	}

}

์ด์ œ valueOfSupply์— ํŠน์ •๊ฐ’ ๋Œ€์‹  ๋งค๊ฐœ๋ณ€์ˆ˜ args[0]์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ ์ด๋•Œ ์šฐ๋ฆฌ๊ฐ€ ๋ฐ›์•„์˜จ args๋Š” ๋ฌธ์žํ˜•์ด๊ณ  valueOfSupply๋Š” ์‹ค์ˆ˜ํ˜•์œผ๋กœ ๋ฐ›์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— parseDouble๋กœ ๊ฐ’์„ ๋ฐ›์•„์ฃผ๋ฉด ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ„ฐ๋ฏธ๋„์„ ์ด์šฉํ•ด์„œ๋„ ๊ฒฐ๊ณผํ™”๋ฉด์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜๊ธธ ๋ฐ”๋ž€๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€