https://s3-us-west-2.amazonaws.com/secure.notion-static.com/126a4de4-11f1-4f2c-8b3c-2c6cc489f671/final0505.mp4

public class Main {
public static void main(String[] args) {
//Tesla, Samsung, KRW, USD 인스턴스 생성
Asset[] asset = new Asset[] { new Tesla(), new Samsung(), new KRW(), new USD() };
//총 자산 계산
int totalAsset = 0;
int stockSum = 0;
int cashSum = 0;
for (int i = 0; i < asset.length; i++) {
totalAsset += asset[i].getTotalValue();
if (asset[i] instanceof Stock) {
stockSum += asset[i].getTotalValue();
} else if (asset[i] instanceof Cash) {
cashSum += asset[i].getTotalValue();
}
asset[i].printInfo();
}
//총 자산 출력
System.out.println("stockSum = " + stockSum);
System.out.println("cashSum = " + cashSum);
System.out.println("totalAsset = " + totalAsset);
//주식, 현금 비중 계산
System.out.println("stockWeight = " + (stockSum / (float) totalAsset * 100) + "%");
System.out.println("cashkWeight = " + (cashSum / (float) totalAsset) * 100 + "%");
}
}
//추상 클래스 상속
public abstract class Asset {
public abstract int getTotalValue();
public abstract int getCount();
public abstract int getPrice();
public abstract void printInfo();
}
public class USD extends Cash {
//가치, 보유 자산 입력
private int price = 1100;
private int count = 15;
@Override
public int getCount() {
return count;
}
@Override
public int getPrice() {
return price;
}
@Override
public int getTotalValue() {
return price * count;
}
@Override
public void printInfo() {
System.out
.println(String.format("name = USD, price = %d, count = %d, total = %d", price, count, price * count));
}
}