2012-03-28 2 views
0

기본적으로 "은행"프로그램을 만들려고합니다..dat 파일에서 객체를 읽는 방법을 궁금합니다.

지금까지 (내가 생각하는) 나중에 사용할 목적으로 .dat 파일에 개체를 쓰는 방법을 얻었고 이전에 만든 계정에 액세스 할 수 있도록 시작할 때마다 이러한 개체를 ArrayList로 읽고 싶었습니다. .

import java.util.*; 
    import java.io.*; 

    public class mainBank 
    { 
private static ArrayList<Account> userList = new ArrayList<Account>(); 
private static int userNum; 
private static Scanner scan = new Scanner(System.in); 
public static void main(String args[]) throws IOException 
{ 
    try 
    { 
     readFromArray(); 
    } 
    catch(Exception e) 
    { 
     e.getStackTrace(); 
    } 
    System.out.println("Welcome to the bank"); 
    System.out.println("Account ID"); 
     int uID = scan.nextInt(); 
    Account currAccount = new Account((Account)userList.get(uID)); 

    nextAction(currAccount); 

} 

public static void nextAction(Account acc) 
{ System.out.println(acc); 

    System.out.print("Are you happy with this y/n? "); 
    String input = scan.nextLine(); 
if(input.toLowerCase().equals("y")) 
{ 
    System.out.println("Thank you Come Again"); 
} 
else 
{ 
    System.out.println("What would you like to do?"); 
    System.out.println("1.Deposit"); 
    System.out.println("2.Widthdraw"); 
    System.out.println("3.Exit"); 
     int choice = scan.nextInt(); 
    switch(choice) 
    { 
    case 1: System.out.print("Deposit Ammount: "); 
      acc.deposit(scan.nextInt()); 
      nextAction(acc); 

      break; 

    case 2: System.out.print("Widthdrawl Ammount: "); 
      try{acc.withdraw(scan.nextInt());} 
      catch(Exception e){System.out.println("not enough money");} 
      nextAction(acc); 

      break; 

    case 3: System.out.print("Okay, Good Bye!"); 
      break; 
    } 
} 


} 
public static void readFromArray() throws IOException, Exception 
{ 
    FileInputStream fis = new FileInputStream("data.dat"); 
    ObjectInputStream ois = new ObjectInputStream(fis); 
    Account acc = new Account((Account) ois.readObject()); 


    for(int i = 0; i<userNum;i++) 
    {  
     acc = (Account) ois.readObject(); 
     userList.add(acc); 
    } 
} 
public static void writeToFile() throws IOException 
{ 
    FileOutputStream fos = new FileOutputStream("data.dat"); 
    ObjectOutputStream oos = new ObjectOutputStream(fos); 


    for(int i =0;i<userList.size();i++) 
    { 
     Account acc = userList.get(i); 
     oos.writeObject(acc); 
    } 
    oos.flush(); 
    oos.close(); 
} 
    } 

Account 클래스 :

내가 mainBank.main (mainBank.java:22)에서 "java.lang.IndexOutOfBoundsException"여기

가 계속이 mainBank 코드입니다

import java.io.Serializable; 

    public class Account implements Serializable 
    { 
private int MONEY; 
private String NAME; 
private String PASSWORD; 

public Account(Account acc) 
{ 
    MONEY = acc.getMoney(); 
    NAME = acc.getName(); 
    PASSWORD = acc.getPassword(); 
} 
private String getPassword() 
{ 
    return PASSWORD; 
} 
public Account(String n,String p,int m) 
{ 

    MONEY = m; 
    NAME =n; 
    PASSWORD = p; 
} 

public void setMoney(int m) 
{ 
    MONEY = m; 
} 
public void setName(String n) 
{ 
    NAME=n; 
} 
public void deposit(int m) 
{ 
    MONEY +=m; 
} 
public void withdraw(int m) throws NotEnoughMoneyException 
{ 
    if(MONEY-m<0) 
     throw new NotEnoughMoneyException(); 
    else 
     MONEY -=m; 
} 

public int getMoney() 
{ 
    return MONEY; 

} 
public String getName() 
{ 
    return NAME; 

} 

public String toString() 
{ 
    String s = getName()+":"+getMoney(); 
    return s; 
} 

    } 

    class NotEnoughMoneyException extends Exception 
    { 
String msg; 

NotEnoughMoneyException() 
{ 
    msg = "Not enough Money"; 
} 
    } 

답변

0

System.in에 무엇을 입력했는지 알지 못하는 경우 배열의 경계를 벗어난 값을 입력하게됩니다. ArrayList가 인덱스 0에서 시작된다는 것을 두 번 확인하면됩니까? 따라서 ArrayList에 5 개의 객체가 있으면 ArrayList의 색인이 0-4가됩니다.

+0

정말 숙제가 아니라, 그냥 내 자신의 뜻대로 JAVA를 연습하고, 팩보다 앞서 가려고합니다. System.in 입력에 의해 ArrayList 또는 그와 비슷한 내용을 말하고 있습니까? – nasir

+0

예외 상태로 22 행의 userList에 액세스하려고하면 indexOutOfBoundsException이 발생합니다. 거기에 어떤 가치가 있습니까? 배열 자체의 크기를 벗어나는 값을 넣고 있습니다. – DavidB

관련 문제