java - 实现类对象和Arraylist

标签 java file-io arraylist

我是 Java 新手,我有任务要做。

我创建了一个类(Company),如下所示

 public class Company implements Serializable {
        private String companyName;
        private String companyCode;
        private int shareNo;
        private double closingRate;

        /**
         * Initializes a newly created company object so that it represents an      company basic information  in the program
     */

    public Company() {
        // TODO Auto-generated constructor stub
        this.companyName="";
        this.companyCode="";
        this.shareNo=0;
        this.closingRate=0.0;

    }
    /**
     * Constructs a new company object with the value of the passed in parameters
     * 
     * @param companyName - the name of company
     * @param companyCode - the three letter Code of the company
     * @param shareNo- the initial value of share issued by the company
     * @param closingRate- the price of the share rate of the previous day
     */
    public Company(String companyName,String companyCode,int shareNo,double closingRate)
    {
        this.companyName=companyName;
        this.companyCode=companyCode;
        this.shareNo=shareNo;
        this.closingRate=closingRate;
    }

    /**
     * Return the name of the company
     * 
     * @return the value of the attribute companyName
     */
    public String getCompanyName()
    {
        return companyName;
    }
    /**
     * Set the  name of company to a new value
     * 
     * @param companyName - new value of name
     */
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    /**
     * Return the code of the company
     * 
     * @return the value of attribute companyCode
     */public String getCompanyCode()
     {
         return companyCode;
     }
     /**
      * Set the code of company to a new value
      * 
      * @param companycode - new value of code
      */
     public void setCompanyCode(String companyCode) {
         this.companyCode = companyCode;
     }
     /**
      * Return the shares  of the company
      * 
      * @return the value of attribute shareNo
      */
     public int getShareNo()
     {
         return shareNo;
     }
     /**
      * Set the shareNo of company to a new value
      * 
      * @param shareNo - new value of share number
      */
     public void setShareNo(int shareNo) {
         this.shareNo = shareNo;
     }
     /**
      * Return the closingRate of the 
      * 
      * @return the value of attribute closingRate
      */
     public double getClosingRate()
     {
         return closingRate;
     }
     /**
      * Set the closing rate of company to a new value referring to the previous day
      * 
      * @param description - new value of description
      */
     public void setClosingRate(double closingRate) {
         this.closingRate = closingRate;
     }

     /**
      * Return a String representation of the object in a presentable format
      * 
      * @return a String representation of the object
      */
     public String toString() //@override
     {
         return "Company Code: " + companyCode + 
                 "\nCompany Name: " + companyName +
                 "\nNumber of Shares: " + shareNo +
                 "\nClosing Rate: " +closingRate;
     }

}

我有主程序,我将从中获取该类所需的所有参数。

现在我想要的是将公司数据(如名称、代码、股票号和收盘价)存储在 ArrayList 中,然后写入文件。

我尝试过类似的东西,但无法理解。

public class Companydes {

    private ArrayList<Company> companyinfo; 
    public Companydes()
    {
        companyingo = new ArrayList<Company>();
    }
}

然后被卡住了。帮助将不胜感激!

最佳答案

您需要在 main() 中编写逻辑,该逻辑应该:

  • 创建公司对象
  • 将公司对象添加到 ArrayList
  • 从ArrayList读取内容
  • 将内容写入文件

使用以下 main() 代码。

public static void main(String[] args) {
        try {

            List<Company> companyList = new ArrayList<Company>();

            Company c1 = new Company("Test Company","111",111,89077.0);
            companyList.add(c1);
            Company c2 = new Company("Non Test Company","22",222,077.0);
            companyList.add(c2);

            String content=null;
            for(Company company:companyList)
            content+= company;

            File file = new File("D://output.txt");


            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

关于java - 实现类对象和Arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30637186/

相关文章:

java - 请求后发生 "javax.ws.rs.NotFoundException: HTTP 404 Not Found"

c++ - 使用C++将不规则文件读入二维数组

java - 输入不正确的字符时无法清除文本字段

java - 自动提交 OpenJPA 写入

java - BufferedWriter在Android性能中的应用

java - 从 ArrayList 中检索样本

java - 根据给定条件从 ArrayList 中删除对象

java - ArrayList<Item> (其中 Item 是内部类)添加错误

java - 创建错误条件并将其传回客户端

c - 对这种行为有什么解释吗?