Skip to content
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# java-convenience-store-precourse

## 출력

- 보유재고 출력
- 영수증 출력

## 입력

- 구매할 상품명 및 수량 입력
- 프로모션 적용 가능 시, 적용 여부 입력
- 프로모션 재고 부족 시, 정가 결제 여부 입력
- 멤버십 할인 여부 입력
- 추가 구매 여부 입력

## 연산

- 최신 재고 유지 연산
- 프로모션 적용 연산
- 멤버십 할인 적용 연산

## 예외

- 입력 포맷 이상
17 changes: 17 additions & 0 deletions src/main/java/store/model/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package store.model;

import java.lang.String;

public class Product {
private String name;
private int price;
private int quantity;
private String promotion;

public Product(String name, int price, int quantity, String promotion) {
this.name = name;
this.price = price;
this.quantity = quantity;
this.promotion = promotion;
}
}
22 changes: 22 additions & 0 deletions src/main/java/store/model/Promotion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package store.model;

import java.lang.String;
import java.util.Date;

// name,buy,get,start_date,end_date

public class Promotion {
private String name;
private int buy;
private int get;
private Date startDate;
private Date endDate;

public Promotion(String name, int buy, int get, Date startDate, Date endDate) {
this.name = name;
this.buy = buy;
this.get = get;
this.startDate = startDate;
this.endDate = endDate;
}
}
67 changes: 67 additions & 0 deletions src/main/java/store/repository/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package store.repository;

import store.model.Product;

import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;

public class ProductRepository {
private List<Product> products = new ArrayList<>();

public ProductRepository() {
loadProducts();
}

// 파일 읽어오기
private void loadProducts() {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("products.md");

// 파일 존재 않을 시 예외처리
if (inputStream == null) {
throw new RuntimeException("[ERROR] 'products.md' file not found");
}

try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
boolean firstLine = true;

while ((line = reader.readLine()) != null) {

// 첫 줄 건너뛰기
if (firstLine) {
firstLine = false;
continue;
}

// 한 줄씩 파싱 후 products 리스트에 저장
Product product = parseProduct(line);
if (product != null) {
products.add(product);
}
}
}
catch (IOException e) {
throw new RuntimeException("[ERROR] IO error reading products file");
}

}

// 문자 분리하기
private Product parseProduct(String line) {
String[] parts = line.split(",");
if (parts.length != 4) {
return null;
}

String name = parts[0].trim();
int price = Integer.parseInt(parts[1].trim());
int quantity = Integer.parseInt(parts[2].trim());
String promotion = parts[3].trim();

return new Product(name, price, quantity, promotion);
}
}
78 changes: 78 additions & 0 deletions src/main/java/store/repository/PromotionRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package store.repository;

import store.model.Promotion;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class PromotionRepository {
private List<Promotion> promotions = new ArrayList<>();

public PromotionRepository() {
loadPromotions();
}

// 파일 읽어오기
private void loadPromotions() {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("promotions.md");

// 파일 존재 않을 시 예외처리
if (inputStream == null) {
throw new RuntimeException("[ERROR] 'promotions.md' file not found");
}

try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
boolean firstLine = true;

while ((line = reader.readLine()) != null) {

// 첫 줄 건너뛰기
if (firstLine) {
firstLine = false;
continue;
}

// 한 줄씩 파싱 후 promotions 리스트에 저장
Promotion promotion = parsePromotion(line);
if (promotion != null) {
promotions.add(promotion);
}
}
}
catch (IOException e) {
throw new RuntimeException("[ERROR] IO error reading promotions file");
}

}

// 문자 분리하기
private Promotion parsePromotion(String line) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

String[] parts = line.split(",");
if (parts.length != 5) {
return null;
}

try {
String name = parts[0].trim();
int buy = Integer.parseInt(parts[1].trim());
int get = Integer.parseInt(parts[2].trim());
Date startDate = df.parse(parts[3].trim());
Date endDate = df.parse(parts[4].trim());

return new Promotion(name, buy, get, startDate, endDate);
}
catch (Exception e) {
throw new RuntimeException("[ERROR] Text file format error");
}
}
}
6 changes: 3 additions & 3 deletions src/main/resources/promotions.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name,buy,get,start_date,end_date
탄산2+1,2,1,2024-01-01,2024-12-31
MD추천상품,1,1,2024-01-01,2024-12-31
반짝할인,1,1,2024-11-01,2024-11-30
탄산2+1,2,1,2024-01-01,2025-03-08
MD추천상품,1,1,2024-01-01,2025-03-08
반짝할인,1,1,2024-11-01,2025-04-10