将对象保存在另一个类中的arraylist中

时间:2014-12-19 00:08:52

标签: java arraylist

您好我需要个人项目的帮助。我正在开发一个软件来管理节目(事件),我有问题要保存并反对我的课堂内的arraylist。 我有3个对象,艺术家,事件,合同,每次用户签订合同,在艺术家和代理之间,我希望能够将艺术家保存到代理类内的agentContract中。因为我需要每个代理人的艺术家名单。我的问题是每次我尝试保存时,我的程序只保存第一个代理中的所有艺术家。有人可以帮助我吗?

这是我的艺术家课程:

public class Agent extends Person implements Serializable {
float dueAmount;
int percentage;
public static ArrayList<Artist> AgentArtists = new ArrayList<Artist>();

public Agent() 
{
    super();
    dueAmount = 0;
    percentage = 0;
}

.....    .....

public static ArrayList<Artist> getAgentArtists() 
{
    return AgentArtists;
}

public void setAgentArtists(Agent age,Artist art) 
{
    age.AgentArtists.add(art);
}

我使用此方法签署合同并将艺术家保存到座席的艺术家列表中

public void SignWithArtist(Artist art, Agent age) 
{
    if (age.getAgentArtists().add(art) == true) 
    {
        System.out.println("\n" + art.getName() + " is signing the contract\n");
        ProgManage.PauseScreen(1000);

        for(int i=0; i<=1; i++)
        {
            System.out.print("*");
            ProgManage.PauseScreen(1000);
            System.out.print("**");
            ProgManage.PauseScreen(1000);
            System.out.print("***");
            ProgManage.PauseScreen(1000);
            System.out.print("****");
            ProgManage.PauseScreen(1000);
        }

        System.out.println("\nContract signed by " + art.getName());
        ProgManage.PauseScreen(1000);

    } else {
        System.out.println("\n*ERROR* Artist not added");
        ProgManage.PauseScreen(1000);
    }
}

1 个答案:

答案 0 :(得分:0)

试试这段代码:

public class Agent extends Person implements Serializable {
  float dueAmount;
  int percentage;
  public ArrayList<Artist> agentArtists;

  public Agent() 
  {
    super();
    agentArtists = new ArrayList<Artist>();
    dueAmount = 0;
    percentage = 0;
  }

  public ArrayList<Artist> getAgentArtists() 
  {
    return agentArtists;
  }

  public void setAgentArtists(Artist art) 
  {
    agentArtists.add(art);
  }
}