본문 바로가기
Project/Study | etc

상품 구매 프로그램

by 코젼 2022. 6. 29.
728x90
반응형

💾 소스코드

SellProduct.zip
0.01MB


📃 상품 구매 프로그램을 작성하세요.

💬 클래스
◾ BuyerMain : 메인 클래스
◾ Product : 제품
 - Video : 비디오 제품
 - Computer : 컴퓨터 제품
 - Audio : 오디오 제품
◾ Buyer : 구매자


💬 생성자
◾ Product
- Product(int price)
◾ Video
- super(100)
◾ Computer
- super(200)
◾ Audio
- super(50)


💬 메소드 - Buyer
◾ selectMenu - 메뉴 선택
- 상품 구매
- 상품 환불
- 구매 상품 확인
- 종료
◾ buy - 구입
- 비디오 / 컴퓨터 / 오디오 구입
- 메뉴 재선택
◾ refund - 환불
- 비디오 / 컴퓨터 / 오디오 환불
- 메뉴 재선택
◾ summary - 구매한 물품에 대한 정보 요약 출력


💬 요구사항
◾ 포인트 적립, 환불시 회수
◾ 현재 소유 잔액 부족시 에러 메시지 출력
◾ ArrayList, 다형성, 상속 등 활용

📂seller 패키지

 

📝 BuyerMain 클래스

package seller;

import java.util.Scanner;

public class BuyerMain {
	public static void main(String[] args) {
		System.out.println("\t[ 상품 구매 프로그램을 시작합니다. ]");
		System.out.print("현재 가지고 있는 금액을 입력하세요 >> ");
		Scanner s = new Scanner(System.in);
		int money = s.nextInt();
		
		Buyer b = new Buyer(money);
		b.selectMenu();
	}
	
}

📝 Product 클래스

package seller;

class Product {
	int price; // 제품의 가격

	Product(int price) {
		this.price = price;
	}
}

class Video extends Product {
	Video() {
		super(100);
	}

	public String toString() {
		return "Video";
	}
}

class Computer extends Product {
	Computer() {
		super(200);
	}

	public String toString() {
		return "Computer";
	}
}

class Audio extends Product {
	Audio() {
		super(50);
	}

	public String toString() {
		return "Audio";
	}
}

📝 Buyer 클래스

package seller;

import java.util.ArrayList;
import java.util.Scanner;

//고객, 물건을 사는 사람
class Buyer {
	int money; // 소유금액
	int bonusPoint = 0; // 보너스점수
	final int MAX_NUM = 10;
	static int button = 0;

	Scanner s = new Scanner(System.in);

	ArrayList<Product> item = new ArrayList<Product>(); // 구입한 제품을 저장하는데 사용될 Vector객체

	// 생성자 , 매개변수 1, 소유 금액을 초기화
	Buyer(int money) {
		this.money = money;
	}

	// 1. 메소드, selectMenu - 메뉴 선택
	void selectMenu() {
		Product p1 = new Video();
		Product p2 = new Computer();
		Product p3 = new Audio();
		
		System.out.println("-----------------------------------------------");
		System.out.println("사용할 기능의 숫자를 입력하세요.\n\n1. 상품 구매\n2. 상품 환불\n3. 구매 상품 확인\n4. 종료");
		int number = s.nextInt();

		int num = number;

		switch (num) {
		case 1:
			System.out.println("-----------------------------------------------");
			System.out.println("메뉴 1 선택, 상품을 구매합니다.");
			System.out.println("1. 비디오 구입 (" + p1.price + "만원)" +
						"\n2. 컴퓨터 구입 (" + p2.price + "만원)" +
						"\n3. 오디오 구입 (" + p3.price + "만원)" +
						"\n4. 메뉴 재선택");
			button = s.nextInt();

			switch (button) {
			case 1:
				buy(p1); // 비디오 구입
				selectMenu();
				break;
			case 2:
				buy(p2); // 컴퓨터 구입
				selectMenu();
				break;
			case 3:
				buy(p3); // 오디오 구입
				selectMenu();
				break;
			case 4:
				selectMenu();
				break;
			default:
				System.out.println("잘못 입력하셨습니다.");
				selectMenu();
			}

			break;
		case 2:
			System.out.println("------------------------------------------------------------");
			System.out.println("메뉴 2 선택, 상품을 환불합니다.");
			System.out.println("1. 비디오 환불\n2. 컴퓨터 환불\n3. 오디오 환불\n4. 메뉴 재선택");
			button = s.nextInt();

			switch (button) {
			case 1:
				refund(p1); // 비디오 환불
				selectMenu();
				break;
			case 2:
				refund(p2); // 컴퓨터 환불
				selectMenu();
				break;
			case 3:
				refund(p3); // 오디오 환불
				selectMenu();
				break;
			case 4:
				selectMenu();
				break;
			default:
				System.out.println("잘못 입력하셨습니다.");
				selectMenu();
			}
			break;
		case 3:
			System.out.println("--------------------------------------------");
			System.out.println("메뉴 3 선택, 구매 상품을 확인합니다.");
			summary();
			selectMenu();
			break;
		case 4:
			System.out.println("종료합니다.");
			break;
		default:
			System.out.println("잘못 입력하셨습니다.");
		}
	}

	// 2. 메소드 , buy - 구입
	void buy(Product p) {
		// 잔액이 충분한 지 확인
		if (money < p.price) {
			System.out.println("잔액이 부족합니다.");
			return;
		} else {
			// 상품의 가격만큼 금액을 감소
			money -= p.price;

			// 가격의 10%만큼 포인트 추가
			bonusPoint += (p.price * 0.1);

			// ArrayList 제품 추가
			if (p instanceof Video) {
				item.add(new Video());
			} else if (p instanceof Computer) {
				item.add(new Computer());
			} else if (p instanceof Audio) {
				item.add(new Audio());
			}
			System.out.println(p + "를 구입하셨습니다.");
			System.out.println("현재 잔액은 " + money + "만원이고, 현재 쌓인 포인트는 " + bonusPoint + "포인트 입니다.");

		}
	}

	// 3. 메소드 , refund - 환불
	void refund(Product p) {
		// 제품을 ArrayList에서 제거
		if (p instanceof Video) {
			item.remove(new Video());
		} else if (p instanceof Computer) {
			item.remove(new Computer());
		} else if (p instanceof Audio) {
			item.remove(new Audio());
		}

		// 보너스 환불
		bonusPoint -= (p.price * 0.1);

		// 소유 금액 증가
		this.money += p.price;

		// 알림 메세지
		System.out.println(p + "가 환불되었습니다.");
		System.out.println("현재 잔액은 " + money + "만원이고, 현재 쌓인 포인트는 " + bonusPoint + "포인트 입니다.");

	}

	// 4. 메소드 , summary - 구매한 물품에 대한 정보 요약 출력
	void summary() {
		
		Product p1 = new Video();
		Product p2 = new Computer();
		Product p3 = new Audio();
		
		int sum = 0; // 구입한 물품의 가격합계
		String itemList = ""; // 구입한 물품목록

		// 구매하려는 제품이 없는지(비어있는지) 확인한다.
		if (item.isEmpty()) {
			System.out.println("구입하신 제품이 없습니다.");
			return;
		}

		// 반복문을 이용해서 구입한 물품의 총 가격과 목록을 만든다.
		for(int i=0; i<item.size(); i++) {
			sum +=  item.get(i).price;
			itemList += "" + item.get(i) + ", ";
		}
		System.out.println("구입하신 물품의 총금액은 " + sum + "만원입니다.");
		System.out.println("구입하신 제품은 " + itemList + "입니다.");
	}
}

💻 실행 화면

 

728x90
반응형

댓글