๐โ ์ฃผ์ด์ง String ๋ฐ์ดํฐ๋ฅผ โ,โ๋ก ๋๋์ด 5๊ฐ์ ์ค์ ๋ฐ์ดํฐ๋ค์ ๊บผ๋ด์ด ํฉ๊ณผ ํ๊ท ์ ๊ตฌํ์ธ์. ๋จ, String ๋ฌธ์์ด์ ๋ชจ๋ ์ค์ ๋ฐ์ดํฐ๋ฅผ ๋ฐฐ์ด๋ก ๋ง๋ค์ด ๊ณ์ฐํฉ๋๋ค.
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, ๋ฐฐ์ด์ ์ฌ์ฉํ๋ ํ์ต์ด ๋์๊น๋๋ ๋๋์ด์๋ค.
๐โ GregorianCalendar ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ, ํ์ฌ ๋ ๋์ ๋น๊ตํ ๋์ด๋ฅผ ๊ณ์ฐํ๊ณ ์์ผ์ ์์ผ์ ์ถ๋ ฅํ์ธ์. ์ถ๋ ฅ์ SimpleDateFormat์ ์ฌ์ฉํ์ฌ ์ถ๋ ฅํ์ธ์.
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์ ํ์ฉํ์ฌ ํ์ด๋ด๋ คํ๋ ๊ฝค ์๊ฐ์ ๋ง์ด ํด์ผํ๋
๋ถ๋ถ์ด ์์๋ค. ๋ง์ด ์ฌ์ฉํด๋ณด์ง ๋ชป ํ ํด๋์ค๋ค์ด๋ผ ์ ๋ฆฌํด๋ ๊ฒ์ ๊ผผ๊ผผํ ์ฐพ์๊ฐ๋ฉฐ ํด์ผํ๋ค.
์์ง ๋จธ๋ฆฟ์์ ์ ๋ง ๋ฌด์ํ ๋ฉ์๋๋ค์ด ์ ๋ฆฌ๋์ง ์์์ ์ ๋ฅผ ๊ฝค ๋จน์์ง๋ง ์ ์ ๋ฆฌ๋ ์ฝ๋๊ฐ
์์ฑ๋๋ ๊ทํ ์ฌ์ฐ์ด ์๊ธด ๋๋์ด๊ณ ๋งค์ฐ ๋ฟ๋ฏํ๋ค.
๐โ 2๋ถํฐ 5๊น์ง์ ์ ์ํ ๋ฐ์ดํฐ๋ง์ ํค๋ณด๋๋ก ์ ๋ ฅ ๋ฐ์ 1๋ถํฐ ์ ๋ ฅ ๋ฐ์ ์๊น์ง์ ํฉ์ ์ถ๋ ฅํ์ธ์. ๋จ, ์ ๋ ฅ ๋ฐ์ ์๊ฐ 2๋ถํฐ 5๊น์ง์ ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ฉด โ์ ๋ ฅ ๊ฐ์ ์ค๋ฅ๊ฐ ์์ต๋๋ค.โ๋ผ๊ณ ์ถ๋ ฅํ๊ณ ํ๋ก๊ทธ๋จ์ ์ข ๋ฃํฉ๋๋ค.
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;
}
}
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();
}
}
}
public class InvalidException extends Exception {
public InvalidException(String message) {
super(message);
}
}
๐ฌ Overall Comment
* ํญ์ ์๋ฒฝํ ๊ธฐ์ตํ์ง ๋ชป ํ๋ ๋ถ๋ถ๋ค์ ์ธ์ ๊ฐ ๋ฌธ์ ๋ก ๋ค๊ฐ์์ ๋์ ๋ทํต์๋ฅผ ์ฝฉ- ์น๋ ๊ฒ
๊ฐ๋ค. ์์ธ์ฒ๋ฆฌ์ ํ๋ฆ์ ์ดํดํ๊ณ ๊ธฐ์ตํ๋ค๊ณ ์๊ฐํ์ผ๋ ์ดํด๋ง ํ๋ ๋ด ์์ ๋ฐ์ฑํด์ผํ๋ค.
๊ฒฐ๊ตญ ๋ ์ ๋ฆฌํด๋ ์์ธ์ฒ๋ฆฌ ํฌ์คํธ๋ฅผ ์ฐพ์๋ณด๋ฉฐ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์๋ค. ์์ง์ ๊ธฐ์ต๋ณด๋ค
๊ธฐ๋ก์ ์์กดํด์ผํ๋ ์์น๋จ๊ณ์ด์ง๋ง ์ด๋ฐ ๋ฌธ์ ๋ฅผ ํ์ด๋ณด๋ฉฐ ๊ณ์ ๊ธฐ์ต์ ๊ณฑ์น์ด์ค๋ค๋ฉด ํ๋
์ฐพ์๋ณด์ง์๊ณ ๋ฌธ์ ๋ฅผ ํ ์ ์๋ ๋ ์ด ์ฌ ๊ฒ์ด๋ผ ๋ฏฟ๋๋ค :)
๐โ Abstract ํด๋์ค์์ ์์๋ฐ์ ๋ ๊ฐ์ ํด๋์ค๋ฅผ ๊ตฌํํ์ฌ ์๋ 5๋ฒ ํญ๋ชฉ์ ์คํ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋๋ก ํ๋ก๊ทธ๋จ์ ์์ฑํ์ธ์.
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);
}
}
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));
}
}
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));
}
}
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
* ๋ด๊ฐ ์ข์ํ๋ ํํธ์ธ '์์'ํํธ. ํ์ง๋ง ์ด ๋ฌธ์ ๋ํ ํธ๋ฝํธ๋ฝํ์ง์์๋ค.
์๋ง๋ ํด๋์ค ๋ค์ด์ด๊ทธ๋จ์ผ๋ก ๋ฌธ์ ๋ฅผ ์ ํด์์ธ์ง ๋ชฐ๋ผ๋ ์ผ๋จ ๋ฌธ์ ๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ํ์
ํ๋๋ฐ
์๊ฐ์ด ๊ฝค ์์๋์๋ค. ์ธ์๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋๊ฒจ์ฃผ์ด ๊ฐ๊ฐ ์๋ธํด๋์ค์ ๋ฉ์๋๊ฐ ํธ์ถ๋๊ฒ๋
ํด์ผํ๋ ๋ฌธํญ์ด์๋ค. ์ดํดํ๋๋ฐ์ ๋ ์ค๋ ๊ฑธ๋ฆฐ ๋ฏํ๋ ๋ง์กฑ์ค๋ฌ์ด ์ฝ๋๊ฐ ์์ฑ๋ ๊ฒ ๊ฐ์ ๋ฟ๋ฏํ๋ค.
๐โ Abstract ํด๋์ค์์ ์์๋ฐ๊ณ Interface๋ฅผ ๊ตฌํํ ๋ ๊ฐ์ ํด๋์ค๋ฅผ ๊ตฌํํ์ฌ ํญ๋ชฉ 5๋ฒ์ ์คํ๊ฒฐ๊ณผ๊ฐ ๋์ค๋๋ก ํ๋ก๊ทธ๋จ์ ์์ฑํ์ธ์.
public interface Bonus {
public void incentive(int pay);
}
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;
}
}
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);
}
}
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);
}
}
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๋ก
๋ณ๊ฒฝํ๋ ๋ฐฉ์์ ์ ํํ๋๋ฐ, ๋ด์ผ ์ ์๋์ ํด์ค ์ฝ๋๊ฐ ์ฌ๋ผ์ค๋ฉด ๊ผญ ์ฐธ๊ณ ํ๋๋ก ํด์ผ๊ฒ ๋ค.
๐โ java.util.ArrayList ์ ๊ฐ๋ค์ ์ ์ฅํ๊ณ , ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ๋ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅ๋๊ฒ ํ์ธ์.
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;
}
}
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์ ์ค๋ณต๊ฐ์ ์ ๊ฑฐํ ์ ์๋ ๋ฐฉ๋ฒ์ ์ฐพ์๋ด์๋ค! ์ ์ด ๋์ ์ฝ๋๋ฅผ ์ด์ฌํ ๋ง๋ค์ด๋ณด๊ณ
์์ ํ๋ ๋ง์กฑ์ค๋ฌ์ด ๊ฒฐ๊ณผ๋ฌผ์ด ๋์จ ๋ฏ ํ์ฌ ๋ฟ๋ฏํ๋ค :)
๐โ java.util.Properties ๋ฅผ ์ฌ์ฉํ์ฌ, data.xml ํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ก ์ ์ฅํ ๋ค์ ํ์ผ์ ๊ธฐ๋ก๋ ๋ฐ์ดํฐ๋ค์ ์ฝ์ด์์ Fruit[] ์ ๊ธฐ๋กํ๊ณ Fruit[]์ ๊ฐ๋ค์ ํ๋ฉด์ ์ถ๋ ฅ ์ฒ๋ฆฌํ์ธ์.
public class Fruit {
private String name;
private int price;
private int quantity;
/* ๊ธฐ๋ณธ ์์ฑ์, ๋ชจ๋ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์ */
/* getter & setter */
/* toString */
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++;
}
}
}
<?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 ํ์ผ์์ ์ฝ์ด์ค๋ ๊ณผ์ ์ ๋ณด๋ค ๋ ํ์ตํด์ผ๊ฒ ๋ค๊ณ ๋ค์งํ๊ฒ ๋ ๋ฌธํญ์ด์๋ค.
๐โ java.util.Map ์ ์ฌ์ฉํ์ฌ ์ํ์ฌ๊ณ ์ ๋ณด๋ฅผ ์ ์ฅํ๊ณ ์ถ๋ ฅ๋๊ฒ ํ์ธ์. ์ถ๋ ฅ์ ๋ ์ง ๋ฐ์ดํฐ์ ๋ํด์๋ SimpleDateFormat ์ ์ฌ์ฉํ์ฌ ์ถ๋ ฅ ์ฒ๋ฆฌํ์ธ์.
public class AmountNotEnough extends Exception {
public AmountNotEnough() {}
public AmountNotEnough(String message) {
super(message);
}
}
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 + "๊ฐ";
}
}
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๋ฅผ ๊ตฌ์ฑํ ๋ ์ ์ํด์ผํ ๋ถ๋ถ์ด ๋ง์ ์ด๋ ค์ ๋ค.
๐โ Good ํด๋์ค๋ฅผ ์์ฑํ๊ณ , java.io.BufferedReader ๋ฅผ ์ฌ์ฉํ์ฌ ํค๋ณด๋๋ก ์ ๋ ฅ ๋ฐ์ ์คํธ๋ฆผ์ ์์ฑ
ํ๊ณ ๊ฐ ํ๋์ ๊ธฐ๋กํ ๊ฐ์ ์ ๋ ฅ ๋ฐ์ ๊ฐ์ฒด ์ด๊ธฐํ์ ์ฌ์ฉํ๋ค.
๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ณ์ฐํ์ฌ ๊ตฌ๋งค๊ฐ๊ฒฉ์ ์ถ๋ ฅํ๋ค.
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) + "์";
}
}
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 ์ฒ๋ผ ์์ ์์ฌ๋ก ์ฌ์ฉํ ์ ์๋ ๋ ์ด ์ค์ง์์๊นํ๋ค :)
๐โ ArrayList ์ 3๊ฐ์ Book ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ์ ์ฅํ๊ณ , books.dat ํ์ผ์ ๊ฐ์ฒด๋ฅผ ๊ธฐ๋ก ๋ฐ ์ ์ฅํ์ธ์. books.dat ์ ๊ธฐ๋ก๋ ๊ฐ์ฒด ์ ๋ณด๋ฅผ ์ฝ์ด์ ๊ฐ๊ฐ์ ์ ๋ณด์ ํ ์ธ๋ ๊ฐ๊ฒฉ์ ์ถ๋ ฅํ์ธ์.
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) + "์";
}
}
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๊น์ง ํฉ์ธํ๋ ์ ๋ง ๋ํญ
๊ทธ ์์ฒด์๋ค. ์ด๋ฒ์ ํผ ๋ชจ๋ ์
์ถ๋ ฅ ๋ฌธํญ๋ค ์ค ๋ด๊ฐ ๋งํ๋ ๋ถ๋ถ์ ๋์ผํ๋ค. ํ์ผ์ ์ ์ฅํ๋
๊ฒ๊น์ง๋ ์ฑ๊ณตํ์ผ๋, ๊ทธ ํ์ผ์์ ์ฝ์ด์ ๋ฐฐ์ด์ด๋ ๋ฆฌ์คํธ์ ์ ์ฅํ๋ ๋ฐฉ์์์ ๊ณ์ ๋งํ๋
์๊ฐ์ด ํ์ ์ด์์ผ๋ก ์์๋์๊ณ ์ง์ ์ด ๋์ง ์์๋ค. ์ด ๋ฌธํญ์ผ๋ก ์ ๋ง ๋ง์ด ๋ฐฐ์ ์ผ๋, ๊ผญ
๊ธฐ๋ก๋ฟ ์๋๋ผ ๋จธ๋ฆฟ์์๋ ๊ธฐ์ตํด์ ๋ค๋ฅธ ๋น์ทํ ๋ฌธํญ์์๋ ์ ์ฐํ๊ฒ ์ฌ์ฉํ๊ณ ์ถ๋ค :( ํ์ดํ
!