๐Ÿงฉ ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ์— ๋”ฐ๋ผ ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ

Q1 split()๊ณผ parsing์„ ์‚ฌ์šฉํ•˜์—ฌ String ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“  ํ›„, ํ•ฉ๊ณผ ํ‰๊ท  ๊ณ„์‚ฐํ•˜๊ธฐ

๐Ÿ’โ€ ์ฃผ์–ด์ง„ String ๋ฐ์ดํ„ฐ๋ฅผ โ€œ,โ€๋กœ ๋‚˜๋ˆ„์–ด 5๊ฐœ์˜ ์‹ค์ˆ˜ ๋ฐ์ดํ„ฐ๋“ค์„ ๊บผ๋‚ด์–ด ํ•ฉ๊ณผ ํ‰๊ท ์„ ๊ตฌํ•˜์„ธ์š”. ๋‹จ, String ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ์‹ค์ˆ˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

โ—ผ StringTest Class

String str = "1.22, 4.12, 5.93, 8.71, 9.34";
		
double data[] = new double[5];
double sum = 0;
double avg = 0;
		
// str์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌ
String[] st = str.split(",");
		
int i = 0;
// for-each๋ฌธ ์‚ฌ์šฉ
for(String s : st) {
	data[i] = Double.parseDouble(s);
	sum += data[i++];
	avg = sum/(st.length);
}
System.out.printf("ํ•ฉ๊ณ„ : %.3f\n", sum); // ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ์…‹์งธ์ž๋ฆฌ๊นŒ์ง€ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด printf์™€ %f ์‚ฌ์šฉ
System.out.println("ํ‰๊ท  : " + avg);

๐Ÿ’ฌ Overall Comment

* Java ๋ฌธ์ œ๋ฅผ ์˜ค๋žœ๋งŒ์— ํ’€์–ด๋ด์„œ ๊ทธ๋Ÿฐ์ง€ ์ฒ˜์Œ์— ์–ด๋–ค ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•ด์•ผํ• ์ง€ ์ฃผ์ €ํ–ˆ์œผ๋‚˜ 
ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ฝ์œผ๋‹ˆ ๊ฐ์ด ์žกํ˜”๋‹ค. ํ‰์†Œ์— ๋ฐฐ์—ด ๋ถ€๋ถ„์ด ์•ฝํ•ด์„œ ๋ฐฐ์—ด ์˜ˆ์ œ๋ฅผ 
๋งŽ์ด ํ’€์–ด๋ด์•ผํ•˜๋Š”๋ฐ ์˜ค๋žœ๋งŒ์— split(), parsing, ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋‹ˆ ํ•™์Šต์ด ๋˜์ƒˆ๊น€๋˜๋Š” ๋Š๋‚Œ์ด์—ˆ๋‹ค.

Q2 GregorianCalendar ํด๋ž˜์Šค์™€ SimpleDateFormat ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์ผ, ํ˜„์žฌ, ๋‚˜์ด ๊ตฌํ•˜๊ธฐ

๐Ÿ’โ€ GregorianCalendar ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ํ˜„์žฌ ๋…„๋„์™€ ๋น„๊ตํ•œ ๋‚˜์ด๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ์ƒ์ผ์˜ ์š”์ผ์„ ์ถœ๋ ฅํ•˜์„ธ์š”. ์ถœ๋ ฅ์‹œ SimpleDateFormat์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•˜์„ธ์š”.

โ—ผ DataTest Class

Calendar gre = new GregorianCalendar(); 
		
/* ํ˜„์žฌ */
/* ์•„๋ž˜์˜ ์ƒ์ผ์—์„œ ๋‚ ์งœ๋ฅผ setํ•  ์˜ˆ์ •์ด๋ฏ€๋กœ, ํ˜„์žฌ๋ฅผ ๋จผ์ € ์ž‘์„ฑํ•˜์—ฌ ๊ฐ’ ์ถ”์ถœ */
long tDay = gre.getTimeInMillis(); // ์ถœ๋ ฅํ•œ ์‹œ๊ฐ„(ํ˜„์žฌ)
		
// ๋งŒ ๋‚˜์ด๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ํ˜„์žฌ์˜ ๋…„, ์›”, ์ผ ์ถ”์ถœ
int tYear = gre.get(Calendar.YEAR);
int tMonth = gre.get(Calendar.MONTH) + 1;
int tDate = gre.get(Calendar.DATE);
		
/* ์ƒ์ผ */
int bYear = 1987;
int bMonth = 5-1; // month๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ, '์›ํ•˜๋Š” ์›” - 1'
int bDate = 27;
		
gre.set(bYear, bMonth, bDate);
long bDay = gre.getTimeInMillis(); // setํ•œ ์‹œ๊ฐ„(์ƒ์ผ)
int dayOfWeek = gre.get(Calendar.DAY_OF_WEEK); // setํ•œ ์š”์ผ(์ƒ์ผ)
		
String day = "";
switch(dayOfWeek) {
case 1 : day = "์ผ"; break;
case 2 : day = "์›”"; break;
case 3 : day = "ํ™”"; break;
case 4 : day = "์ˆ˜"; break;
case 5 : day = "๋ชฉ"; break;
case 6 : day = "๊ธˆ"; break;
case 7 : day = "ํ† "; break;
}
		
/* ์ถœ๋ ฅ */
SimpleDateFormat sdf = new SimpleDateFormat("yyyy๋…„ MM์›” dd์ผ "); // ์›ํ•˜๋Š” ํฌ๋งท์œผ๋กœ ๋ฌธ์ž์—ด ์„ค์ •
String birthday = sdf.format(bDay);
String today = sdf.format(tDay);
		
System.out.println("์ƒ์ผ : " + birthday + day + "์š”์ผ");
System.out.println("ํ˜„์žฌ : " + today);
		
/* ๋งŒ ๋‚˜์ด */
int age = tYear - bYear;
if(bMonth*100 + bDate > tMonth*100 + tDate)
	age--;
System.out.println("๋‚˜์ด : ๋งŒ " + age + " ์„ธ");

๐Ÿ’ฌ Overall Comment

* GregorianCalendar์™€ SimpleDateFormat์„ ํ™œ์šฉํ•˜์—ฌ ํ’€์–ด๋‚ด๋ คํ•˜๋‹ˆ ๊ฝค ์ƒ๊ฐ์„ ๋งŽ์ด ํ•ด์•ผํ•˜๋Š” 
  ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋‹ค. ๋งŽ์ด ์‚ฌ์šฉํ•ด๋ณด์ง€ ๋ชป ํ•œ ํด๋ž˜์Šค๋“ค์ด๋ผ ์ •๋ฆฌํ•ด๋‘” ๊ฒƒ์„ ๊ผผ๊ผผํžˆ ์ฐพ์•„๊ฐ€๋ฉฐ ํ•ด์•ผํ–ˆ๋‹ค. 
  ์•„์ง ๋จธ๋ฆฟ์†์— ์ •๋ง ๋ฌด์ˆ˜ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์ด ์ •๋ฆฌ๋˜์ง€ ์•Š์•„์„œ ์• ๋ฅผ ๊ฝค ๋จน์—ˆ์ง€๋งŒ ์ž˜ ์ •๋ฆฌ๋œ ์ฝ”๋“œ๊ฐ€ 
  ์™„์„ฑ๋˜๋‹ˆ ๊ท€ํ•œ ์žฌ์‚ฐ์ด ์ƒ๊ธด ๋Š๋‚Œ์ด๊ณ  ๋งค์šฐ ๋ฟŒ๋“ฏํ–ˆ๋‹ค.

Q3 ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ™œ์šฉํ•œ 1๋ถ€ํ„ฐ ์ž…๋ ฅ ๋ฐ›์€ ์ˆ˜๊นŒ์ง€์˜ ํ•ฉ ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ’โ€ 2๋ถ€ํ„ฐ 5๊นŒ์ง€์˜ ์ •์ˆ˜ํ˜• ๋ฐ์ดํ„ฐ๋งŒ์„ ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅ ๋ฐ›์•„ 1๋ถ€ํ„ฐ ์ž…๋ ฅ ๋ฐ›์€ ์ˆ˜๊นŒ์ง€์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•˜์„ธ์š”. ๋‹จ, ์ž…๋ ฅ ๋ฐ›์€ ์ˆ˜๊ฐ€ 2๋ถ€ํ„ฐ 5๊นŒ์ง€์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด โ€œ์ž…๋ ฅ ๊ฐ’์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.โ€๋ผ๊ณ  ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

โ—ผ Calculator Class

public class Calculator {

	public double getSum(int data) throws InvalidException {
		
		int sum = 0;
		
		if(data > 1 && data < 6) {
			for(int i = 0; i <= data; i++) {
				sum += i;
			}
		} else {
			throw new InvalidException("์ž…๋ ฅ ๊ฐ’์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.");
		} 
		return sum;	
	} 
}

โ—ผ ExceptionTest Class

public class ExceptionTest {

	public static void main(String[] args) {
		
		Calculator c = new Calculator();
		
		Scanner sc = new Scanner(System.in);

		try {
			System.out.println("< 1๋ถ€ํ„ฐ ์ž…๋ ฅ ๋ฐ›์€ ์ˆ˜๊นŒ์ง€์˜ ํ•ฉ ์ถœ๋ ฅํ•˜๊ธฐ >");
			System.out.println("---------------------------------");			
			System.out.print("2๋ถ€ํ„ฐ 5๊นŒ์ง€์˜ ์ •์ˆ˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ");
			int num = sc.nextInt();
			System.out.println("๊ฒฐ๊ณผ๊ฐ’ : " + c.getSum(num));

		} catch (InvalidException e) {
			e.printStackTrace(); 
		}
	}
}

โ—ผ InvalidException Class

public class InvalidException extends Exception {

	public InvalidException(String message) {
		super(message);
	}
}

๐Ÿ’ฌ Overall Comment

* ํ•ญ์ƒ ์™„๋ฒฝํžˆ ๊ธฐ์–ตํ•˜์ง€ ๋ชป ํ•˜๋Š” ๋ถ€๋ถ„๋“ค์€ ์–ธ์  ๊ฐ€ ๋ฌธ์ œ๋กœ ๋‹ค๊ฐ€์™€์„œ ๋‚˜์˜ ๋’ทํ†ต์ˆ˜๋ฅผ ์ฝฉ- ์น˜๋Š” ๊ฒƒ 
  ๊ฐ™๋‹ค. ์˜ˆ์™ธ์ฒ˜๋ฆฌ์˜ ํ๋ฆ„์„ ์ดํ•ดํ•˜๊ณ  ๊ธฐ์–ตํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์œผ๋‚˜ ์ดํ•ด๋งŒ ํ–ˆ๋˜ ๋‚ด ์ž์‹  ๋ฐ˜์„ฑํ•ด์•ผํ•œ๋‹ค. 
  ๊ฒฐ๊ตญ ๋˜ ์ •๋ฆฌํ•ด๋‘” ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํฌ์ŠคํŠธ๋ฅผ ์ฐพ์•„๋ณด๋ฉฐ ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์•„์ง์€ ๊ธฐ์–ต๋ณด๋‹ค 
  ๊ธฐ๋ก์— ์˜์กดํ•ด์•ผํ•˜๋Š” ์ƒˆ์‹น๋‹จ๊ณ„์ด์ง€๋งŒ ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ฉฐ ๊ณ„์† ๊ธฐ์–ต์„ ๊ณฑ์”น์–ด์ค€๋‹ค๋ฉด ํ›—๋‚  
  ์ฐพ์•„๋ณด์ง€์•Š๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋‚ ์ด ์˜ฌ ๊ฒƒ์ด๋ผ ๋ฏฟ๋Š”๋‹ค :)

Q4 ์ƒ์†์„ ํ™œ์šฉํ•˜์—ฌ ์˜ค๋ฒ„๋ผ์ด๋”ฉ๋œ ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿ’โ€ Abstract ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›์€ ๋‘ ๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์•„๋ž˜ 5๋ฒˆ ํ•ญ๋ชฉ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

โ—ผ Plane Abstract Class

public abstract class Plane {
	
	/* ํ•„๋“œ */
	private String planeName;
	private int fuelSize;
	
	/* ์ƒ์„ฑ์ž */
	public Plane() {}

	public Plane(String planeName, int fuelSize) {
		super();
		this.planeName = planeName;
		this.fuelSize = fuelSize;
	}
	
	/* getter & setter */
	public String getPlaneName() {
		return planeName;
	}

	public void setPlaneName(String planeName) {
		this.planeName = planeName;
	}

	public int getFuelSize() {
		return fuelSize;
	}

	public void setFuelSize(int fuelSize) {
		this.fuelSize = fuelSize;
	}
	
	/* ๋ฉ”์†Œ๋“œ */
	public void refuel(int fuel) { // ์ฃผ์œ 
		fuelSize += fuel;
	}
	
	public abstract void flight(int distance); // ์šดํ•ญ


	public void print() { // ์ถœ๋ ฅ์šฉ
		System.out.println(planeName + "\t\t" + fuelSize);
	}
}

โ—ผ Airplane Class

public class Airplane extends Plane {

	public Airplane() {}
	
	public Airplane(String planeName, int fuelSize) {
		super(planeName, fuelSize);
	}
	
	@Override
	public void flight(int distance) {
		setFuelSize(getFuelSize() - (distance * 3)); 
	}
}

โ—ผ Cargoplane Class

public class Cargoplane extends Plane {

	public Cargoplane() {}
	
	public Cargoplane(String planeName, int fuelSize) {
		super(planeName, fuelSize);
	}
	
	@Override
	public void flight(int distance) {
		setFuelSize(getFuelSize() - (distance * 5)); 
	}
}

โ—ผ PlaneTest Class

public class PlaneTest {

	public static void main(String[] args) {
		
		/* Airplane & Cargoplane ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์ธ์ž์ „๋‹ฌ */
		Plane air = new Airplane("L747", 1000);
		Plane cargo = new Cargoplane("C40", 1000);
		
		/* ๊ฐ์ฒด ์ •๋ณด ์ถœ๋ ฅ */
		System.out.println("Plane\t\tfuelSize");
		System.out.println("--------------------------");
		air.print();
		cargo.print();
		
		/* 100 ์šดํ•ญ */
		air.flight(100);
		cargo.flight(100);
		
		System.out.println("100 ์šดํ•ญ");
		System.out.println("Plane\t\tfuelSize");
		System.out.println("--------------------------");
		air.print();
		cargo.print();
		
		/* 200 ์ฃผ์œ  */
		air.refuel(200);
		cargo.refuel(200);
		
		System.out.println("200 ์ฃผ์œ ");
		System.out.println("Plane\t\tfuelSize");
		System.out.println("--------------------------");
		air.print();
		cargo.print();
	}
}

๐Ÿ’ฌ Overall Comment

* ๋‚ด๊ฐ€ ์ข‹์•„ํ•˜๋Š” ํŒŒํŠธ์ธ '์ƒ์†'ํŒŒํŠธ. ํ•˜์ง€๋งŒ ์ด ๋ฌธ์ œ ๋˜ํ•œ ํ˜ธ๋ฝํ˜ธ๋ฝํ•˜์ง€์•Š์•˜๋‹ค. 
  ์•„๋งˆ๋„ ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ์ ‘ํ•ด์„œ์ธ์ง€ ๋ชฐ๋ผ๋„ ์ผ๋‹จ ๋ฌธ์ œ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํŒŒ์•…ํ•˜๋Š”๋ฐ 
  ์‹œ๊ฐ„์ด ๊ฝค ์†Œ์š”๋˜์—ˆ๋‹ค. ์ธ์ž๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ฒจ์ฃผ์–ด ๊ฐ๊ฐ ์„œ๋ธŒํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ฒŒ๋” 
  ํ•ด์•ผํ•˜๋Š” ๋ฌธํ•ญ์ด์—ˆ๋‹ค. ์ดํ•ดํ•˜๋Š”๋ฐ์— ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆฐ ๋“ฏํ•˜๋‚˜ ๋งŒ์กฑ์Šค๋Ÿฌ์šด ์ฝ”๋“œ๊ฐ€ ์™„์„ฑ๋œ ๊ฒƒ ๊ฐ™์•„ ๋ฟŒ๋“ฏํ•˜๋‹ค.

Q5 ์ƒ์†๊ณผ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ™œ์šฉํ•œ ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿ’โ€ Abstract ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›๊ณ  Interface๋ฅผ ๊ตฌํ˜„ํ•œ ๋‘ ๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ํ•ญ๋ชฉ 5๋ฒˆ์˜ ์‹คํ–‰๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

โ—ผ Bonus Interface

public interface Bonus {

	public void incentive(int pay);
}

โ—ผ Employee Abstract Class

public abstract class Employee {
	
	/* ํ•„๋“œ */
	private String name;
	private int number;
	private String department; // ๋ถ€์„œ
	protected int salary; // private int salary๋กœ ์–ด๋–ป๊ฒŒ ํ’€์ฃ ..
	
	/* ์ƒ์„ฑ์ž */
	public Employee() {}

	public Employee(String name, int number, String department, int salary) {
		super();
		this.name = name;
		this.number = number;
		this.department = department;
		this.salary = salary;
	}

	/* getter & setter */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}
	
	/* ๋ฉ”์†Œ๋“œ */
	public double tax() { // ์„ธ๊ธˆ
		
		return salary * 0.0;
	}
}

โ—ผ Sales Class

public class Sales extends Employee implements Bonus {

	public Sales() {}
	
	public Sales(String name, int number, String department, int salary) {
		super(name, number, department, salary);
	}

	public double tax() { // ์„ธ๊ธˆ
		
		return salary * 0.13;
	}
	
	@Override
	public void incentive(int pay) { // ์ธ์„ผํ‹ฐ๋ธŒ ์ง€๊ธ‰
		salary += (int) (pay * 1.2);
	}
}

โ—ผ Secretary Class

public class Secretary extends Employee implements Bonus {

	public Secretary() {}
	
	public Secretary(String name, int number, String department, int salary) {
		super(name, number, department, salary);
	}

	public double tax() { // ์„ธ๊ธˆ
		
		return salary * 0.1;
	} 
	
	@Override
	public void incentive(int pay) { // ์ธ์„ผํ‹ฐ๋ธŒ ์ง€๊ธ‰
		salary += (int) (pay * 0.8);
	}
}

โ—ผ Company Class

public class Company {

	public static void main(String[] args) {
		
		/* ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ๋ฐฐ์—ด์— ๋„ฃ๊ธฐ */
		Employee[] employees = new Employee[2];
		employees[0] = new Secretary("Hilery", 1, "secretary", 800);
		employees[1] = new Sales("Clinten", 2, "sales     ", 1200);
		
		/* ๊ธฐ๋ณธ ์ •๋ณด ์ถœ๋ ฅ */
		System.out.println("name\t department\tsalary");
		System.out.println("-------------------------------");
		
		for(int i = 0; i < employees.length; i++)
			System.out.println(employees[i].getName() +"\t "+ employees[i].getDepartment() +"\t"+ employees[i].getSalary());

		
		/* ์ธ์„ผํ‹ฐ๋ธŒ 100 ์ง€๊ธ‰ ํ›„ ๋‹ค์‹œ ๊ฐ์ฒด์˜ salary์— ๋„ฃ๊ธฐ */
		((Secretary)employees[0]).incentive(100);
		((Sales)employees[1]).incentive(100);		
		
		System.out.println();
		System.out.println("์ธ์„ผํ‹ฐ๋ธŒ 100 ์ง€๊ธ‰");
		System.out.println("name\t department\tsalary\t   tax");
		System.out.println("---------------------------------------");
		
		for(int i = 0; i < employees.length; i++)
			System.out.println(employees[i].getName() +"\t "+ employees[i].getDepartment() +"\t"+ employees[i].getSalary() +"\t "+ employees[i].tax());
	}
}

๐Ÿ’ฌ Overall Comment

* ๋‚˜์˜ ์•„ํ”ˆ ์†๊ฐ€๋ฝ์ธ '๋ฐฐ์—ด'์ด ํฌํ•จ๋œ ์ƒ์† ํŒŒํŠธ ๋ฌธํ•ญ์ด์—ˆ๋‹ค. ๊ทธ๋ž˜๋„ ๋ฌธ์ œ๋ฅผ ๊ณ„์† ํ’€๋‹ค๋ณด๋‹ˆ 
  ์ดํ•ด๋ ฅ๋„ ๋Š˜์–ด๋‚˜ ๊ณง์ž˜ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•œ ๊ฐ€์ง€ ์• ๋กœ์‚ฌํ•ญ์ด ์žˆ์—ˆ๋Š”๋ฐ, ๊ฐ ์„œ๋ธŒํด๋ž˜์Šค์˜ 
  ํ•„๋“œ์— salary๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒƒ๊ณผ ์•„์˜ˆ ์ˆ˜ํผํด๋ž˜์Šค์—์„œ salary๋ฅผ private๊ฐ€ ์•„๋‹Œ protected๋กœ 
  ์„ค์ •ํ•˜๋Š” ๊ฒƒ. ๋‘˜ ์ค‘์— ํ•œ ๊ฐ€์ง€๋ฅผ ๊ณ ๋ คํ•ด์•ผ๋งŒ ํ–ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๊ณ ์„œ ์„œ๋ธŒ ํด๋ž˜์Šค๋“ค์—์„œ salary๋ฅผ 
  ๊ฐ€์ง€๊ณ  ์„ธ๊ธˆ๊ณผ ์ธ์„ผํ‹ฐ๋ธŒ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†์—ˆ๋‹ค. ๋‚˜๋Š” ๊ฒฐ๊ตญ private๊ฐ€ ์•„๋‹Œ protected๋กœ 
  ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ์•ˆ์„ ์„ ํƒํ–ˆ๋Š”๋ฐ, ๋‚ด์ผ ์„ ์ƒ๋‹˜์˜ ํ•ด์„ค ์ฝ”๋“œ๊ฐ€ ์˜ฌ๋ผ์˜ค๋ฉด ๊ผญ ์ฐธ๊ณ ํ•˜๋„๋ก ํ•ด์•ผ๊ฒ ๋‹ค. 

Q6 ArrayList๋ฅผ ํ™œ์šฉํ•œ ๋žœ๋ค ์ˆซ์ž ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ’โ€ java.util.ArrayList ์— ๊ฐ’๋“ค์„ ์ €์žฅํ•˜๊ณ , ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ๋˜๊ฒŒ ํ•˜์„ธ์š”.

โ—ผ Descending Class

public class Descending implements Comparator<Integer> {

	@Override
	public int compare(Integer o1, Integer o2) {
		int result = 0;
		
		if(o1 > o2) {
			result = -1;
            // ๋‚ด๋ฆผ์ฐจ์ˆœ์„ ์œ„ํ•ด ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์–‘์ˆ˜ ๋ฐ˜ํ™˜
            
		} else if(o1 < o2) {
			result = 1;
            // ์ด๋ฏธ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์Œ์ˆ˜ ๋ฐ˜ํ™˜
            
		} else {
			result = 0;
            // ๋‘ ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ 0์„ ๋ฐ˜ํ™˜
		}
		return result;
	}
}

โ—ผ ListTest Class

public class ListTest {

	public static void main(String[] args) {
		
		List<Integer> list = new ArrayList<>();
		Random random = new Random();
		
		// ๋ช…์‹œํ•œ ์‚ฌ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ list์— ๊ธฐ๋ก
		for(int i = 0; i < 10; i++) {
			int randomNum = random.nextInt(100) + 1;
			if(!list.contains(randomNum)) // ์ค‘๋ณต๊ฐ’ ์ œ๊ฑฐ
			list.add(randomNum);
		}
		
		// display ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
		ListTest dis = new ListTest();
		System.out.print("์ •๋ ฌ ์ „ : ");
		dis.display(list);
		
		// list์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
		Collections.sort(list, new Descending()); 
		
		// display ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
		System.out.print("์ •๋ ฌ ํ›„ : ");
		dis.display(list);
	}
	
	public void display(List list) {

		System.out.println(list);
	}
}

๐Ÿ’ฌ Overall Comment

* ArrayList์™€ random์„ ์‚ฌ์šฉํ•˜์—ฌ 1~100 ์ค‘ ๋žœ๋ค ์ˆซ์ž 10๊ฐœ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ฌธํ•ญ์ด์—ˆ๋Š”๋ฐ, 
  ๋ฐฐ์—ด์˜ ํ˜•์ œ ArrayList๊ฐ€ ์žˆ์œผ๋‹ˆ ๊ผญ ์ง‘์ค‘ํ•ด์„œ ํ’€์–ด์•ผํ–ˆ๋‹ค. ๋˜ํ•œ ArrayList์—๋Š” ๋‹ค์–‘ํ•œ 
  ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ํ–ˆ๊ณ , ์ •๋ ฌ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผํ–ˆ๋‹ค. 
  ๋ฌธ์ œ์—๋Š” ์—†์—ˆ์œผ๋‚˜ ์ค‘๋ณต๊ฐ’๋„ ์ œ๊ฑฐํ•˜๊ณ ์‹ถ์–ด ํ•œ์ฐธ ์ฐพ์•„๋ณด๋˜ ์™€์ค‘ contains()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 
  List์˜ ์ค‘๋ณต๊ฐ’์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋‚ด์—ˆ๋‹ค! ์‹ ์ด ๋‚˜์„œ ์ฝ”๋“œ๋ฅผ ์—ด์‹ฌํžˆ ๋งŒ๋“ค์–ด๋ณด๊ณ  
  ์ˆ˜์ •ํ•˜๋‹ˆ ๋งŒ์กฑ์Šค๋Ÿฌ์šด ๊ฒฐ๊ณผ๋ฌผ์ด ๋‚˜์˜จ ๋“ฏ ํ•˜์—ฌ ๋ฟŒ๋“ฏํ–ˆ๋‹ค :) 

Q7 Properties์™€ .xmlํŒŒ์ผ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋‹ค์‹œ ๋ฐฐ์—ด์— ๊ธฐ๋ก ๋ฐ ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ’โ€ java.util.Properties ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, data.xml ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ก ์ €์žฅํ•œ ๋‹ค์Œ ํŒŒ์ผ์— ๊ธฐ๋ก๋œ ๋ฐ์ดํ„ฐ๋“ค์„ ์ฝ์–ด์™€์„œ Fruit[] ์— ๊ธฐ๋กํ•˜๊ณ  Fruit[]์˜ ๊ฐ’๋“ค์„ ํ™”๋ฉด์— ์ถœ๋ ฅ ์ฒ˜๋ฆฌํ•˜์„ธ์š”.

โ—ผ Fruit Class

public class Fruit {
	private String name;
	private int price;
	private int quantity;
	
/* ๊ธฐ๋ณธ ์ƒ์„ฑ์ž, ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž */

/* getter & setter */

/* toString */

โ—ผ PropTest Class

public class PropTest {
	
	public static void main(String[] args) {
		Properties p = new Properties();
		
		//๋ช…์‹œํ•œ ์‚ฌ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ prop ์— ๊ธฐ๋ก
		p.setProperty("1", "apple,1200,3");
		p.setProperty("2", "banana,2500,2");
		p.setProperty("3", "grape,4500,5");
		p.setProperty("4", "orange,800,10");
		p.setProperty("5", "melon,5000,2");
		
		// fileSave() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ
		new PropTest().fileSave(p);
		// fileOpen() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ
		new PropTest().fileOpen(p);
		
	}

	// fileSave() ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„
	public void fileSave(Properties p) {
		try {
			p.storeToXML(new FileOutputStream("data.xml"), "test");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// fileOpen() ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„
	public void fileOpen(Properties p) {
		try {
			p.loadFromXML(new FileInputStream("data.xml"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		// data.xmlํŒŒ์ผ์˜ ํ‚ค๋“ค๋งŒ set์œผ๋กœ ๋ฌถ์€ ๊ฒƒ์„ Iterator๋กœ ํ•˜๋‚˜์”ฉ ์ ‘๊ทผ
		Iterator<Object> it = p.keySet().iterator();
		ArrayList<String> keyList = new ArrayList<>();
		
		while(it.hasNext()) { // ๊ฐ’์ด ์žˆ์„ ๋•Œ(true)๊นŒ์ง€ ์ ‘๊ทผํ•˜์—ฌ keyList์— ํ•˜๋‚˜์”ฉ String์œผ๋กœ ๋‹ด๊ธฐ
			keyList.add((String)it.next());
		}
		
		Collections.sort(keyList); // Collections.sort ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ ์‹œ, list๊ฐ€ ์ •๋ ฌ๋œ ๋’ค ํ•ด๋‹น ์ƒํƒœ๊ฐ€ ์œ ์ง€
		
		// Fruit ๋ฐฐ์—ด์— ๋‹ด์•„ ์ •๋ ฌ๋œ KeyList์—์„œ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๋ฝ‘์•„๋‚ด๊ณ  split, parsingํ•˜์—ฌ ํ”„๋ฆฐํŠธ
		Fruit[] arr = new Fruit[p.size()];
		int idx = 0;
		for(String key : keyList) { // "apple,1200,3"
			String[] s =  p.getProperty(key).split(",");
			arr[idx] = new Fruit(s[0], Integer.parseInt(s[1]), Integer.parseInt(s[2]));
			System.out.println(key + " = " + arr[idx].getName() + ", " + 
			      arr[idx].getPrice() +"์›, " + arr[idx].getQuantity()+"๊ฐœ");
			idx++;
		}
	}
}

โ—ผ data.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="1">apple,1200,3</entry>
<entry key="2">banana,2500,2</entry>
<entry key="3">grape,4500,5</entry>
<entry key="4">orange,800,10</entry>
<entry key="5">melon,5000,2</entry>
</properties>

๐Ÿ’ฌ Overall Comment

* ์ด ๋ฌธํ•ญ์€ iterator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ List์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด๋‚ด์•ผํ–ˆ์œผ๋‚˜, ๊ณ„์† ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์„ฑ์„ 
๊ฐ€์ง€๊ณ  ์ ‘๊ทผํ•˜๋‹ค๋ณด๋‹ˆ ๋ฌธ์ œ๊ฐ€ ํ’€๋ฆฌ์ง€ ์•Š์•˜์—ˆ๋‹ค.
Property ํŒŒ์ผ์—์„œ ์ฝ์–ด์˜ค๋Š” ๊ณผ์ •์„ ๋ณด๋‹ค ๋” ํ•™์Šตํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ๋‹ค์งํ•˜๊ฒŒ ๋œ ๋ฌธํ•ญ์ด์—ˆ๋‹ค.

Q8 Map๊ณผ SimpleDateFormat์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋‹ค์‹œ ๋ฐฐ์—ด์— ๊ธฐ๋ก ๋ฐ ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ’โ€ java.util.Map ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํ’ˆ์žฌ๊ณ  ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์ถœ๋ ฅ๋˜๊ฒŒ ํ•˜์„ธ์š”. ์ถœ๋ ฅ์‹œ ๋‚ ์งœ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” SimpleDateFormat ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅ ์ฒ˜๋ฆฌํ•˜์„ธ์š”.

โ—ผ AmountNotEnough Class

public class AmountNotEnough extends Exception {
	
	public AmountNotEnough() {}

	public AmountNotEnough(String message) {
		super(message);
	}
}

โ—ผ Inventory Class

public class Inventory {

	/* ํ•„๋“œ */
	private String productName;	// ์ƒํ’ˆ๋ช…
	private Date putDate;		// ์ž…๊ณ ์ผ
	private Date getDate;		// ์ถœ๊ณ ์ผ
	private int putAmount;		// ์ž…๊ณ ์ˆ˜๋Ÿ‰
	private int getAmount;		// ์ถœ๊ณ ์ˆ˜๋Ÿ‰
	private int inventoryAmount;// ์žฌ๊ณ ์ˆ˜๋Ÿ‰
	
	/* ์ƒ์„ฑ์ž */
	public Inventory() {}

	public Inventory(String productName, Date putDate, int putAmount) {
		super();
		this.productName = productName;
		this.putDate = putDate;
		this.putAmount = putAmount;
		this.inventoryAmount = putAmount; // ์žฌ๊ณ ์ˆ˜๋Ÿ‰ ์ž„์˜๋กœ ์„ค์ •
	}

	/* getter & setter */
	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public Date getPutDate() {
		return putDate;
	}

	public void setPutDate(Date putDate) {
		this.putDate = putDate;
	}

	public Date getGetDate() {
		return getDate;
	}

	public void setGetDate(Date getDate) {
		this.getDate = getDate;
	}

	public int getPutAmount() {
		return putAmount;
	}

	public void setPutAmount(int putAmount) {
		this.putAmount = putAmount;
	}

	public int getGetAmount() {
		return getAmount;
	}

	public void setGetAmount(int getAmount) throws AmountNotEnough {
		this.getAmount = getAmount;
		if(inventoryAmount < getAmount) {
			throw new AmountNotEnough("ํ˜„์žฌ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. ์žฌ๊ณ ์ˆ˜๋Ÿ‰ ํ™•์ธํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.");
		}
		inventoryAmount -= getAmount;
	}
	

	public int getInventoryAmount() {
		return inventoryAmount;
	}

	public void setInventoryAmount(int inventoryAmount) {
		this.inventoryAmount = inventoryAmount;
	}

	@Override
	public String toString() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy๋…„ M์›” d์ผ");
		String putDate = this.putDate == null ? null + ", " : sdf.format(this.putDate) + " ์ž…๊ณ , ";
		String getDate = this.getDate == null ? null + ", ": sdf.format(this.getDate) + " ์ถœ๊ณ , "; 
		
		return productName + ", " + putDate + putAmount + "๊ฐœ, " 
		+ getDate + getAmount +"๊ฐœ, ์žฌ๊ณ  " + inventoryAmount + "๊ฐœ";
	}
}

โ—ผ MapTest Class

public class MapTest {

	public static void main(String[] args) {
		
		// Generics ์ ์šฉ๋œ ๋งต ๊ฐ์ฒด๋ฅผ ์„ ์–ธ ๋ฐ ํ• ๋‹น
		Map<String, Inventory> map = new HashMap<String, Inventory>();
		
		// ์ƒํ’ˆ๋ช…์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ €์žฅ ์ฒ˜๋ฆฌ
		map.put("์‚ผ์„ฑ ๊ฐค๋Ÿญ์‹œS7", new Inventory("์‚ผ์„ฑ ๊ฐค๋Ÿญ์‹œS7", new GregorianCalendar(2016, 3-1, 15).getTime(), 30));
		map.put("LG G5", new Inventory("LG G5", new GregorianCalendar(2016, 2-1, 25).getTime(), 20));
		map.put("์• ํ”Œ ์•„์ดํŒจ๋“œ Pro", new Inventory("์• ํ”Œ ์•„์ดํŒจ๋“œ Pro", new GregorianCalendar(2016, 1-1, 23).getTime(), 15));
		
		Inventory[] inventory = new Inventory[map.size()];
		int i = 0;
		
		// ๋งต์— ๊ธฐ๋ก๋œ ์ •๋ณด๋ฅผ ์—ฐ์† ์ถœ๋ ฅ. EntrySet() ์‚ฌ์šฉ
		// ๋งต์— ๊ธฐ๋ก๋œ ์ •๋ณด๋ฅผ Inventory[] ๋กœ ๋ณ€ํ™˜ํ•œ ๋‹ค์Œ,
		for(Entry<String, Inventory> e : map.entrySet()) {
			System.out.println(e.getValue());
			inventory[i++] = e.getValue();
		}
		
		// ์ถœ๊ณ  ๋‚ ์งœ๋ฅผ ์˜ค๋Š˜ ๋‚ ์งœ๋กœ, ์ถœ๊ณ  ์ˆ˜๋Ÿ‰์€ ๋ชจ๋‘ 10๊ฐœ๋กœ ์ง€์ •. ์˜ˆ์™ธ์ฒ˜๋ฆฌ
		// ๋ณ€๊ฒฝ๋œ Inventory[] ์˜ ์ •๋ณด๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅ
		System.out.println("์ถœ๊ณ  ์ˆ˜๋Ÿ‰ 10 ์ ์šฉ์‹œ-------------------------------------");
		for(Inventory iv : inventory) {
			
			try {
				iv.setGetDate(new Date());
				iv.setGetAmount(10);
				System.out.println(iv);
			} catch (AmountNotEnough e) {
				System.out.println(e.getMessage());
			}
			
		}
		
		System.out.println("์ถœ๊ณ ์ˆ˜๋Ÿ‰ ๋ถ€์กฑ์‹œ-------------------------------------");
		for(Inventory iv : inventory) {
			
			try {
				iv.setGetDate(new Date());
				iv.setGetAmount(25);
				System.out.println(iv);
			} catch (AmountNotEnough e) {
				System.out.println(e.getMessage());
			}
		}
	}
}

๐Ÿ’ฌ Overall Comment

* Map์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํ™œ์šฉํ•˜๋Š” ๋ฒ•์„ ๋ชฐ๋ผ์„œ ์ฒ˜์Œ์— ๋งŽ์ด ํ—ค๋งธ์œผ๋‚˜, ๋ ˆ์‹œํ”ผ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ’€์–ด๋‚ด๊ณ  Map์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์˜ ํ๋ฆ„์„ ์–ด๋Š์ •๋„ ํŒŒ์•…ํ•˜๊ฒŒ ํ•ด์ค€ ๋ฌดํ•ญ์ด์—ˆ๋‹ค. DTO๋ฅผ ๊ตฌ์„ฑํ•  ๋•Œ ์œ ์˜ํ•ด์•ผํ•  ๋ถ€๋ถ„์ด ๋งŽ์•„ ์–ด๋ ค์› ๋‹ค.

Q9 BufferedReader์™€ try-with-resource๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ž…์ถœ๋ ฅ์ฒ˜๋ฆฌํ•˜๊ธฐ

๐Ÿ’โ€ Good ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ณ , java.io.BufferedReader ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅ ๋ฐ›์„ ์ŠคํŠธ๋ฆผ์„ ์ƒ์„ฑ
ํ•˜๊ณ  ๊ฐ ํ•„๋“œ์— ๊ธฐ๋กํ•  ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์•„ ๊ฐ์ฒด ์ดˆ๊ธฐํ™”์— ์‚ฌ์šฉํ•œ๋‹ค.
๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐํ•˜์—ฌ ๊ตฌ๋งค๊ฐ€๊ฒฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

โ—ผ Goods Class

public class Goods {

	private String name;
	private int price;
	private int quantity;
	
	public Goods() {}

	public Goods(String name, int price, int quantity) {
		super();
		this.name = name;
		this.price = price;
		this.quantity = quantity;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	@Override
	public String toString() {
		return name + ", " + price + "์›, " + quantity + "๊ฐœ \n์ด ๊ตฌ๋งค ๊ฐ€๊ฒฉ : " + (price * quantity) + "์›";
	}
}

โ—ผ GoodsTest Class

public class GoodsTest {

	public static void main(String[] args) {
		
//		Goods goods = new Goods();
		
		// ์ž…์ถœ๋ ฅ (try-with-resource๋ฌธ)
		try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) {
			System.out.println("๋‹ค์Œ ํ•ญ๋ชฉ์˜ ๊ฐ’์„ ์ž…๋ ฅํ•˜์‹œ์˜ค.");
			System.out.print("์ƒํ’ˆ๋ช… : ");
			String name = br.readLine();
			System.out.print("๊ฐ€๊ฒฉ : ");
			int price = Integer.parseInt(br.readLine());
			System.out.print("์ˆ˜๋Ÿ‰ : ");
			int quantity = Integer.parseInt(br.readLine());
			
			System.out.println("์ž…๋ ฅ๋œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.");
			Goods goods = new Goods(name, price, quantity);
			System.out.println(goods.toString());
		
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
}

๐Ÿ’ฌ Overall Comment

* ์ž…์ถœ๋ ฅ์€ ์•„์ง ์‚ฌ์šฉํ•˜๊ธฐ์— ์–ด๋ ค์šด ์ ์ด ๋งŽ์•˜๋‹ค. ๊ทธ๋ž˜๋„ ์—ด์‹ฌํžˆ ์ •๋ฆฌํ•˜์—ฌ ํฌ์ŠคํŒ…ํ•ด๋‘” ๋•๋ถ„์— 
์ฐพ์•„๋ณด๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ด ๋ฌธํ•ญ์—์„œ๋Š” ํ‰์†Œ์— ์ •๋ง ์ž์ฃผ ์‚ฌ์šฉํ–ˆ๋˜ Scanner๊ฐ€ 
์•„๋‹Œ BufferReader์˜ readLine() ๋ฉ”์†Œ๋“œ๋กœ ์ฝ˜์†”์ฐฝ์— ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ €์žฅํ–ˆ๋Š”๋ฐ ๊ทธ์ € ์ด๋ก ์œผ๋กœ 
๋ฐฐ์› ์„ ๋•Œ์™€๋Š” ๋‹ฌ๋ฆฌ ์žฌ๋ฐŒ๊ณ  ์ด๋ ‡๊ฒŒ๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๊ตฌ๋‚˜ ํ•˜๋ฉฐ ์‹ ๊ธฐํ–ˆ๋‹ค. ์•„์ง ์ƒ์†Œํ•œ ๋ถ€๋ถ„์ด 
๋งŽ์ง€๋งŒ ๊ณ„์† ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋ฉด Scanner ์ฒ˜๋Ÿผ ์ž์œ ์ž์žฌ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‚ ์ด ์˜ค์ง€์•Š์„๊นŒํ•œ๋‹ค :)

Q10 ArrayList์™€ .datํŒŒ์ผ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ’โ€ ArrayList ์— 3๊ฐœ์˜ Book ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ €์žฅํ•˜๊ณ , books.dat ํŒŒ์ผ์— ๊ฐ์ฒด๋ฅผ ๊ธฐ๋ก ๋ฐ ์ €์žฅํ•˜์„ธ์š”. books.dat ์— ๊ธฐ๋ก๋œ ๊ฐ์ฒด ์ •๋ณด๋ฅผ ์ฝ์–ด์„œ ๊ฐ๊ฐ์˜ ์ •๋ณด์™€ ํ• ์ธ๋œ ๊ฐ€๊ฒฉ์„ ์ถœ๋ ฅํ•˜์„ธ์š”.

โ—ผ Book Class

public class Book implements Serializable {
	
	private static final long serialVersionUID = 4973296860562099534L;
	private String title;
	private String author;
	private int price;
	private String publisher;
	private double discountRate;
	
	public Book() {}

	public Book(String title, String author, int price, String publisher, double discountRate) {
		super();
		this.title = title;
		this.author = author;
		this.price = price;
		this.publisher = publisher;
		this.discountRate = discountRate;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public double getDiscountRate() {
		return discountRate;
	}

	public void setDiscountRate(double discountRate) {
		this.discountRate = discountRate;
	}

	@Override
	public String toString() {
		return title + ", " + author + ", " + publisher + ", " + price + "์›, " 
				+ (int)(Math.round(discountRate*100)) + "% ํ• ์ธ\nํ• ์ธ๋œ ๊ฐ€๊ฒฉ : " + (int)(price-price*discountRate) + "์›";
	}
}

โ—ผ BookListTest Class

public class BookListTest {

	public static void main(String[] args) {
		
		BookListTest test10 = new BookListTest();
		ArrayList<Book> list = new ArrayList<Book>();
		
		/* Book ๊ฐ์ฒด๋ฅผ 3๊ฐœ ์ƒ์„ฑํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ๊ธฐ */
		test10.storeList(list);
		
		/* book.dat ํŒŒ์ผ์— ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋œ book ๊ฐ์ฒด๋“ค์„ ์ €์žฅ */
		test10.saveFile(list);
		
		/* books.dat ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋“ค์„ ์ฝ์–ด์„œ ๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ธฐ */
		List<Book> booksList = test10.loadFile();
		
		// ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋œ ๊ฐ์ฒด ์ •๋ณด ์ถœ๋ ฅ
		test10.printList(booksList);
		
	}
	
	/* Book ๊ฐ์ฒด๋ฅผ 3๊ฐœ ์ƒ์„ฑํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ๊ธฐ */
	public void storeList(List<Book> list) {
		
		list.add(new Book("์ž๋ฐ”์˜ ์ •์„", "๋‚จ๊ถ์„ฑ", 30000, "๋„์šฐ์ถœํŒ", 0.15));
		list.add(new Book("์—ดํ˜ˆ๊ฐ•์˜ ์ž๋ฐ”", "๊ตฌ์ •์€", 29000, "ํ”„๋ฆฌ๋ ‰", 0.2));
		list.add(new Book("๊ฐ์ฒด์ง€ํ–ฅ JAVA8", "๊ธˆ์˜์šฑ", 30000, "๋ถ์Šคํ™ˆ", 0.1));
		
	}
	
	/* book.dat ํŒŒ์ผ์— ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋œ book ๊ฐ์ฒด๋“ค์„ ์ €์žฅ */
	public void saveFile(List<Book> list) {
		
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/ncs/test10/book.dat"));
			
			for(int i = 0; i < list.size(); i++) {
				oos.writeObject(list.get(i));
				}
			
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/* books.dat ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋“ค์„ ์ฝ์–ด์„œ ๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ธฐ */
	public List<Book> loadFile() {

		List<Book> booksList = new ArrayList<>();
		
		Book[] book = new Book[3];
		
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/ncs/test10/book.dat"))) {
			
			for(int i = 0; i < book.length; i++) {
				book[i] = (Book) ois.readObject();

				booksList.add(book[i]);
			}
			
			ois.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		return booksList;
	}

	public void printList(List<Book> booksList) {
		
		for(Book book : booksList) {
			System.out.println(book);
		}		
	}
}

๐Ÿ’ฌ Overall Comment

* ์ •๋ง ์• ๋จน์—ˆ๋˜ ๋ฌธํ•ญ ์ค‘ ํ•˜๋‚˜์˜€๋‹ค. ์ž…์ถœ๋ ฅ๊ณผ ๋ฐฐ์—ด, ๊ทธ๋ฆฌ๊ณ  ArrayList๊นŒ์ง€ ํ•ฉ์„ธํ•˜๋‹ˆ ์ •๋ง ๋‚œํ•ญ 
๊ทธ ์ž์ฒด์˜€๋‹ค. ์ด๋ฒˆ์— ํ‘ผ ๋ชจ๋“  ์ž…์ถœ๋ ฅ ๋ฌธํ•ญ๋“ค ์ค‘ ๋‚ด๊ฐ€ ๋ง‰ํ˜”๋˜ ๋ถ€๋ถ„์€ ๋™์ผํ–ˆ๋‹ค. ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” 
๊ฒƒ๊นŒ์ง€๋Š” ์„ฑ๊ณตํ–ˆ์œผ๋‚˜, ๊ทธ ํŒŒ์ผ์—์„œ ์ฝ์–ด์™€ ๋ฐฐ์—ด์ด๋‚˜ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์—์„œ ๊ณ„์† ๋ง‰ํžˆ๋‹ˆ 
์‹œ๊ฐ„์ด ํ•„์š” ์ด์ƒ์œผ๋กœ ์†Œ์š”๋˜์—ˆ๊ณ  ์ง„์ „์ด ๋˜์ง€ ์•Š์•˜๋‹ค. ์ด ๋ฌธํ•ญ์œผ๋กœ ์ •๋ง ๋งŽ์ด ๋ฐฐ์› ์œผ๋‹ˆ, ๊ผญ 
๊ธฐ๋ก๋ฟ ์•„๋‹ˆ๋ผ ๋จธ๋ฆฟ์†์—๋„ ๊ธฐ์–ตํ•ด์„œ ๋‹ค๋ฅธ ๋น„์Šทํ•œ ๋ฌธํ•ญ์—์„œ๋„ ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ ์‹ถ๋‹ค :( ํ™”์ดํŒ…!
profile
Tiny little habits make me

0๊ฐœ์˜ ๋Œ“๊ธ€