2014-01-20 52 views
-2

我一直被困在相同的問題幾天,並搜索了一切我能想到的,我放棄了。我正在嘗試編寫一個程序,您應該先創建一個人物對象,然後才能爲該人員提供不同類型的所有物。我將每個創建人都放置在一個數組列表中,每個人都應該有自己的庫存,這也是一個數組列表。我只是不明白如何將項目添加到特定的人數列表中以及如何打印數組列表。每次創建新人時,我是否必須創建一個新的數組列表「物品」實例?以及如何訪問某個人的數據列表?欣賞任何形式的反饋,因爲我是超級noob。如何訪問屬於陣列列表中的對象的數組列表

import java.util.ArrayList; 


public class Sakregister extends Programskelett{ 

public static ArrayList<Person> personList = new ArrayList<Person>(); 


protected boolean nextCommand(){ 
    String command = readString("> ").toLowerCase(); 

    switch(command){ 
    case "print list": 
     printAll(); 
     System.out.println("Program is running"); 
    break; 

    case "print person": 
     printUser(); 
     System.out.println("Program is running"); 
    break; 

    case "create item": 
     newItem(); 

    break; 

    case "create user": 
     newUser(); 
    break; 

    case "print richest": 
     printRichest(); 
     System.out.println("Program is running"); 
    break; 

    case "crash": 
     initCrash(); 

    break; 

    case "quit": 
     System.out.println("Program has terminated"); 
     return true; 
    default: 
     System.out.println("Not a valid command"); 
} 
    return false; 
} 

private void printAll() { 



    } 
private void initCrash() { 


    for (Person thisPerson : personList) { 
     for (Item thisItem : thisPerson.belongings) 
      if (thisItem.name == "Stock"){ 
       ((Stock) thisItem).setStockCrash(0); 
      } 
      } 
      } 

private void printRichest() { 


} 

private void newUser() { 
    System.out.println("enter name: "); 

    String name = keyboard.nextLine(); 

    Person newPerson = new Person(name); 


    personList.add(newPerson); 



    System.out.println("Person added to list"); 
} 

private boolean newItem() { 

    System.out.println("enter item type: "); 

    String itemType = readString("> ").toLowerCase(); 

    switch(itemType){ 

    case "trinket": 

     addTrinket(); 

     break; 

    case "stock": 

     addStock(); 

     break; 

    case "appliance": 

     addAppliance(); 

     return true; 
    default: 
     System.out.println("Not a valid item type"); 
} 
    return false; 
    } 

private void addAppliance() { 

    System.out.println("Enter name of appliance: "); 

    String appName = keyboard.nextLine(); 

    System.out.println("Enter initial price: "); 

    int appInitialPrice = keyboard.nextInt(); 

    System.out.println("Enter level of wear: "); 

    int appWear = keyboard.nextInt(); 

    Appliance newAppliance = new Appliance(appName, appInitialPrice, appWear); 

    System.out.println("Enter name of owner: "); 

    Object owner = keyboard.nextLine(); 

    for(Person entry : personList) 
       if(entry.equals(owner)) 
      entry.belongings.add(newAppliance); 


} 

private void addStock() { 

    System.out.println("Enter name of stock entry: "); 

    String stockName = keyboard.nextLine(); 

    System.out.println("Enter amount: "); 

    int stockAmount = keyboard.nextInt(); 

    System.out.println("Enter price: "); 

    int stockPrice = keyboard.nextInt(); 

    Stock newStock = new Stock(stockName, stockAmount, stockPrice); 

    System.out.println("Enter name of owner: "); 

    String owner = keyboard.nextLine(); 

    keyboard.nextLine(); 

    for(Person entry : personList) { 
     if(entry.equals(owner)) { 
      entry.belongings.add(newStock); 

    } 

    } 
} 

private void addTrinket() { 

    System.out.println("Enter name of trinket: "); 

    String trinketName = keyboard.nextLine(); 

    System.out.println("Enter number of gems: "); 

    int trinketGems = keyboard.nextInt(); 

    System.out.println("Choose gold or silver: "); 

    String trinketMineral = keyboard.nextLine(); 

    keyboard.nextLine(); 

    Trinket newTrinket = new Trinket(trinketName, trinketGems, trinketMineral); 

    System.out.println("Enter name of owner: "); 

    String owner = keyboard.nextLine(); 

    for(Person entry : personList) 

     if(entry.equals(owner)) 
      entry.belongings.add(newTrinket); 



} 

private void printUser() { 
    // TODO Auto-generated method stub 

} 

protected void printMainMenu(){ 
    System.out.println("Choose a command: "); 
    System.out.println("start"); 
    System.out.println("quit"); 
} 

public static void main(String[] args) { 

    Sakregister registerProgram = new Sakregister(); 
    registerProgram.run(); 

} 

} 

public class Item{ 

protected String name; 

public Item(String name){ 

    this.name = name; 
} 

public String getItemName() { 
    return name; 
} 

    import java.util.ArrayList; 



    public class Person{ 

public String name; 
public String items; 

public ArrayList<Item> belongings = new ArrayList<Item>(); 

public Person(String name){ 

    this.name = name; 
} 

public String getName(){ 
    return name; 

} 


public String toString() { 
    return "Name: " + name; 
} 
    } 
+1

這看起來像一個家庭作業。 –

回答

0

ArrayList<Item> belongings的新實例已被每次創建新的對象Person時創建的,所以你不需要任何地方重新創建它。但是,belongings目前沒有吸氣劑,所以您需要編寫一個訪問給定人的物品。你想是這樣的:

public ArrayList<Item> getBelongings() { 
    return belongings; 
} 

你需要編寫一個接受一個Item對象和add這跟那個Personbelongings的公共方法。您不需要幫助編寫另一種直接調用System.out.println()的公共方法,或者遍歷它們並將其打印出來。

0

「我只是不知道如何將項目添加到一個特定的人ArrayList和如何打印的ArrayList」

你的人是在personList,所以我會用personList.get(n)獲得第n個人物。 要將項目添加到belongings,您可以使用personList.get(n).belongings.add(item)

要打印,你可以使用普通的foreach循環一個ArrayList:

for(Item i:personList.get(n).belongings){ 
System.out.println(i.getItemName()); 
} 

我一定要創建ArrayList的「家當」每一次的新實例創建一個新的人嗎?

ArrayList belongingsPerson類中的字段。當你用構造函數創建一個人時,所有的字段和變量都在內存中創建。每次創建人時都會發生這種情況,因此每個對象(在personList中的個案人員)都有自己的字段,如belongings列表。

+0

get(n)只對索引有效嗎?如果我只知道他們的名字,我如何訪問某人? – user3200867

+0

你可以通過數組搜索找到具有正確名稱的Person:'for(Person p:personList){if(p.getName()。equals(「person」)){// do something}}' – tly