Icecream

"Java Stuty"

Posted by Chungman on March 17, 2021

class Icecream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package HomeworkReview;

public class Icecream {			// 편의점에서 아이스크림 한개씩 사는 형식으로 생각 할 수 있다.

	String name;
	int price;
	
	
	void set(String n, int p) {
		name = n;
		price = p;
	}
	
	String getName() {
		return name;
	}
	
	int getPrice() {
		return price;
	}
}

class IcecreamMain

1
2
3
4
5
6
7
8
9
10
11
12
package HomeworkReview;

public class IcecreamMain {

	public static void main(String[] args) {
		
		IcecreamMarket market = new IcecreamMarket();
	
		market.doSomething();
	}

}

class IcecreamMarket

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package HomeworkReview;

import java.util.Scanner;

public class IcecreamMarket {

	Icecream[] iceArr;						// 아이스크림명과 아이스크림 가격을 받을수 있는 배열
	Scanner sc = new Scanner(System.in);
	
	int inputInt(String msg) {
		System.out.print(msg);
		int num = sc.nextInt();
		sc.nextLine();
		return num;
	}
	
	String inputString(String msg) {
		System.out.print(msg);
		String str = sc.nextLine();
		return str;
	}
	
	void doSomething() {
		// 손님이 아이스크림을 구매
		buy();
		
		// 구매정보 출력
		info();
	}
	
	void buy() {
		int count = inputInt("아이스크림 몇개 구입할래? : ");
		System.out.println(count + "개");
		
		iceArr = new Icecream[count];
		for(int i = 0; i < iceArr.length; i++) {
			System.out.println("*** " + (i+1) + "번째 아이스크림 구매정보 입력 ***");
			String name = inputString("아이스크림명 : ");
			int price = inputInt("아이스크림 가격 : ");
			
			iceArr[i] = new Icecream();			// 생성자를 통해 출력
			iceArr[i].set(name, price);
		}
	}
	
	void info() {
		System.out.println("< 총 " + iceArr.length + "개 구매정보 출력 >");
		System.out.println("번호\t아이스크림명\t아이스크림가격");
		int no = 1;
		for(Icecream ice : iceArr) {
			System.out.println(no++ + "\t" + ice.getName() + "\t\t" + ice.getPrice());
		}
	}

}