Skip to content
This repository was archived by the owner on Dec 26, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.nhnacademy.gaeun.assignment01;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class Consumer implements Runnable {
private Mart store;
private String name;
private Random random;

private static int count = 0;

public Consumer(String name, Mart store) {
this.store = store;
addCount();
this.name = name + this.count;
}

public String getName() {
return name;
}
public void addCount() {
this.count = count + 1;
}

@Override
public void run() {
try {
store.enter(this);
sellRandomTime();
store.exit(this);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}

private void sellRandomTime() {
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 10000));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
}
pickFoods();
}

public void pickFoods() {
int productNum = randomProductNum();
int amount = randomAmount();
Store product = store.getFoodStand().getFoodsList().get(productNum);
store.getFoodStand().sell(product, amount);
}

private int randomAmount() {
random = new Random();
return random.nextInt(10) + 1;
}

private int randomProductNum() {
random = new Random();
return random.nextInt(store.getFoodStand().getFoodsList().size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.nhnacademy.gaeun.assignment01;
import java.util.ArrayList;
import java.util.List;

public class FoodStand {
private List<Store> productList;

public FoodStand() {
this.productList = new ArrayList<>();
}


public List<Store> getFoodsList() {
return this.productList;
}

public void add(Store product, int amount) {
if(productList.contains(product)) {
product.add(amount);
} else {
productList.add(product);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

물건 리스트에 추가하고, 그 물건의 amount도 add해주는 식이면 더 의도에 맞을 수도 있겠습니다

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

product클래스 내부의 add메서드에서 구현하고 있습니다

}
}

public void sell(Store product, int amount) { //상품 판매
if(productList.contains(product)) {
product.sell(amount);
} else {
throw new IllegalArgumentException("해당 상품이 존재하지 않습니다.");
}
}
}
20 changes: 20 additions & 0 deletions Thread/src/main/java/com/nhnacademy/gaeun/assignment01/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.nhnacademy.gaeun.assignment01;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Main {
public static void main(String[] args) {
Mart store = new Mart();
Producer producer = new Producer(store);
Thread producerThread = new Thread(producer);
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
Runnable consumerTask = () -> {
Consumer consumer = new Consumer("Consumer", store);
Thread consumerThread = new Thread(consumer);
consumerThread.start();
};
executorService.scheduleAtFixedRate(consumerTask, 0, 5, TimeUnit.SECONDS);
producerThread.start();
}
}
50 changes: 50 additions & 0 deletions Thread/src/main/java/com/nhnacademy/gaeun/assignment01/Mart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.nhnacademy.gaeun.assignment01;
import java.util.ArrayList;
import java.util.List;

public class Mart {
private List<Consumer> people;

private FoodStand foodStand;

public static final int PEOPLE_NUM_LIMIT = 5;

public Mart() {
people = new ArrayList<>();
foodStand = new FoodStand();
}

public FoodStand getFoodStand() {
return foodStand;
}

public synchronized boolean isAbleEnter() {
return people.size() < PEOPLE_NUM_LIMIT;
}

public synchronized void enter(Consumer consumer) {
if (consumer == null) {
throw new IllegalArgumentException("consumer is null");
}
while (!isAbleEnter()) {
try {
System.out.println("대기중입니다 ...");
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
people.add(consumer);
System.out.println(consumer.getName() + " enter");
}

public synchronized void exit(Consumer consumer) {
people.remove(consumer);
System.out.println(consumer.getName() + " exit");
notifyAll();
}

public void addFoodStand(Store product, int amount) {
foodStand.add(product, amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.nhnacademy.gaeun.assignment01;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import com.nhnacademy.gaeun.assignment01.product.*;

public class Producer implements Runnable {
private Mart store;
private List<Store> itemList;
private Random random;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자에서 초기화 해줘도 좋을 것 같습니다

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다!


public Producer(Mart store) {
this.store = store;
this.itemList = new ArrayList<>();
this.random = new Random();
initialSetting();
}

private void initialSetting() {
itemList.add(new Vegetable("야채", 30));
itemList.add(new ProcessedFood("조리 식품", 30));
itemList.add(new DriedSeafood("건어물", 30));
itemList.add(new CannedFoods("기호 식품", 30));
itemList.add(new OtherItems("기타", 30));
}

@Override
public void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
}

randomDelivery();
}
}

private void randomDelivery() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

randomDelivery가 배달하는 행동이라고 생각을 하는데, 메서드 안에서 store.getFoodStand를 호출해서 add를 하는 방식 대신에 product를 return을 시켜주는 방식은 어떨까요??

그리고 store에서 return 값을 받는 메서드를 만든 다음에 Producer run메서드에 store."메서드 명"(randomDelivery()) 이런 방식으로 해주는건 어떨까여?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오! 참고해서 수정했습니다.

int deliveryNum = randomDeliveryNum();
int amount = randomAmount();
Store product = bringProduct(deliveryNum);
this.store.addFoodStand(product, amount);

}
private int randomAmount() {
return this.random.nextInt(10) + 1;
}

private int randomDeliveryNum() {
return this.random.nextInt(itemList.size());
}

private Store bringProduct(int deliveryNum) {
return this.itemList.get(deliveryNum);
}
}
55 changes: 55 additions & 0 deletions Thread/src/main/java/com/nhnacademy/gaeun/assignment01/Store.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.nhnacademy.gaeun.assignment01;
public class Store {

private String name;
private int amount;
private static final int MAX_NUM = 30;

public Store() {
this.name = "";
this.amount = 0;
}
public Store(String name, int amount) {
this.name = name;
this.amount = amount;
}
public String getName() {
return name;
}

public synchronized boolean isAbleBuy(int amount) {
if (this.amount + amount > MAX_NUM) {
System.out.println(this.name + "의 재고는 30개까지만 가능합니다.");
return false;
}
return true;
}

public synchronized boolean isAbleSell(int amount) {
return (this.amount - amount >= 0);
}

public synchronized void add(int amount) {
if (isAbleBuy(amount)) {
this.amount += amount;
System.out.printf("%s %d개가 가판대에 추가 되었습니다.\n", this.name, amount);
notifyAll();
}
}

public synchronized void sell(int amount) { //상품 판매
while (!isAbleSell(amount)) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
if(isAbleSell(amount)) {
this.amount -= amount;
System.out.printf("%s %d개 구매 완료.\n", this.name, amount);
System.out.printf("남은 %s의 수량: %d\n", this.name, this.amount);
notifyAll();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.nhnacademy.gaeun.assignment01;


interface StoreFactory {
Store createStore();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//package com.nhnacademy.gaeun.assignment01.factory;
//
//import com.nhnacademy.gaeun.assignment01.Store;
//import com.nhnacademy.gaeun.assignment01.StoreFactory;
//import com.nhnacademy.gaeun.assignment01.product.CannedFoods;
//
//public class CannedFoodsFactory implements StoreFactory {
// @Override
// public Store createStore() {
// return new CannedFoods();
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.nhnacademy.gaeun.assignment01.product;
import com.nhnacademy.gaeun.assignment01.Store;

public class CannedFoods extends Store {
public CannedFoods(String name, int amount) {
super(name, amount);
}

public CannedFoods() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nhnacademy.gaeun.assignment01.product;
import com.nhnacademy.gaeun.assignment01.Store;
public class DriedSeafood extends Store {
public DriedSeafood(String name, int amount) {
super(name, amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nhnacademy.gaeun.assignment01.product;
import com.nhnacademy.gaeun.assignment01.Store;
public class OtherItems extends Store {
public OtherItems(String name, int amount) {
super(name, amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nhnacademy.gaeun.assignment01.product;
import com.nhnacademy.gaeun.assignment01.Store;

public class ProcessedFood extends Store {
public ProcessedFood(String name, int amount) {
super(name, amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nhnacademy.gaeun.assignment01.product;
import com.nhnacademy.gaeun.assignment01.Store;

public class Vegetable extends Store {
public Vegetable(String name, int amount) {
super(name, amount);
}
}
Loading