2015-07-19 80 views
0

我已經搜索了互聯網上的問題的答案,我有和似乎無法得到我正在尋找的答案,我有兩個類和應用程序類和一個用戶類,我提示用戶輸入一個新用戶,或者返回已設置爲保存用戶對象的數組列表中的用戶的結果。應用程序結束後,我期望數組列表繼續存放對象,以便在應用程序類中每次連續運行主方法時,都可以引用arrayList以便稍後進行交叉檢查。在arrayList中存儲用戶創建的對象多次迭代

所以我的問題是,當主方法完成,我再次運行它,它是否重新創建所有我的對象和arrayList從頭開始?

下面是我正在使用的兩個類。首先是應用程序類,其次是用戶。

import java.util.ArrayList; 
import java.util.Scanner; 

public class Application{ 
    static Scanner in = new Scanner(System.in); 


    public static void main(String[] args) { 


    //Creating admin user object that will be able to access everything 
     Users admin = new Users(); 
     Users result = null; 


    //creating a new user that is not an admin 
     System.out.println("Are you a new user?"); 
     String answer = null; 
     answer = in.nextLine(); 

     if(answer.equalsIgnoreCase("YES") || answer.equalsIgnoreCase("Y")) { 
      result = admin.addNewUser(); 
      result.addUsertoArrayList(result); 
     }else { 
      result.displayUsers(result.users); 
     } 
    }//End of Main Method. 
}//End of Application Class 

import java.util.ArrayList; 

public class Users extends Application { 

    private String username; 
    private double biWeeklyIncome = 0; 
    private String password; 
    private String email; 

// ArrayList to store all the objects of Type Users. 
    ArrayList<Users> users = new ArrayList<Users>(); 



// Default Constructor. 
    Users() { 
    } 

// Constructor that takes a string as a name parameter. 
    public String name; 

    Users(String name) { 
     this.name = name; 
    } 

// Setter Methods. 

// User name 
    public void setUsername() { 
     System.out.println("Enter the username that you wish to go by:\n Ex.  bigBoss45"); 
     username = in.nextLine(); 
    } 

// Income 
    public void setBiWeeklyIncome() { 
     System.out.println("Enter your current bi-weekly income: \n Ex. 4500.00"); 

     try { 
      biWeeklyIncome = Double.parseDouble(in.nextLine()); 
     } catch (NumberFormatException e) { 
     // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

// Password 
    public void setPassword() { 
     System.out.println("Enter the password that you wish to access your account with:\n Ex. bigBoss45"); 


     password = in.nextLine(); 
    } 

// Email 
    public void setEmail() { 
     System.out.println("Enter a valid email address: \n Ex. [email protected]"); 

     email = in.nextLine(); 
    } 

// Getter Methods 

// User name 
    public String getUsername() { 
     return username; 
    } 

// Income 
    public double getBiWeeklyIncome() { 
     return biWeeklyIncome; 
    } 

// Password 
    public String getPassword() { 
     return password; 
    } 

// Email 
    public String getEmail() { 
     return email; 
    } 

// Method to create a new user 
    public Users addNewUser() { 
     String name = null; 

     System.out.println("Enter the name of the new user\n Ex.John Smith"); 
     name = in.nextLine(); 
    // Creating the new 
     Users newUser = new Users(name); 

    // Setting the new users information 
     newUser.setUsername(); 
     newUser.setPassword(); 
     newUser.setBiWeeklyIncome(); 
     newUser.setEmail(); 

    //adding the new user to the users arrayList 

     displayUsers(users); 

     return newUser; 
}// end of addNewUser method. 

//Method that is going to add a new user to the array List. 
    public void addUsertoArrayList(Users nUser) { 
     users.add(nUser); 
    } 

    public void displayUsers(ArrayList<Users> users) { 
    // Printing out the user added to the array list for testing purposes. 
     for (Users user : users) { 
      System.out.println(user.getUsername()); 
     } 
    }//End of displayUser method. 


} 

我是一種新的Java和所有的面向對象的,所以任何的幫助深表感謝,我感謝你抽出時間來看看我的代碼!

+0

這是通過不結束程序(無限循環)完成的,如果用戶輸入「exit」,則關閉程序,否則執行迭代。如果要永久存儲值,請使用數據庫(例如,如果不想設置數據庫,則使用SQLite)。 – maraca

回答

0

相同,對象存儲內存丟失。

您可以選擇堅持所需的對象。一種方法是在關閉之前將對象序列化爲文件,然後在啓動時重新加載它們。

就你而言,一個簡單的方法是使用Java serialization。您需要將您的可序列化類標記爲實施Serializable,給它們一個serialVersionUID字段,並使用ObjectOutputStreamObjectInputStream

順便說一句,您的Users類表示兩種不同的東西 - 一組用戶和一個用戶。如果每個班級代表一個概念,通常事情會更好。考慮將你的Users類分成兩個類,其中一個包含用戶列表,另一個表示單個用戶。

1

每次運行一個像java Application這樣的命令來運行程序時,您將啓動一個新的java進程。內存中的任何數據都不會從以前執行的任何Java程序中執行。

如果要存儲數據,因此它停留在你的過程中多次執行當應用程序被關閉,你應該考慮外部化到文件或數據庫等