0

I set an object equal to the other that invokes the copy constructor. My problem is when I test if the two are equal to another my output says that they are not. One method is through an overloaded constructor and the other is from a copy constructor that both output the same thing.

public class HotDogClass {

    private int standNumber; // ID number for hotdog stand
    private  int soldToday; //dogs each stand sold
    private static int totalSales;

    public void setstandNumber (int newstandNumber ) { standNumber =      newstandNumber; }
    public int getstandNumber ( ) { return standNumber; }
    public int getsoldToday() { return soldToday; }
    public void setsoldToday ( int st ) { soldToday = st;
        if (soldToday <= 0) 
        System.out.println("no hotdogs sold");}
    public static int gettotalSales ( ) { return totalSales; }


    static  {
        totalSales=0;
        System.out.println("Static initializer block\n" );
    }
    public HotDogClass () { 
        this ( 0, 0);
        System.out.println("Default constructor");
    }

    public HotDogClass (int sta, int st) {
        standNumber = sta;
        soldToday = st;
        totalSales += soldToday;
        System.out.println("overloaded contructor");
    }
    public HotDogClass (HotDogClass original) {
        this.standNumber = original.standNumber;
        this.soldToday = original.soldToday;
        //totalSales += soldToday;
        System.out.println("Copy contructor");
    }
    public void justSold ( ) { 
        soldToday++;
        totalSales++;
    }

    public String toString ( ){ 
        String x = "Stand number " + standNumber  + " sold " + soldToday + " hotdogs" ;
        return x;
    }


    public void dispose ( ) {
        System.out.println("HotDogStand - dispose method");
    }   
    public void finalize ( ) {
        System.out.println("HotDogStand - finalize method");
    }
}

public class HotDogStand {

    public static void main(String[] args) {

        HotDogClass stand1 = new HotDogClass(21, 10);
        System.out.println (stand1 + " \n "  );
        //stand1.justSold();
        //stand1.justSold();
        HotDogClass standCopy = new HotDogClass (stand1);
        System.out.println( standCopy + "\n");
        if (standCopy.equals(stand1))
            System.out.println("Stand1 and Standcopy are equal\n");
        else
            System.out.println("Stand1 and standcopy are not equal\n");

        HotDogClass stand2 = new HotDogClass(32, 7);
        System.out.println (stand2 + " \n ");
        stand2.justSold();
        stand2.justSold();
        stand2.justSold();

        HotDogClass stand3 = new HotDogClass();
        System.out.println(stand3 + " \n ");


        HotDogClass stand4 = new HotDogClass();
        stand4.setsoldToday(0);
        stand4.setstandNumber(10);
        System.out.println(stand4 + " - Testing my set and get methods\n");

        System.out.println("The ending total of all hotdogs sold is " 
                + HotDogClass.gettotalSales());

    }
}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    You would do well to learn basic Java coding standards, such as naming (`getSoldToday()`, not `getsoldToday()`), using `@Override`, and not using static blocks when a basic initializer assignment would work `private static int totalSales = 0`. Most fundamentally, though, you never override `equals`. – chrylis -cautiouslyoptimistic- Apr 08 '17 at 18:34
  • You are not overriding equals in the first place. Please study the duplicated question/answer to learn how to do that properly! – GhostCat Apr 08 '17 at 18:43
  • Don't put `println` calls in constructors. – Lew Bloch Apr 08 '17 at 23:33

0 Answers0