자바 코딩

클래스 , 메서드 Coding 연습1

akasha.park 2023. 3. 16. 13:53

public class Flower {
	private String name;
	private String color;
	private int price;
	public static final String COLOR_RED = "RED";
	public static final String COLOR_YELLOW = "YELLOW";
	public static final String COLOR_WHITE = "WHITE";

	public Flower(String name, String color, int price) {		
		this.name = name;
		this.color = color;
		this.price = price;
	}

	public String getName() {
		return name;
	}

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

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public int getPrice() {
		return price;
	}

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

}
public class FlowerTest {

	public static void main(String[] args) {
		System.out.println("***************************************");
		System.out.println("꽃다발을 만듭니다.");
		System.out.println("빨간색 2송이씩, 흰색 3송이씩, 노란색 4송이씩");
		System.out.println("***************************************");
		Flower flowers[] = new Flower[] { new Flower("장미", Flower.COLOR_RED, 1250)
				,new Flower("장미", Flower.COLOR_RED, 1250)
		,new Flower("백합", Flower.COLOR_WHITE, 1000)
		,new Flower("백합", Flower.COLOR_WHITE, 1000)
		,new Flower("백합", Flower.COLOR_WHITE, 1000)
		,new Flower("수국", Flower.COLOR_WHITE, 1000)
		,new Flower("수국", Flower.COLOR_WHITE, 1000)
		,new Flower("수국", Flower.COLOR_WHITE, 1000)
		,new Flower("프리지아", Flower.COLOR_YELLOW, 1500)
		,new Flower("프리지아", Flower.COLOR_YELLOW, 1500)
		,new Flower("프리지아", Flower.COLOR_YELLOW, 1500)
		,new Flower("프리지아", Flower.COLOR_YELLOW, 1500)
		};
		int counts[] = new int[4];
		String names[] = new String[4];
		int totalPrice = 0;
		for (int i=0;i<flowers.length;i++) {
			switch(flowers[i].getName()) {
			    case "장미" : counts[0]++; 
			                 if(names[0] == null) names[0] = flowers[i].getName();  break;
			    case "백합" : counts[1]++; 
			                 if(names[1] == null)names[1] =flowers[i].getName(); break;
			    case "수국" : counts[2]++; 
			                 if(names[2] == null)names[2] =flowers[i].getName(); break;
			    case "프리지아" : counts[3]++; 
			                if(names[3] == null) names[3] =flowers[i].getName(); break;
			}	
			 totalPrice +=flowers[i].getPrice();			
		}
		
		for (int i=0;i<counts.length;i++) {
		   System.out.println(names[i] +" : " +counts[i]+"송이");
		}
		
		System.out.println("***************************************");
		System.out.println("총 "+flowers.length+" 송이로 이루어진 꽃다발입니다.");
		System.out.println("가격은 "+totalPrice+"원 입니다.");
	}

}