java - Trouble with toString method -
why won't variables basica (crank) , basicb (pedal) show in arraylist display along other values? there's class called part can post if needed, seems ignoring variables.
main class
package main; import java.util.arraylist; import java.util.list; import java.util.scanner; public class testassembledpart { public static void main(string[] args) { // todo auto-generated method stub scanner sc = new scanner(system.in); list<assembledpart> aparts = new arraylist<assembledpart>(); aparts.add(new assembledpart("a200", "crank & pedal", 10, 3.5, "crank", "pedal")); system.out.println("part before stock level change - start"); system.out.println(assembledpart.toassembledstring(aparts)); } }
assembledpart class
package main; import java.util.*; public class assembledpart extends part { private string basica; private string basicb; public assembledpart(string id, string name, int stocklevel, double unitprice, string basica, string basicb) { super(id, name, stocklevel, unitprice); this.basica = basica; this.basicb = basicb; } public string toassembledstring(string id, string name, int stocklevel, double unitprice) { return id + " | " + name + " | " + stocklevel + " | " + unitprice + " | " + basica + " | " + basicb; } public static string toassembledstring(collection<assembledpart> aparts){ string s = ""; (assembledpart apart: aparts){ s += apart + "\n"; } return s; } }
part class
package main; import java.util.*; public class part { private string id; private string name; private int stocklevel; private double unitprice; private int qty = 6000; public part(string id, string name, int stocklevel, double unitprice){ this.id = id; this.name = name; this.stocklevel = stocklevel; this.unitprice = unitprice; } string partsavailable() { //string newline = system.getproperty("line.separator"); return (id + "\t" + name + "\t " + stocklevel + "\t\t " + unitprice); } public string getid() { return id; } public void setid(string id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public int getstocklevel(){ return stocklevel - qty; } public void setstocklevel(int stocklevel){ this.stocklevel = stocklevel; } public double getunitprice(){ return unitprice; } public void setunitprice(double unitprice){ this.unitprice = unitprice; } public void replenish(int qty){ this.stocklevel = stocklevel + qty; } public double supply(int qty){ return unitprice * qty; } public string tostring() { return id + " | " + name + " | " + stocklevel + " | " + unitprice; } public static string tostring(collection<part> parts){ string s = ""; (part part: parts){ s += part + "\n"; } return s; }
}
because not calling toassembledstring() method, calling tostring() method, (i expect) implemented in part class.
instead this:
for (assembledpart apart: aparts){ s += apart + "\n";
do this:
for (assembledpart apart: aparts){ s += apart.toassembledstring() + "\n";
then, should avoid parameters in instance methods:
public string toassembledstring(string id, string name, int stocklevel, double unitprice) { return id + " | " + name + " | " + stocklevel + " | " + unitprice + " | " + basica + " | " + basicb; }
do follows:
public string toassembledstring() { return super.tostring() + " | " + basica + " | " + basicb; }
thanks @parker_halo comment
Comments
Post a Comment