[상속-12] Cart 연습문제

seratpfk·2022년 8월 1일
0

JAVA

목록 보기
63/96

연습문제

  • 클래스: Product, Snack, Meat, Milk, Customer
  • Snack, Meat, Milk는 모두 Product이다.
  • 모든 Product는 이름(name)과 가격(price)만 가진다.
  • 고객(Customer)은 모든 Product를 10개 담을 수 있는 cart를 가진다.
  • 고객은 돈(money)와 보너스포인트(bonusPoint)를 가진다.
Customer customer = new Customer();  // 아직 돈이 없는 고객
customer.setMoney(10000);  // 10000원이 생긴 고객
customer.buy(new Snack("홈런볼", 1500)); // 1500원짜리 홈런볼을 산다. (카트에 담는다.)
customer.buy(new Meat("한우", 5000));  // 5000원짜리 한우를 산다
customer.buy(new.Milk("서울우유", 2500));  // 2500원짜리 서울우유를 산다.
customer.receipt();  // 영수증을 본다.
/*
홈런볼 1500원
한우 5000원
서울우유 2500원
--------------
구매총액 9000원
보너스 900원
남은돈 1000원
*/

Product클래스 생성(메인메소드 없음)

public class Product {
	private String name;
	private int price;
    private int total;
	public Product(String name, int price) {
		super();
		this.name = name;
		this.price = price;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
}

Snack클래스 생성(메인메소드 없음)

public class Snack extends Product {
	public Snack(String name, int price) {
		super(name, price);
	}
}

Meat클래스 생성(메인메소드 없음)

public class Meat extends Product {
	public Meat(String name, int price) {
		super(name, price);
	}
}

Milk클래스 생성(메인메소드 없음)

public class Milk extends Product {
	public Milk(String name, int price) {
		super(name, price);
	}
}

Customer클래스 생성(메인메소드 없음)

public class Customer {
	private int money;
	private int bonusPoint;
	private int total;
	private Product[] cart = new Product[10];
	private int idx; // 카트에 담긴 물건 수, cart 배열의 인덱스
	// 생성자 생략
	// 메소드
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	public int getBonusPoint() {
		return bonusPoint;
	}
	public void setBonusPoint(int bonusPoint) {
		this.bonusPoint = bonusPoint;
	}
		// buy() 메소드
		public void buy(Product product) {
		int price = product.getPrice();
		// 가진 돈보다 비싼 물건은 못 산다.
		if(money < price) {
			System.out.println(product.getName() + "사려면 돈이 " + (price - money) + "원 부족합니다.");
			return;
		}
	    // 카트가 가득 차면 물건을 못 산다.
		if(idx == cart.length) {
			System.out.println("카트가 가득 찼습니다.");
			return;
		}
		// 구매
		cart[idx++] = product;
		money -= price;
		total += price;
		bonusPoint += price * 0.1;
		}
	        // receipt() 메소드
			public void receipt() {
			// 물건을 안 샀다.
			if(idx == 0) {
				System.out.println("구매한 물건이 없습니다.");
				return;
			}
			// 구매 총액 구하기 및 출력
			for(int i = 0; i < idx; i++) {
				Product product = cart[i];
				System.out.println(product.getName() + "\t" + product.getPrice() + "원");
				total += product. getPrice();
			}
			System.out.println("*******************");
			System.out.println("구매총액 " + total + "원");
			System.out.println("보너스 " + bonusPoint + "원");
			System.out.println("남은돈 " + money + "원");	
	}	
}

Main클래스 생성(메인메소드 생성)

public class Main {
	public static void main(String[] args) {
		Customer customer = new Customer();
		customer.setMoney(10000);
		customer.buy(new Snack("홈런볼", 1500));
		customer.buy(new Meat("한우", 5000));
		customer.buy(new Milk("서울우유", 2500));
		customer.buy(new Meat("불고기", 5000));
		customer.receipt();
	}
}

출력:
불고기사려면 돈이 4000원 부족합니다.
홈런볼 1500원
한우 5000원
서울우유 2500원


구매총액 18000원
보너스 900원
남은돈 1000원

0개의 댓글