0

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

4
  • 2
    You are not creating a second Telivision. You keep using the first (and only) instance. Commented Aug 5, 2019 at 18:47
  • Yeah as f1sh said you are only making 1 television. Say you have 1 TV in real life and modify it to be different, you still have only 1 TV. Call telivision = new Telivision(); again in your test code before the Sony brand. Commented Aug 5, 2019 at 18:50
  • So in the above code whenever i need to update a new value , i need to create a new object and then update it. Commented Aug 5, 2019 at 18:50
  • instanciate 2 Telivisions. Telivision samsung = new Telivision(); for Samsung and Telivision sony = new Telivision(); for Sony Commented Aug 5, 2019 at 18:53

2 Answers 2

0

You're list is not being overwritten. You are are adding the same instance of the class twice instead of creating a new one with the new attributes. Because the original istance changed you get the same attributes for both entries in the list. Create a new instance of the object for each Television you add to the list.

   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 secondTelivision = new Telivision();
    secondTelivision.setTvBrand("Sony");
    secondTelivision.setTvDimension(36);
    secondTelivision.setTvScreen("Led");
    secondTelivision.setTvCost("Sony");
    telList.add(secondTelivision);
    System.out.println(telList);


}
Sign up to request clarification or add additional context in comments.

Comments

0

You forgot to declare a second Telivision to separate the two.

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 television2 = new Television();
    telivision2.setTvBrand("Sony");
    telivision2.setTvDimension(36);
    telivision2.setTvScreen("Led");
    telivision2.setTvCost("Sony");
    telList.add(telivision2);
    System.out.println(telList);
    System.out.println(telivision2.getTvBrand()+"Cost is "+telivision2.getTvCost());
    //telList.addAll(telList);

    //System.out.println(telList);

}
}

Also “Telivision” is actually spelled Television

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.