260617 실습
본문 로딩 중...
댓글 0
댓글을 작성하려면 로그인이 필요합니다.
아직 댓글이 없습니다. 첫 번째 댓글을 작성해보세요!
using System;
public class Shopping
{
// IProduct, IDiscountable 인터페이스를 직접 설계한다.
// Food, Electronics 클래스로 인터페이스를 구현한다.
// Cart<T> where T : IProduct 제네릭 장바구니를 만든다.
// 전체를 조합해서 결제 시뮬레이션을 완성한다.
}
public interface IProduct
{
void DisplayInfo(); // 상품 정보 출력
int GetPrice(); // 정가 반환
string GetName(); // 상품명 반환
}
public interface IDiscountable
{
int GetDiscountRate(); // 할인율 반환 (0~100)
int GetDiscountedPrice(); // 할인가 반환
}
public class Food : IProduct, IDiscountable
{
// 필드: 상품명, 정가, 할인율, 유통기한
private string ProductName;
private int Price;
private int DiscountRate;
//private int expDate; 안써서 주석처리
// 생성자
public Food(string name, int price, int discountRate)
{
ProductName = name;
Price = price;
DiscountRate = discountRate;
//expDate = expirationDate;
}
// GetName() : 상품명 반환
public string GetName()
{
return ProductName;
}
// GetPrice() : 정가 반환
public int GetPrice()
{
return Price;
}
// DisplayInfo(): "[식품] 상품명 / 정가 / 유통기한" 형식으로 출력
public void DisplayInfo()
{
Console.WriteLine($"\n[ 식품 ] \n상품명 : {ProductName}\n정가 : {Price}");
}
// GetDiscountRate() : 할인율 반환
public int GetDiscountRate()
{
return DiscountRate;
}
// GetDiscountedPrice() : 정가 * (100 - 할인율) / 100 반환
public int GetDiscountedPrice()
{
return (Price * (100 - DiscountRate) / 100);
}
}
public class Electronics : IProduct
{
// 필드: 상품명, 정가, 브랜드
private string ProductName;
private int Price;
private string Brand;
// 생성자
public Electronics(string name, int price, string brand)
{
ProductName = name;
Price = price;
Brand = brand;
}
// GetName() : 상품명 반환
public string GetName()
{
return ProductName;
}
// GetPrice() : 정가 반환
public int GetPrice()
{
return Price;
}
// DisplayInfo(): "[전자제품] 상품명 / 정가 / 브랜드" 형식으로 출력
public void DisplayInfo()
{
Console.WriteLine($"\n[ 전자제품 ] \n상품명 : {ProductName}\n정가 : {Price} \n브랜드 : {Brand}");
}
}
public class Cart<T> where T : IProduct
{
// 필드: List<T> _items
List<T> _items = new List<T>();
// Add(T item) : _items에 추가, "{상품명} 장바구니 담김" 출력
public void Add(T item)
{
_items.Add(item);
Console.WriteLine($"\n{item.GetName()} 장바구니 담김");
}
// Remove(T item) : _items에서 제거, "{상품명} 장바구니 제거" 출력
public void Remove(T item)
{
_items.Remove(item);
Console.WriteLine($"\n{item.GetName()} 장바구니 제거");
}
// GetTotalPrice() : 전체 정가 합산 반환
// → item.GetPrice() 사용
public int GetTotalPrice()
{
int tot = 0;
foreach (T item in _items)
{
tot += item.GetPrice();
}
return tot;
}
// GetFinalPrice() : 할인가 합산 반환
// → IDiscountable이면 GetDiscountedPrice() 사용
// → 아니면 GetPrice() 사용
// → is 패턴 활용: if (item is IDiscountable d)
public int GetFinalPrice()
{
int tot = 0;
foreach (T item in _items)
{
if (item is IDiscountable tempItem)
{
tot += tempItem.GetDiscountedPrice();
}
else
tot += item.GetPrice();
}
return tot;
}
// PrintReceipt() : 영수증 출력
// → 각 아이템 DisplayInfo() 호출
// → IDiscountable이면 할인가, 할인율 함께 출력
// → 정가 합계 / 최종 결제액 / 총 할인액 출력
public void PrintReceipt()
{
foreach (T item in _items)
{
item.DisplayInfo();
if (item is IDiscountable tempItem)
{
Console.WriteLine($"할인율 : {tempItem.GetDiscountRate()}\n할인가 : {tempItem.GetDiscountedPrice()}");
}
}
Console.WriteLine($"\n정가 합계 : {GetTotalPrice()}\n최종 결제액 : {GetFinalPrice()} \n총 할인액 : {GetTotalPrice() - GetFinalPrice()}");
}
}
public class Program
{
static void Main()
{
// 식품 장바구니 생성
Cart<Food> myFoodCart = new Cart<Food>();
// Food 3개 추가: 삼각김밥(1200원, 30%), 샌드위치(3500원, 20%), 바나나우유(1800원, 0%)
myFoodCart.Add(new Food("삼각김밥", 1200, 30));
myFoodCart.Add(new Food("샌드위치", 3500, 20));
myFoodCart.Add(new Food("바나나우유", 1800, 0));
// PrintReceipt() 호출
myFoodCart.PrintReceipt();
// 전자제품 장바구니 생성
Cart<Electronics> myElecCart = new Cart<Electronics>();
// Electronics 2개 추가: 무선이어폰(59000원, Sony), USB허브(25000원, Anker)
myElecCart.Add(new Electronics("무선이어폰", 69000, "Sony"));
myElecCart.Add(new Electronics("USB허브", 25000, "Anker"));
// PrintReceipt() 호출
myElecCart.PrintReceipt();
}
}
비동기 프로그래밍(C Sharp)
서로 다른 객체가 서로의 상태를 알아야하는 상황은 굉장히 자주 나오고, 어찌보면 객체지향 프로그래밍의 대부분의 고민이 여기서 비롯되는 것도 같다. 내가 지금껏 개발하며 사용한 패턴이나 전략들의 장단점을 모았다. 싱글턴 클래스 개념 단 한 개의 인스턴스만 존재하도록 하면서, static 으로 전역적인 접근이 가능하도록 하는 클래스. 장점 구현이 매우 빠르다.
람다람쥐
비동기 프로그래밍(C Sharp)


서로 다른 객체가 서로의 상태를 알아야하는 상황은 굉장히 자주 나오고, 어찌보면 객체지향 프로그래밍의 대부분의 고민이 여기서 비롯되는 것도 같다. 내가 지금껏 개발하며 사용한 패턴이나 전략들의 장단점을 모았다. 싱글턴 클래스 개념 단 한 개의 인스턴스만 존재하도록 하면서, static 으로 전역적인 접근이 가능하도록 하는 클래스. 장점 구현이 매우 빠르다.
람다람쥐