Programming Language/자바(JAVA)

Do it 자바 프로그래밍 입문 연습문제 6장

토자맨 2022. 12. 30. 05:21

Q5

public class Kim
{
    String name;
    String coffee;
    int money;

    public Kim(String name, String coffee, int money)
    {
        this.name = name;
        this.coffee = coffee;
        this.money = money;
    }

    public void takecoffee(Star st)
    {
        st.take(4000,this.coffee);
        this.money -= 4000;
    }
    public void showInfo() {
        System.out.println("이름 : " + name + " / 커피 : " + coffee + " / 남은 돈 : " + money);
    }
}

 

public class Star
{
    String coffee;
    int money;
    int customerCount;

    public void take(int money, String coffee)
    {
        this.money += money;
        this.coffee = coffee;
        customerCount++;
    }
    public void showInfo()
    {
        System.out.println("손님 수 : " + customerCount + " / 수입 : " + money + " / 커피 : " + coffee);
    }
}

 

public class Lee
{
    String name;
    String coffee;
    int money;

    public Lee(String name, String coffee, int money)
    {
        this.name = name;
        this.coffee = coffee;
        this.money = money;
    }
    public void takecoffee(Kong ko)
    {
        ko.take(4500, this.coffee);
        this.money -= 4500;
    }
    public void showInfo() {
        System.out.println("이름 : " + name + " / 커피 : " + coffee + " / 남은 돈 : " + money);
    }
}

 

public class Kong
{
    String coffee;
    int money;
    int customerCount;

    public void take(int money, String coffee)
    {
        this.money += money;
        this.coffee = coffee;
        customerCount++;
    }
    public void showInfo()
    {
        System.out.println("손님 수 : " + customerCount + " / 수입 : " + money + " / 커피 : " + coffee);
    }
}

 

public class takecoffee
{
    public static void main(String[] args)
    {
        Kim kim = new Kim("kim","아메리카노", 10000);
        Lee lee = new Lee("lee", "라떼", 15000);

        Star st = new Star();
        kim.takecoffee(st);
        kim.showInfo();
        st.showInfo();

        Kong ko = new Kong();
        lee.takecoffee(ko);
        lee.showInfo();
        ko.showInfo();

    }
}

Q6

public class Card
{
    static int cardNum = 1000;
    int ctmCardNum;

    public Card()
    {
        cardNum++;
        ctmCardNum = cardNum;
    }
    public static int getCardNum() {return cardNum;}

}

 

public class CardTest
{
    public static void main(String[] args) {
        System.out.println("클래스 직접 참조(static 변수) : " + Card.cardNum); // 클래스를 직접 참조
        System.out.println("클래스 직접 참조(static 함수) : " + Card.getCardNum()); // 클래스를 직접 참조

        Card card1 = new Card();
        System.out.println("card1 : " + card1.cardNum);
        System.out.println("card1 : " + card1.getCardNum());
        System.out.println("card1 클래스 직접 참조(static 변수) : " + Card.cardNum); // 클래스를 직접 참조
        System.out.println("card1 클래스 직접 참조(static 함수) : " + Card.getCardNum()); // 클래스를 직접 참조
        System.out.println("card1 고객 카드 번호 : " + card1.ctmCardNum);

        Card card2 = new Card();
        System.out.println("card2 : " + card2.cardNum);
        System.out.println("card2 : " + card2.getCardNum());
        System.out.println("card2 클래스 직접 참조(static 변수) : " + Card.cardNum); // 클래스를 직접 참조
        System.out.println("card2 클래스 직접 참조(static 함수) : " + Card.getCardNum()); // 클래스를 직접 참조
        System.out.println("card2 고객 카드 번호 : " + card2.ctmCardNum);
    }
}

Q7

public class Singleton
{
    // 인스턴스 생성을 외부에서 못하고 내부 함수를 이용해서 할 수 있도록 private 걸어줌
    private static Singleton instance = new Singleton();
    private Singleton() {}

    public static Singleton getInstance()
    {
        // 인스턴스가 생성되지 않았을 때 최초 인스턴스 생성
        // 만약 인스턴스가 생성되어 있다면 기존의 인스턴스 리턴
        // 즉, 인스턴스는 오직 하나만 생성할 수 있음(싱글톤)
        if (instance == null)
            instance = new Singleton();
        return instance;
    }
}

 

public class SingletonTest
{
    public static void main(String[] args) {
        // 인스턴스를 두 개 생성한다.
        Singleton st1 = Singleton.getInstance();
        Singleton st2 = Singleton.getInstance();

        // 만약 두 인스턴스가 같다면 true, 다르다면 false 출력
        System.out.println(st1 == st2); // true 출력
    }
}