The data which was previously stored in an array list , is getting replaced by updated data.
code is shown below
public class Telivision {
private String tvBrand;
private Double tvCost;
private Integer tvDimension;
private String tvScreen;
public String getTvBrand() {
return tvBrand;
}
public void setTvBrand(String tvBrand) {
this.tvBrand = tvBrand;
}
public Double getTvCost() {
return tvCost;
}
public void setTvCost(String brand) {
if(this.tvBrand.equalsIgnoreCase("Samsung")){
this.tvCost = 100*1.5;
}else if(this.tvBrand.equalsIgnoreCase("Sony")){
this.tvCost = 100*2.0;
}
}
public Integer getTvDimension() {
return tvDimension;
}
public void setTvDimension(Integer tvDimension) {
this.tvDimension = tvDimension;
}
public String getTvScreen() {
return tvScreen;
}
public void setTvScreen(String tvScreen) {
this.tvScreen = tvScreen;
}
@Override
public String toString() {
return "Telivision [tvBrand=" + tvBrand + ", tvCost=" + tvCost + ", tvDimension=" + tvDimension + ", tvScreen="
+ tvScreen + "]";
}
TESTER IS AS SHOWN BELOW
public class TelivisionTester {
public static void main(String[] args) {
Telivision telivision = new Telivision();
ArrayList<Telivision> telList = new ArrayList<Telivision>();
telivision.setTvBrand("SAMSUNG");
telivision.setTvDimension(40);
telivision.setTvScreen("Led");
telivision.setTvCost("Samsung");
telList.add(telivision);
System.out.println(telList);
System.out.println(telivision.getTvBrand()+"Cost is "+telivision.getTvCost());
telivision.setTvBrand("Sony");
telivision.setTvDimension(36);
telivision.setTvScreen("Led");
telivision.setTvCost("Sony");
telList.add(telivision);
System.out.println(telList);
System.out.println(telivision.getTvBrand()+"Cost is "+telivision.getTvCost());
}
the output which is expected is as shown below
[Telivision [tvBrand=SAMSUNG, tvCost=150.0, tvDimension=40, tvScreen=Led],
Telivision [tvBrand=Sony, tvCost=200.0, tvDimension=36, tvScreen=Led]]
but the output observed is as shown below
[Telivision [tvBrand=Sony, tvCost=200.0, tvDimension=36, tvScreen=Led],
Telivision [tvBrand=Sony, tvCost=200.0, tvDimension=36, tvScreen=Led]]
kindly let me know what mistake i am doing in this code
telivision = new Telivision();again in your test code before theSonybrand.Telivision samsung = new Telivision();for Samsung andTelivision sony = new Telivision();for Sony