リストに顧客オブジェクトを追加するために、このコードを作成しました。ただし、最初と最後のオブジェクトのみを取得しています。何が悪いんだ。
import java.util.*;
class List{
Customer ptr;
public void add(Customer customer){
Customer temp = customer;
if(ptr==null){
ptr = temp;
}else{
ptr.next = temp;
}
}
public int size(){
int size = 0;
Customer temp = ptr;
while(temp!=null){
size++;
temp = temp.next;
}
return size;
}
public void printList(){
Customer temp = ptr;
while(temp!=null){
System.out.println(temp);
temp = temp.next;
}
}
}
class Demo{
public static void main(String args[]){
List list = new List();
Customer c1 = new Customer("1001","Danapala");
Customer c2 = new Customer("1002","Gunapala");
Customer c3 = new Customer("1003","Somapala");
Customer c4 = new Customer("1004","Amarapala");
list.add(c1);
list.add(c2);
list.add(c3);
list.printList();
System.out.println(list.size());
}
}
class Customer{
String id;
String name;
Customer next;
public Customer(String id, String name){
this.id = id;
this.name = name;
}
public String toString(){
return id+" : "+name;
}
public boolean equals(Object ob){
Customer c=(Customer)ob;
return this.id.equals(c.id);
}
}`