我是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中存储公司数据,如名称,代码,shareno和收盘价,然后写入文件。
我尝试过这样的事情但是无法理解。
public class Companydes {
private ArrayList<Company> companyinfo;
public Companydes()
{
companyingo = new ArrayList<Company>();
}
}
然后卡住了。帮助将不胜感激!
答案 0 :(得分:1)
你需要在main()中编写逻辑,该逻辑应该:
使用以下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();
}
}