public class Account implements Transferable{
String id ;
String name ;
int balance ;
static long grandTotal = 0L ;
Account() { }
Account(String id , String name , int balance) {
this.id = id ; this.name = name ; this.balance = balance ;
grandTotal += balance ;
}
Account(String id , String name) {
this(id,name,0);
}
final public void deposit(int value) {
balance += value ; grandTotal += value ;
}
public int withdraw(int value) throws Exception {
if( value > balance) throw new Exception("잔액없음") ;
balance -= value ; grandTotal -= value;
return balance ;
}
public String toString() {
return "id:" + id + ",name:" + name + ",balance:" + balance;
}
public static void main(String[] args) {
Account acc = new Account("111-222-333" , "홍길동" , 500000) ;
Account acc2 = new Account("1234" , "오진우") ; acc2.deposit(8500);
System.out.println(acc2) ;
System.out.println(acc) ;
acc.deposit(50000) ;
try {
acc.withdraw(4500) ;
} catch (Exception e) {
System.out.println(e.getMessage()) ;
}
System.out.println(acc) ;
System.out.println("Grand Total:" + Account.grandTotal) ;
}
@Override
public int calculate(int amount) {
return balance ;
}
@Override
public void transferIn(int value) {
deposit(value) ;
}
@Override
public void transferOut(int value) throws Exception {
withdraw(value) ;
}
}
public class Fund implements Transferable{
private String name ;
private int price ;
private int balance ;
private int cash = 0 ;
public Fund(String name , int price , int balance) {
this.name = name ;
this.price = price ;
this.balance = balance ;
}
public int buy(int amount) {
balance += amount ;
return price * amount ;
}
public int sell(int amount) throws Exception {
if( amount > balance) throw new Exception("보유주식 부족") ;
balance -= amount ;
return amount * price ;
}
public String toString() {
return name + ":" + price + ":" + balance + ":" + (price*balance) ;
}
@Override
public int calculate(int amount) {
return price * amount ;
}
@Override
public void transferIn(int value) throws Exception {
int buyamount = (value+ cash) / price ;
if( buyamount < 0 ) throw new Exception("1주도 살수 없음") ;
cash = (value+cash) % price ;
int total = buy(buyamount) ;
System.out.print("주식 현재 금액:" + total + "보유") ;
}
@Override
public void transferOut(int value) throws Exception {
if( value > getGrandTotal()) throw new Exception("주식가격 미달") ;
int amount = value / price ;
if( value % price != 0 ) amount ++ ;
int total = sell(amount) ;
cash += total - value ;
}
private int getGrandTotal() {
return price * balance + cash ;
}
}
public interface Transferable {
void transferIn(int value) throws Exception ;
void transferOut(int value) throws Exception ;
int calculate(int amount) ;
}
public class ATMMachine {
public static void main(String[] args) {
Fund fund = new Fund("11-22" , 50000 , 1000) ;
Account acc = new Account("111-333" , "KIM" , 45000000);
System.out.println(fund);
System.out.println(acc) ;
try {
transfer(fund , acc , 500000) ;
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(fund);
System.out.println(acc) ;
}
static void transfer(Transferable out , Transferable in , int amount)
throws Exception {
out.transferOut(amount) ;
in.transferIn(amount);
}
}
댓글 없음:
댓글 쓰기