繁体   English   中英

实现类对象和Arraylist

[英]implement class object and 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;
     }

}

我有一个主程序,从该程序我将获得该类的所有必需参数。

现在我想要的是将公司名称,代码,shareno和收盘价之类的公司数据存储在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();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM