관리 메뉴

Frog is cry

익명클래스(카페예제) 본문

JAVA/복습

익명클래스(카페예제)

Frog is cry 2020. 7. 29. 19:33
package 익명클래스;

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class ButtonTest extends Frame{

	public ButtonTest() {
		super("익명 클래스 테스트");
		Button btn = new Button("눌러!");
		add(btn);
		setSize(500, 500);
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.out.println("프레임 종료");
				System.exit(0);
			}
		});
		
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				System.out.println(e.getX());
				System.out.println(e.getY());
				System.out.println("버튼 눌렀어요!");
			}
		});
		
		setVisible(true);
	}

	public static void main(String[] args) {
		new ButtonTest();
	}
}

카페예제

package 익명클래스;

public interface Cafe {
	// 앞에 abstract가 모두 생략됨. interface이기때문에
	//메뉴판
	//판매
	void setMenu(String[] menu);
	String[] getMenu();
	
	void sell(String choice);
	
}
package 익명클래스;

public class Starbucks {
	private String [] menu = {"아메리카노", "레몬티", "카푸치노"};
	private int[] arPrice = {5000, 8000, 9000};
	private int income;	//수익
	String choice = "카푸치노";

	public String[] getMenu() {
		return menu;
	}


	public void setMenu(String[] menu) {
		this.menu = menu;
	}


	public int[] getArPrice() {
		return arPrice;
	}


	public void setArPrice(int[] arPrice) {
		this.arPrice = arPrice;
	}


	public int getIncome() {
		return income;
	}


	public void setIncome(int income) {
		this.income = income;
	}


	public void registCafe(Cafe c, String choice) {
		c.setMenu(menu);
		System.out.println("카페 등록 완료");
		System.out.println("----Menu----");
		
		for (int i = 0; i < c.getMenu().length; i++) {
			System.out.println(i+1+c.getMenu()[i]);
		}
		c.sell(choice);
	}
	public static void main(String[] args) {
		Starbucks gangnam = new Starbucks();
		gangnam.registCafe(new Cafe() {
			
			@Override
			public void setMenu(String[] menu) {
				gangnam.menu = menu;
			}
			
			@Override
			public void sell(String choice) {
				for (int i = 0; i < gangnam.getMenu().length; i++) {
					if(gangnam.getMenu()[i].equals(choice)) {
						gangnam.income += gangnam.arPrice[i];
					}
				}
			}
			
			@Override
			public String[] getMenu() {
				return gangnam.menu;
			}
		},  "카푸치노"); 
		
		System.out.println("카푸치노 주문 완료");
		System.out.println("현재 수익 : " + gangnam.getIncome());
		}
	}
	

'JAVA > 복습' 카테고리의 다른 글

Wrapper 클래스  (0) 2020.07.29
예외처리(엘레베이터 예제)  (0) 2020.07.29
내부클래스(Out Class)  (0) 2020.07.29
For-each  (0) 2020.07.29
인터페이스와 추상클래스의 차이  (0) 2020.07.25
Comments