Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First seminar 과제 제출 #3

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sopt.org.firstSeminar.AOP;

public interface SortService {

void sort();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sopt.org.firstSeminar.AOP;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 AOP에 대해 잘몰라서 이를 코드로 구현하는걸 생각치도 못했네요! AOP 공부해야겠다는 다짐하고 갑니다.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사실 제가 AOP를 잘 몰라서 언니 AOP 관련 코드를 잘 이해 못했는디...... 이번주 세미나 시간에 물어봐두 되나요..?

SPRING AOP를 적용했다기보다는 부가기능(시간측정)을 분리한다는 개념으로 실습한거긴해!! 당근 세미나때 물어봐도 좋구..아니면 생각과제>AOP쪽에 저거 구현코드랑 같이 설명 적어놔서 그거 봐도 좋을 듯!!


public class SortServiceImpl implements SortService {

public void sort() {
//정렬해줌
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sopt.org.firstSeminar.AOP;

public class SortServiceTime implements SortService {
private SortService sortService;

public SortServiceTime(final SortService sortService) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DI 적용한 코드 좋아요

this.sortService = sortService;
}

@Override
public void sort() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P5] 저는 AOP 관련된 예제 코드는 작성을 못해봤었는데, 이렇게 직접 작성해보는 것도 도움이 될 것 같네요!! 참고해서 저도 직접 작성해보겠습니다 :)

long start = System.currentTimeMillis();
sortService.sort();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오옹 성능 테스트 좋아요~

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AOP 실습에 성능 테스트까지..!!! 최고다...

long end = System.currentTimeMillis();
System.out.printf("실행시간: %d", (end-start));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sopt.org.firstSeminar.AOP;

public interface Sorter {

void Sort();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sopt.org.firstSeminar.BankProgram;

import static sopt.org.firstSeminar.BankProgram.BankClient.accountArrayList;

public class Account {
private String ownerName;
private int AccountNumber;
private int balance;

public Account(String ownerName) {
this.ownerName = ownerName;
this.balance = 0;
this.AccountNumber = accountArrayList.size();
}

public void deposit(int money) {
this.balance += money;
}

public void withdraw(int money) {
if(balance >= money) {
this.balance -= money;
} else {
throw new RuntimeException("잔고 부족");
}
}

public int getBalance() {
return this.balance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sopt.org.firstSeminar.BankProgram;

public abstract class Bank {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P4] 이 코드에서는 Bank를 Sinhan이 상속하는게 변할일이 없지만, 상속을 사용하기보다는 인터페이스 구현을 사용하는게 수정의 용이함과 다중 구현이 가능하다는 점에서 더 좋을것같아요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

난 진자 추상클래스 VS 인터페이스를 뭘 선택해야할지 잘 모르겠드라,,일단 이 경우는 클로징멘트를 모든 Bank의 하위클래스에서 고정하고 싶어서 일반메소드를 가질 수 있는 추상클래스를 사용하긴 했는데,, 셤기간 끝나고 좀 더 자세히 파보고 다시 답변할게잉

{
    //클로징 멘트는 고정
    public void printClosingComment() {
        System.out.println("이용해주셔서 감사합니다.");
    }


abstract public void printWelcomeComment();

abstract public void deposit(int accountNumber, int money);

abstract public void withdraw(int accountNumber, int money);

abstract public int getBalance(int accountNumber);

//클로징 멘트는 고정
public void printClosingComment() {
System.out.println("이용해주셔서 감사합니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package sopt.org.firstSeminar.BankProgram;

import java.util.ArrayList;

public class BankClient {
public static ArrayList<Account> accountArrayList;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P4] 계좌리스트가 은행안에 있으면 좀더 좋을것아요!. 각 은행당 계좌가 있으니까


public static void main(String[] args) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

은행 사용자입장에서의 실행이 좋은것같아요

//은행프로그램에서 사용할 계좌리스트
accountArrayList = new ArrayList<>();

//계좌 생성
Account account = new Account("shinjiyeon");
accountArrayList.add(account);

//신한은행 이용
BankService bankService = new BankService(new ShinhanBank());

//은행 이용 시작
bankService.printWelcomeComment();

//입금 & 잔액 확인
bankService.deposit(0, 1000);
bankService.checkBalance(0);

//출금 & 잔액 확인
bankService.withdraw(0, 500);
bankService.checkBalance(0);

//은행 이용 종료
bankService.printClosingComment();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package sopt.org.firstSeminar.BankProgram;

public class BankService {
private Bank bank;

//생성자 주입
public BankService(Bank bank) {
this.bank = bank;
}

public void printWelcomeComment() {
bank.printWelcomeComment();
}

public void printClosingComment() {
bank.printClosingComment();
}

public void deposit(int accountNumber, int money) {
bank.deposit(accountNumber, money);
}

public void withdraw(int accountNumber, int money) {
bank.withdraw(accountNumber, money);
}

public void checkBalance(int accountNumber) {
int balance = bank.getBalance(accountNumber);
System.out.printf("고객님의 잔액은 %d원 입니다.", balance);
System.out.println();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sopt.org.firstSeminar.BankProgram;

import static sopt.org.firstSeminar.BankProgram.BankClient.accountArrayList;

public class ShinhanBank extends Bank {
@Override
public void printWelcomeComment() {
System.out.println("어서오세요, 신한은행 입니다.");
}

@Override
public void deposit(int accountNumber, int money) {
Account account = getAccountByNumber(accountNumber);
account.deposit(money);
System.out.println("신한은행에 입금이 완료되었습니다.");
}

@Override
public void withdraw(int accountNumber, int money) {
Account account = getAccountByNumber(accountNumber);
try {
account.withdraw(money);
System.out.println("신한은행에서 출금이 완료되었습니다.");
} catch (Exception e) {
System.out.println("신한은행 계좌에 잔고가 부족합니다.");
}
}

@Override
public int getBalance(int accountNumber) {
Account account = getAccountByNumber(accountNumber);
return account.getBalance();
}

private Account getAccountByNumber(int accountNumber) {
return accountArrayList.get(accountNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import sopt.org.firstSeminar.Inheritance.Animal;
import sopt.org.firstSeminar.Inheritance.Cat;

@SpringBootApplication
public class FirstSeminarApplication {

public static void main(String[] args) {

SpringApplication.run(FirstSeminarApplication.class, args);
}

Animal animal = new Cat("여자", 3, 6);
animal.eat("우유"); //자식클래스 Cat의 eat() 호출
//animal.superTest(); 상위클래스에서 하위클래스의 함수 알 수 없다!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P5] 하위 클래스에서는 super 키워드를 통해서 상위 클래스의 메서드를 사용할 수 있지만, 상위 클래스에서는 하위 클래스의 메서드를 알 수 없기에 사용할 수 없다는 의미군요! 한번더 생각해볼 수 있어서 좋았습니다!

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sopt.org.firstSeminar.Inheritance;

public class Animal {
private String gender;
private int age;
private int weight;

public Animal(String gender, int age, int weight) {
//내부 생성자 호출로 중복 제거
this(gender, age);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

깔끔하게 코드를 짰네요

this.weight = weight;
}

public Animal(String gender, int age) {
this.gender = gender;
this.age = age;
}

public void walk() {
System.out.println("동물이 걷습니다.");
}

public void eat(String food) {
System.out.println("동물이 "+ food + "를 먹습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sopt.org.firstSeminar.Inheritance;

public class Cat extends Animal {

public Cat(String gender, int age, int weight) {
//부모 클래스에서 매개변수 있는 생성자 만들어서 기본생성자가 자동으로 호출되지 X
//-> 부모 생성자 호출해줘야 한다.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자세히 정리해주는거 좋아요

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자식 클래스에서 매개변수 있는 생성자를 만들면, 부모 클래스의 기본 생성자가 자동으로 호출되지 않아서 부모 클래스에서 매개변수가 있는 생성자를 만들어준 경우에는 자식 클래스에서 부모 클래스의 생성자를 명시적으로 호출해주어야 하는것..!!!!! 덕분에 다시 되새겨볼수 있어서 좋았습니다.

super(gender, age, weight);
}

@Override
public void walk() {
System.out.println("고양이가 사뿐사뿐 걷습니다.");
}

@Override
public void eat(String food) {
System.out.println("고양이가 " + food + "를 먹습니다.");
}

public void superTest() {
walk(); //자식클래스의 walk() 실행
super.walk(); //부모클래스의 walk() 실행
}
}