일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- UE4
- 코딩도장
- python
- Project
- Unreal
- C++
- std
- for
- CSS
- 영알남
- 파이썬
- endl
- if
- 알고리즘
- 파이선
- 연결리스트
- cout
- font weight
- Euler
- codecademy
- bool
- html5
- html
- Notting hill
- cow
- Polymorphism
- unreal engine4
- Bull
- dictation
- Java
Archives
- Today
- Total
다형성 예제3 (Vector 추가) 본문
package javafuture;
import javafx.scene.Parent;
import java.util.*;
public class Example {
public static void main(String[] args) {
Buyer b = new Buyer();
TV tv=new TV();
Computer com=new Computer();
Audio audio =new Audio();
b.buy(tv);
b.buy(com);
b.buy(audio);
System.out.println(b.bonusPoint);
System.out.println(b.money);
b.summary();
System.out.println();
b.refund(com);
b.summary();
}
}
class Product {
int price;
int bonusPoint;
Product(int price) {
this.price = price;
bonusPoint = (int) (price / 10);
}
Product() {
}
}
class TV extends Product {
TV() {
super(100);
}
@Override
public String toString() {
return "TV";
}
}
class Computer extends Product {
Computer() {
super(200);
}
@Override
public String toString() {
return "Computer";
}
}
class Audio extends Product {
Audio() {
super(50);
}
@Override
public String toString() {
return "Audio";
}
}
class Buyer {
int money = 1000;
int bonusPoint = 0;
Vector item=new Vector();
int i = 0;
void buy(Product p) {
if (this.money < p.price) {
System.out.println("No Money");
return;
}
this.money -= p.price;
this.bonusPoint += p.bonusPoint;
item.add(p);
System.out.println("You bought " + p);
}
void refund(Product p){
if(item.remove(p)){
this.money+=p.price;
this.bonusPoint-=p.bonusPoint;
System.out.println("You refunded "+p);
}else
System.out.println("You didn't bought anything");
}
void summary() {
int sum = 0;
String itemList = "";
if(item.isEmpty()){
System.out.println("No Products");
return;
}
for(int i=0;i<item.size();i++){
Product p=(Product)item.get(i);
sum+=p.price;
itemList+=(i==0)? ""+p:", "+p;
}
System.out.println("sum : "+sum);
System.out.println("products : "+itemList);
}
}
'Programming > Java' 카테고리의 다른 글
다형성 예제 2 (객체 배열 추가) (0) | 2018.01.19 |
---|---|
polymorphism (다형성) Example (0) | 2018.01.19 |
피보나치 (1) | 2018.01.17 |
단축키 (0) | 2018.01.13 |
Java의 정석 공부 (0) | 2018.01.13 |
Comments