java - implement class object and Arraylist -
i new java , have task do.
i have created class (company
) looks
public class company implements serializable { private string companyname; private string companycode; private int shareno; private double closingrate; /** * initializes newly created company object represents company basic information in program */ public company() { // todo auto-generated constructor stub this.companyname=""; this.companycode=""; this.shareno=0; this.closingrate=0.0; } /** * constructs new company object value of passed in parameters * * @param companyname - name of company * @param companycode - 3 letter code of company * @param shareno- initial value of share issued company * @param closingrate- price of share rate of 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 name of company * * @return value of attribute companyname */ public string getcompanyname() { return companyname; } /** * set name of company new value * * @param companyname - new value of name */ public void setcompanyname(string companyname) { this.companyname = companyname; } /** * return code of company * * @return value of attribute companycode */public string getcompanycode() { return companycode; } /** * set code of company new value * * @param companycode - new value of code */ public void setcompanycode(string companycode) { this.companycode = companycode; } /** * return shares of company * * @return value of attribute shareno */ public int getshareno() { return shareno; } /** * set shareno of company new value * * @param shareno - new value of share number */ public void setshareno(int shareno) { this.shareno = shareno; } /** * return closingrate of * * @return value of attribute closingrate */ public double getclosingrate() { return closingrate; } /** * set closing rate of company new value referring previous day * * @param description - new value of description */ public void setclosingrate(double closingrate) { this.closingrate = closingrate; } /** * return string representation of object in presentable format * * @return string representation of object */ public string tostring() //@override { return "company code: " + companycode + "\ncompany name: " + companyname + "\nnumber of shares: " + shareno + "\nclosing rate: " +closingrate; } }
and have main program getting required parameters class.
now want store company data name, code, shareno , closing price in arraylist, , write file.
i tried cant understand.
public class companydes { private arraylist<company> companyinfo; public companydes() { companyingo = new arraylist<company>(); } }
and got stuck. appreciated!
you need write logic in main(), logic should :
- create company objects
- add company objects arraylist
- read contents arraylist
- write contents file
use following main() code.
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(); } }
Comments
Post a Comment