import java.util.*;
public class Test {
public static void main(String[] args) {
List db = new ArrayList();
ShopDatabase sb = new ShopDatabase(db);
sb.addEntry(new ProductEntry("film", 30, 400));
sb.addEntry(new CashierEntry("Kate", "Smith", 23, 600));
sb.addEntry(new ManagerEntry("Jack", "Simpson", 1, 700));
sb.addEntry(new ProductEntry("soap", 18, 3));
sb.addEntry(new ManagerEntry("Helen", "Jones", 60, 650));
sb.addEntry(new CashierEntry("Jane", "Tomson", 15, 900));
sb.addEntry(new ProductEntry("shampoo", 25, 5));
sb.addEntry(new CashierEntry("Bill", "Black", 59, 300));
Collections.sort(db);
Iterator itr = db.iterator();
while (itr.hasNext()) {
Entry element = (Entry) itr.next();
System.out.println(element + "\n");
}
}
}
public abstract class Entry implements Comparable {
public String name;
public int id;
public Entry(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return this.name;
}
public int getId() {
return this.id;
}
public int compareTo(Object entry) {
Entry temp = (Entry) entry;
int difference = this.id - temp.id;
return difference;
}
public String toString() {
return this.id + " " + this.name;
}
}
public class People extends Entry {
private String name;
private String familyName;
private int id;
private int salary;
public People(String name, String familyName, int id, int salary) {
super(name, id);
this.familyName = familyName;
this.salary = salary;
}
/*
* public String toString() { return this.id + " " + this.name + " " +
* this.familyName + " " + this.salary; } /*public int compareTo(Object
* entry) { Entry temp = (Entry) entry; int difference = this.id - temp.id;
* return difference;
*
* }
*/
}
public class ProductEntry extends Entry {
public String name;
public int id;
public int stock;
public ProductEntry(String name, int id, int stock) {
super(name, id);
this.stock = stock;
}
/*
* public String toString() { return this.id + " " + this.name + " " +
* this.stock; } /*public int compareTo(Object entry) { Entry temp = (Entry)
* entry; int difference = this.id - temp.id; return difference;
*
* }
*/
}
public class ManagerEntry extends People {
public String name;
public String familyName;
public int id;
public int salary;
public ManagerEntry(String name, String familyName, int id, int salary) {
super(name, familyName, id, salary);
}
}
public class CashierEntry extends People {
public String name;
public String familyName;
public int id;
public int salary;
public CashierEntry(String name, String familyName, int id, int salary) {
super(name, familyName, id, salary);
}
/*
* public String toString() { return this.id + " " + this.name + " " +
* this.familyName + " " + this.salary; }
*/
}
Can anyone please help when I have code like this the output is
1 Jack
15 Jane
18 soap
23 Kate
25 shampoo
30 film
59 Bill
60 Helen
but if I uncommenting toString in People and ProductEntry the output is
0 null Simpson 700
0 null Tomson 900
0 null 3
0 null Smith 600
0 null 5
0 null 400
0 null Black 300
0 null Jones 650
why name and id is becomming null ???? Thank you in advance
The reason is name and id are null.
When you retrieve
this.id, it returns the value of this classes (ProductEntry) member variable rather than its super class (which is what you want in this case). If these member variables weren’t defined in ProductEntry, a call to this.id would retrieve the id from the super class, in this case Entry. This is known as Variable shadowingYou can solve the problem in two possible ways. The first is to simply remove name and id member variables from the sub classes of Entry. This will mean
this.idwill access the member variable ofEntryrather than its sub classProductEntry. The other option is to simply assign these in the constructor, but as you already storing these values in the super class, I don’t think this is the best approach.In the spirit of encapsulation you also may want to consider making your member variables private (only accessible by the class there defined in), or protected (accessible by the class its defined in and its sub types).