Skip to content

Commit 8f86708

Browse files
authored
Add files via upload
1 parent ce1a398 commit 8f86708

5 files changed

Lines changed: 231 additions & 0 deletions

File tree

week02/advanced/ArrayUtil.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package week02.advanced;
2+
3+
import java.util.Arrays;
4+
5+
public class ArrayUtil {
6+
7+
static int[] sort(int[] arr) {
8+
int[] sortArr = Arrays.copyOf(arr, arr.length);
9+
for (int i = 0; i < arr.length-1; i++) {
10+
int cnt = 0;
11+
for (int j = 0; j < arr.length-1-i; j++) {
12+
if (sortArr[j] > sortArr[j+1]) {
13+
int tmp = sortArr[j];
14+
sortArr[j] = sortArr[j+1];
15+
sortArr[j+1] = tmp;
16+
cnt++;
17+
}
18+
}
19+
if (cnt == 0) {
20+
break;
21+
}
22+
}
23+
return sortArr;
24+
}
25+
26+
static int search(int[] arr, int target) {
27+
for (int i = 0; i < arr.length; i++) {
28+
if (arr[i] == target) {
29+
return i;
30+
}
31+
}
32+
return -1;
33+
}
34+
35+
36+
// 25단계 (심화): 배열 유틸리티 클래스 만들기
37+
// 문제: ArrayUtil.java 클래스를 만들고, 모든 메서드를 static으로 구현하세요.
38+
// 1. static int[] sort(int[] arr): 버블 정렬 알고리즘을 사용하여 배열을 오름차순으로
39+
// 정렬한 새로운 배열을 리턴합니다. (원본 배열은 변경하지 않음)
40+
// 2. static int search(int[] arr, int target): 정렬된 배열에서 target 값이 있는
41+
// 인덱스를 찾아 리턴합니다. 없으면 -1을 리턴합니다.
42+
// UtilTest.java에서 정수 배열을 만들고, ArrayUtil의 메서드들을 호출하여 결과를 확인하세요.
43+
// 핵심 사고: 특정 데이터에 종속되지 않고 기능만 제공하는 유틸리티성 클래스를 static 멤버만으로
44+
// 구현하고, 메서드의 순수성(원본 데이터를 훼손하지 않음)을 이해합니다.
45+
// 힌트: 모든 멤버가 static인 클래스, 배열, 이중 for문(버블정렬), return
46+
47+
}

week02/advanced/Cart.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package week02.advanced;
2+
3+
public class Cart {
4+
5+
Product [] items = new Product[10];
6+
int count = 0;
7+
8+
void addProduct(Product p) { // 카트에 물건 담기
9+
if (count == items.length) {
10+
System.out.println("카트에 물건이 가득 찼습니다.");
11+
return;
12+
}
13+
else {
14+
items[count] = p;
15+
count++;
16+
}
17+
}
18+
19+
void displayCart() { // 카트 목록 + 총 금액 출력
20+
int sum = 0;
21+
System.out.println();
22+
23+
if (count == 0) {
24+
System.out.println("카트가 비어있습니다.");
25+
}
26+
else {
27+
System.out.println("-----쇼핑 목록-----");
28+
for (int i = 0; i < count; i++) {
29+
System.out.println(items[i]);
30+
sum += items[i].getPrice();
31+
}
32+
System.out.println("총 금액: " + sum);
33+
}
34+
}
35+
36+
// 24단계 (심화): 쇼핑몰 상품과 장바구니 시스템
37+
// 문제:
38+
// 1. Product.java: 상품명(name), 가격(price)을 private 멤버와 Getter로 가집니다.
39+
// 2. Cart.java: Product 객체 배열(items, 크기 10)과 현재 담긴 개수(count)를
40+
// 멤버 변수로 가집니다.
41+
// addProduct(Product p) 메서드와, 카트의 모든 상품 정보와 총액을 출력하는 displayCart()
42+
// 메서드를 구현합니다.
43+
// 3. Shopping.java의 main에서 여러 Product 객체를 만들고, while문을 통해 사용자에게
44+
// 상품 번호를 입력받아 Cart 객체에 추가합니다. '0'을 입력하면 displayCart()를
45+
// 호출하고 종료합니다.
46+
// 핵심 사고: 서로 다른 역할을 하는 여러 클래스들이 상호작용하며 하나의 완성된 시스템을
47+
// 구축하는 과정을 경험합니다.
48+
// 힌트: 여러 클래스 설계, 객체 배열 관리, 객체 간 상호작용, while문, switch문, input
49+
50+
}

week02/advanced/Product.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package week02.advanced;
2+
3+
public class Product {
4+
5+
private String name;
6+
private int price;
7+
8+
Product(String name, int price) {
9+
this.name = name;
10+
this.price = price;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
public int getPrice() {
17+
return price;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "상품명: " + name + ", 가격: " + price;
23+
}
24+
25+
// 24단계 (심화): 쇼핑몰 상품과 장바구니 시스템
26+
// 문제:
27+
// 1. Product.java: 상품명(name), 가격(price)을 private 멤버와 Getter로 가집니다.
28+
// 2. Cart.java: Product 객체 배열(items, 크기 10)과 현재 담긴 개수(count)를
29+
// 멤버 변수로 가집니다.
30+
// addProduct(Product p) 메서드와, 카트의 모든 상품 정보와 총액을 출력하는 displayCart()
31+
// 메서드를 구현합니다.
32+
// 3. Shopping.java의 main에서 여러 Product 객체를 만들고, while문을 통해 사용자에게
33+
// 상품 번호를 입력받아 Cart 객체에 추가합니다. '0'을 입력하면 displayCart()를
34+
// 호출하고 종료합니다.
35+
// 핵심 사고: 서로 다른 역할을 하는 여러 클래스들이 상호작용하며 하나의 완성된 시스템을
36+
// 구축하는 과정을 경험합니다.
37+
// 힌트: 여러 클래스 설계, 객체 배열 관리, 객체 간 상호작용, while문, switch문, input
38+
39+
}

week02/advanced/Shopping.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package week02.advanced;
2+
3+
import java.util.Scanner;
4+
5+
public class Shopping {
6+
7+
public static void main(String[] args) {
8+
System.out.println("<쇼핑몰 상품과 장바구니 시스템>");
9+
// 24단계 (심화): 쇼핑몰 상품과 장바구니 시스템
10+
// 문제:
11+
// 1. Product.java: 상품명(name), 가격(price)을 private 멤버와 Getter로 가집니다.
12+
// 2. Cart.java: Product 객체 배열(items, 크기 10)과 현재 담긴 개수(count)를
13+
// 멤버 변수로 가집니다.
14+
// addProduct(Product p) 메서드와, 카트의 모든 상품 정보와 총액을 출력하는 displayCart()
15+
// 메서드를 구현합니다.
16+
// 3. Shopping.java의 main에서 여러 Product 객체를 만들고, while문을 통해 사용자에게
17+
// 상품 번호를 입력받아 Cart 객체에 추가합니다. '0'을 입력하면 displayCart()를
18+
// 호출하고 종료합니다.
19+
// 핵심 사고: 서로 다른 역할을 하는 여러 클래스들이 상호작용하며 하나의 완성된 시스템을
20+
// 구축하는 과정을 경험합니다.
21+
// 힌트: 여러 클래스 설계, 객체 배열 관리, 객체 간 상호작용, while문, switch문, input
22+
Product [] arr = {new Product("딸기", 15000), // Product 객체 생성
23+
new Product("사과", 10000),
24+
new Product("배", 30000),
25+
new Product("바나나", 5000),
26+
new Product("우유", 4000),
27+
new Product("두부", 2000),
28+
new Product("계란", 12000),
29+
new Product("파", 1500),
30+
new Product("갈치", 30000),
31+
new Product("굴비", 25000),
32+
new Product("김치", 15000)};
33+
34+
35+
36+
Cart c = new Cart();
37+
38+
Scanner sc = new Scanner(System.in);
39+
int pNum = 100;
40+
41+
while(pNum != 0) {
42+
System.out.println("--------상품목록--------");
43+
// for ( Product p : arr ) {
44+
// System.out.println(p); // 상품목록 출력
45+
// }
46+
for (int i = 0; i < arr.length; i++) {
47+
System.out.println((i + 1) + ")" + arr[i]);
48+
}
49+
50+
System.out.println();
51+
System.out.println("추가하실 상품의 번호를 입력하세요.> ");
52+
System.out.println("(종료를 원하시면 0을 입력해주세요.)");
53+
pNum = sc.nextInt();
54+
if (1 <= pNum && pNum <= arr.length) {
55+
c.addProduct(arr[pNum - 1]); // 상품 카트에 담기
56+
}
57+
else if (pNum != 0) {
58+
System.out.println("없는 번호입니다.");
59+
}
60+
}
61+
c.displayCart(); // 카트 목록 + 총 금액 출력
62+
sc.close();
63+
}
64+
65+
}

week02/advanced/UtilTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package week02.advanced;
2+
3+
import java.util.Arrays;
4+
5+
public class UtilTest {
6+
7+
public static void main(String[] args) {
8+
System.out.println("<배열 유틸리티 클래스 만들기>");
9+
// 25단계 (심화): 배열 유틸리티 클래스 만들기
10+
// 문제: ArrayUtil.java 클래스를 만들고, 모든 메서드를 static으로 구현하세요.
11+
// 1. static int[] sort(int[] arr): 버블 정렬 알고리즘을 사용하여 배열을 오름차순으로
12+
// 정렬한 새로운 배열을 리턴합니다. (원본 배열은 변경하지 않음)
13+
// 2. static int search(int[] arr, int target): 정렬된 배열에서 target 값이 있는
14+
// 인덱스를 찾아 리턴합니다. 없으면 -1을 리턴합니다.
15+
// UtilTest.java에서 정수 배열을 만들고, ArrayUtil의 메서드들을 호출하여 결과를 확인하세요.
16+
// 핵심 사고: 특정 데이터에 종속되지 않고 기능만 제공하는 유틸리티성 클래스를 static 멤버만으로
17+
// 구현하고, 메서드의 순수성(원본 데이터를 훼손하지 않음)을 이해합니다.
18+
// 힌트: 모든 멤버가 static인 클래스, 배열, 이중 for문(버블정렬), return
19+
int [] arr = {3, 8, 3, 9, 7 , 4 , 3, 1, 9, 2, 4, 5, 0, 1, 2, 7, 4, 5};
20+
System.out.print("원본 배열: ");
21+
System.out.println(Arrays.toString(arr));
22+
System.out.print("정렬된 배열: ");
23+
System.out.println(Arrays.toString(ArrayUtil.sort(arr)));
24+
System.out.print("숫자 3이 존재하는 배열의 인덱스: ");
25+
System.out.println(ArrayUtil.search(ArrayUtil.sort(arr), 3));
26+
27+
28+
}
29+
30+
}

0 commit comments

Comments
 (0)