treemap.zip

public class AccountManagementTreeMap {
 private TreeMap<AccountNumber, AccountTreeMap> accounts;
 //TreeMap 을 사용하여 Key값에는 AccountNumber 클래스를 Value 에는 AccountTreeMap 클래스를 
 public AccountManagementTreeMap() {
  accounts = new TreeMap<AccountNumber, AccountTreeMap>(
    new AccountNumber());
 }
 // AccountNumber을 Comparator에 사용하기 위해 new를 생성하여 객체를 집어 넣는다.

 public void insertAccount(float money, String accountNumber,
   String accountName) {
  accounts.put(new AccountNumber(accountNumber), new AccountTreeMap(
    money, accountNumber, accountName));
 //Treemap 구조인 hash 에 갑을 넣기 위해put 을 사용하며 AccountNumber클래스 안에 있는
 //accountNumber을 사용해야 하기 때문에 new AccountNumber을 사용하여 생성자 값 리턴을 한다.
 }

 public void setDeposite(float money, String accountNumber) {
  if (accounts.containsKey(accountNumber)) {
   //accounts 에 존재하는 key값을 accountnumber로 찾는다.
   AccountTreeMap account = accounts.get(accountNumber);
   //찾은 value 를 가져오기 위해 key값을 저장 하고 money를 저장한다.
   account.deposite(money);
   return;
  }
  System.out.println("there is no account");
 }

 public void setWithdraw(float money, String accountNumber) {
  if (accounts.containsKey(accountNumber)) {
   AccountTreeMap account = accounts.get(accountNumber);
   account.withdraw(money);
   return;
  }
  System.out.println("there is no account");
 }

 public void displayAll() {

  Iterator ir = accounts.keySet().iterator();
  //accounts 의 키값을 저장 next next 하기위해 Iterator사용
  while (ir.hasNext()) {
   AccountNumber accountNumber = (AccountNumber) ir.next();
   // Key 값인 AccountNumber를 key 값으로 저장 하기 위해 accountNumber로 선언
   AccountTreeMap account = accounts.get(accountNumber);
   // AccountTreeMap을 account 변수로 선언 하여 key 주소를 가져옴   
   System.out.println(account);
  }
 }

}


public class AccountNumber implements Comparator{
 //key 값으로 사용하는 class
 String accountNumber;
 
 public AccountNumber() {}
 //정렬을 할 객체를 사용할때의 생성자
 public AccountNumber(String accountNumber2) {
  // TODO Auto-generated constructor stub
  accountNumber = accountNumber2;
  //AccountNumber 의 key를 리턴하기 위해 만든 생성자
 }


 @Override
 public int compare(Object o1, Object o2) {
  // TODO Auto-generated method stub
  String ac1  = (((AccountNumber)o1).accountNumber).toString();
  String ac2  = (((AccountNumber)o2).accountNumber).toString();
  
  //Obejct를 다운캐스팅 하여 AccountNumber 로 변환 그리고 accountNumber변수를 사용한다.
  
  return ac1.compareTo(ac2);
  }

}


public class AccountTreeMap {
 private float balance;
 private String accountNumber;
 private String accountName;
 
 public AccountTreeMap(){}
 public AccountTreeMap(float balance, String accountNumber, String accountname){
  this.balance = balance;
  this.accountNumber = accountNumber;
  this.accountName = accountname; 
 }
 
 public void withdraw(float amount){
  if(amount > balance){
   System.out.println("not enough money");
  }
  else
   balance -=amount;
 }
 public void deposite(float amount){
  if(amount <= 0){
   System.out.println("입금불가");
  }
  else
   balance += amount;
 }
 
 public float getBalance() {
  return balance;
 }

 public String getAccountNumber() {
  return accountNumber;
 }

 public String getAccountName() {
  return accountName;
 }
 public String toString(){
  return accountName + "님의 현재 계좌번호는" + accountNumber +
    "이며, 현재 잔고는" + balance + "입니다";
 }

}

 


 

 

 

'개발자 > Java' 카테고리의 다른 글

Java 변수 메모리가 가르키는 위치  (0) 2013.02.12
Java 제너릭 사용 예제  (0) 2013.01.30
Access Constrol ( 접근제어 ) 3가지  (0) 2013.01.28
Constructor  (0) 2013.01.28
Class 와 메모리  (0) 2013.01.27
블로그 이미지

김진리

,