java - Placing average cost of something into a table -
i've been trying add average cost of petshop 6th slot of table , can't work. no errors appear , i'm totally lost on how so. below method calculates average cost of pets in shop.
public string getcalculatepetshopaverage(string shop) { arraylist<pets> petshoppets = new arraylist<pets>(); arraylist<pets> petslist = new arraylist<pets>(); petslist.addall(dogs); petslist.addall(fishes); for(pets pet : petslist) { if(pet != null && pet.getshop().equals(shop)) { petshoppets.add(pet); } } double totalaverage = 0; int = 0; for(pets pet : petshoppets) { i++; totalaverage = totalaverage + pet.getprice(); } system.out.println("there "+i+" pets in here"); this.averagecost = totalaverage; system.out.println(""+this.averagecost); return string.format("%.2f",totalaverage/i); }
below part of code table i'm displaying in gui. i've tried calling method name of petshop whenever run gui application, in 6th section of table can see nan.
@override public object getvalueat(int rowindex, int columnindex) { pets pets = petshops.get(rowindex); switch (columnindex) { case 0: return pets.getshop(); case 1: return pets.gettype(); case 2: return pets.getprice(); case 3: return pets.getdateacquired(); case 4: return pets.getnotes(); case 5: return pets.getcalculatepetshopaverage(pets.getshop()); } return null; }
i'm still quite new java , have no idea how add average cost table, i'd appreciate help.
Comments
Post a Comment