1. 고객 등급을 인자 없이 출력하고 싶었음
1-1. 문제
메인 화면(
printMain())에서customer.getRating()처럼 인자 없이 등급을 출력하고 싶은데,getRating(int totalAmount)형태면 호출할 때마다 값을 넣어야 해서 흐름이 헷갈렸음.
1-2. 수정 전 코드
package CommerceProject;
public class Customer {
private String customerName;
private String email;
private String rating;
private int totalAmount;
public Customer(String customerName, String email, String rating, int totalAmount) {
this.customerName = customerName;
this.email = email;
this.rating = rating;
this.totalAmount = totalAmount;
}
public String getRating(int totalAmount) {
int t = totalAmount;
if (t < 500_000) return "브론즈";
if (t < 1_000_000) return "실버";
if (t < 2_000_000) return "골드";
return "플레티넘";
}
public void addOrderAmount(int amount) {
totalAmount += amount;
}
}
1-3. 수정 후 코드
package CommerceProject;
public class Customer {
private String customerName;
private String email;
private int totalAmount;
public Customer(String customerName, String email, int totalAmount) {
this.customerName = customerName;
this.email = email;
this.totalAmount = totalAmount;
}
public String getRating() {
if (totalAmount < 500_000) return "브론즈";
if (totalAmount < 1_000_000) return "실버";
if (totalAmount < 2_000_000) return "골드";
return "플레티넘";
}
public void addOrderAmount(int amount) {
totalAmount += amount;
}
public int getTotalAmount() {
return totalAmount;
}
public double getDiscountRate() {
if (totalAmount < 500_000) return 0.00;
if (totalAmount < 1_000_000) return 0.05;
if (totalAmount < 2_000_000) return 0.10;
return 0.15;
}
}
2. 주문 확정에서 출력/누적금액 반영이 꼬였음
2-1. 문제
할인 금액(
discountedTotal)을 계산해놓고도 주문 완료 출력이basket.getTotalPrice()로 나가서 화면에 보여주는 총액이 할인 전 금액이었음.
또 누적 구매금액 반영도 할인 전 금액으로 누적되어 등급/할인 로직과 어긋날 수 있었음.
2-2. 수정 전 코드
if (Confirmation == 1) {
int originalTotal = basket.getTotalPrice();
double rate = customer.getDiscountRate();
int discountedTotal = (int) Math.round(originalTotal * (1 - rate));
System.out.printf("주문이 완료되었습니다! 총 금액: %,d\n", basket.getTotalPrice());
System.out.printf("[%s 등급] 할인된 총 금액: %,d\n",customer.getRating(),discountedTotal);
System.out.println("---------------------------------------");
customer.addOrderAmount(basket.getTotalPrice());
systemDeductStock();
basket.Basketclear();
}
2-3. 수정 후 코드
if (Confirmation == 1) {
int originalTotal = basket.getTotalPrice();
String rating = customer.getRating();
double rate = customer.getDiscountRate();
int discountedTotal = (int) Math.round(originalTotal * (1 - rate));
System.out.printf("주문이 완료되었습니다! 총 금액: %,d%n", discountedTotal);
System.out.printf("[%s 등급] 할인된 총 금액: %,d%n", rating, discountedTotal);
System.out.println("---------------------------------------");
customer.addOrderAmount(discountedTotal);
systemDeductStock();
basket.Basketclear();
}
3. 장바구니 수량 저장 방식 때문에 출력/합계/재고차감이 복잡했음
3-1. 문제
addShoppingBasket(product, quantity)가 같은 상품을List<Product>에 quantity만큼 반복 저장해서
getOverLab()로 중복 제거하고getCount()로 다시 개수 세는 로직이 계속 반복되었음.
3-2. 수정 전 코드
package CommerceProject;
import java.util.ArrayList;
import java.util.List;
public class shoppingBasket {
private List<Product> products = new ArrayList<>();
public void Basketclear() {
products.clear();
}
public void addShoppingBasket(Product product, int quantity) {
for (int i = 0; i < quantity; i++) {
products.add(product);
}
}
public List<Product> getOverLab() {
List<Product> overlab = new ArrayList<>();
for (Product product : products) {
boolean run = false;
for (Product product1 : overlab) {
if (product1 == product) {
run = true;
break;
}
}
if (!run) overlab.add(product);
}
return overlab;
}
public int getCount(Product c) {
int count = 0;
for (Product product : products) {
if (product == c) count++;
}
return count;
}
public void printBasketItems() {
System.out.println("---------------------------------------");
System.out.println("아래와 같이 주문 하시겠습니까?");
System.out.println();
System.out.println("[ 장바구니 내역 ]");
List<Product> printed = new ArrayList<>();
for (Product p : products) {
boolean ready = false;
for (Product product : printed) {
if (product == p) {
ready = true;
break;
}
}
if (ready) continue;
int count = 0;
for (Product x : products) {
if (x == p) count++;
}
System.out.printf("%s | %,d원 | %s | 수량: %d개%n",
p.getProductName(), p.getPrice(), p.getDescription(), count);
printed.add(p);
}
}
public int getTotalPrice() {
int total = 0;
List<Product> printed = new ArrayList<>();
for (Product p : products) {
boolean ready = false;
for (Product product : printed) {
if (product == p) {
ready = true;
break;
}
}
if (ready) continue;
int count = 0;
for (Product x : products) {
if (x == p) count++;
}
total += p.getPrice() * count;
printed.add(p);
}
return total;
}
}
3-3. 수정 후 코드
package CommerceProject;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class shoppingBasket {
private final Map<Product, Integer> products = new LinkedHashMap<>();
public void Basketclear() {
products.clear();
}
public void addShoppingBasket(Product product, int quantity) {
products.put(product, products.getOrDefault(product, 0) + quantity);
}
public List<Product> getOverLab() {
return new ArrayList<>(products.keySet());
}
public int getCount(Product c) {
return products.getOrDefault(c, 0);
}
public void printBasketItems() {
System.out.println("---------------------------------------");
System.out.println("아래와 같이 주문 하시겠습니까?");
System.out.println();
System.out.println("[ 장바구니 내역 ]");
for (Map.Entry<Product, Integer> entry : products.entrySet()) {
Product p = entry.getKey();
int count = entry.getValue();
System.out.printf("%s | %,d원 | %s | 수량: %d개%n",
p.getProductName(), p.getPrice(), p.getDescription(), count);
}
}
public int getTotalPrice() {
int total = 0;
for (Map.Entry<Product, Integer> entry : products.entrySet()) {
Product p = entry.getKey();
int count = entry.getValue();
total += p.getPrice() * count;
}
return total;
}
}
4. 재고 비교가 “이번에 담는 수량”만 체크해서 누적 초과가 가능했음
4-1. 문제
categoryProduct()에서 수량 입력할 때selected.getInventory() < buy만 체크하면
이미 장바구니에 담긴 수량까지 합쳤을 때 재고를 초과하는 케이스가 생길 수 있었음.
4-2. 수정 전 코드
if (selected.getInventory() < buy) {
System.out.println("구매하시려는 양이 재고 보다 많습니다. 다시 입력해주세요");
continue;
}
basket.addShoppingBasket(selected, buy);
break;
4-3. 수정 후 코드
int already = basket.getCount(selected);
if (selected.getInventory() < already + buy) {
System.out.println("구매하시려는 양이 재고 보다 많습니다. 다시 입력해주세요");
continue;
}
basket.addShoppingBasket(selected, buy);
break;