Skip to content

Commit b0f93ab

Browse files
committed
Show all crop model read from Simulation.CDE
1 parent f04d0c6 commit b0f93ab

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

src/DSSATModel/CropModel.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@ public class CropModel extends BaseModel {
1616
public String toString(){
1717
return Description;
1818
}
19+
20+
public int compare(CropModel c2){
21+
int compare = this.ModelCode.compareTo(c2.ModelCode);
22+
23+
if(compare == 0){
24+
compare = this.Code.compareTo(c2.Code);
25+
}
26+
27+
return compare;
28+
}
1929
}

src/DSSATModel/CropModelList.java

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77

88
import java.util.ArrayList;
99
import java.util.Collections;
10-
import java.util.HashMap;
1110
import java.util.List;
1211

1312
/**
1413
*
1514
* @author Jazzy
1615
*/
1716
public class CropModelList {
18-
protected static HashMap cropModels = new HashMap();
17+
protected static List<CropModel> cropModels = new ArrayList<>();
1918

2019
public static void AddNew(CropModel cropModel)
2120
{
22-
cropModels.put(cropModel.ModelCode, cropModel);
21+
cropModels.add(cropModel);
2322
}
2423

2524
public static void Clear(){
@@ -28,55 +27,39 @@ public static void Clear(){
2827

2928
public static CropModel GetAt(String ModelCode)
3029
{
31-
CropModel cropModel = null;
32-
try{
33-
cropModel = (CropModel) cropModels.get(ModelCode);
30+
for(int i = 0;i < cropModels.size();i++)
31+
{
32+
if(cropModels.get(i).ModelCode.equals(ModelCode))
33+
return cropModels.get(i);
3434
}
35-
catch(Exception ex) {}
36-
37-
return cropModel;
35+
return null;
3836
}
3937

4038
public static CropModel GetByCrop(String CropCode)
4139
{
42-
CropModel cropModel = null;
43-
try {
44-
Object[] objects = cropModels.values().toArray();
45-
for (Object object : objects) {
46-
if(((CropModel) object).Code.endsWith(CropCode)) {
47-
cropModel = (CropModel) object;
48-
break;
49-
}
50-
}
51-
} catch (Exception ex) {
40+
for(int i = 0;i < cropModels.size();i++)
41+
{
42+
if(cropModels.get(i).Code.equals(CropCode))
43+
return cropModels.get(i);
5244
}
53-
54-
return cropModel;
45+
return null;
5546
}
5647

5748
public static CropModel GetAt(int n)
5849
{
59-
CropModel cropModel = null;
6050
try{
61-
Object[] object = cropModels.values().toArray();
62-
cropModel = (CropModel) object[n];
51+
return cropModels.get(n);
6352
}
6453
catch(Exception ex) {}
6554

66-
return cropModel;
55+
return null;
6756
}
6857

6958
public static List<CropModel> GetAll()
7059
{
71-
List<CropModel> cropModelList = new ArrayList<>();
72-
Object[] objects = cropModels.values().toArray();
73-
for (Object object : objects) {
74-
cropModelList.add((CropModel) object);
75-
}
76-
77-
Collections.sort(cropModelList, (BaseModel c1, BaseModel c2) -> c1.Code.compareTo(c2.Description));
60+
Collections.sort(cropModels, (CropModel c1, CropModel c2) -> c1.compare(c2));
7861

79-
return cropModelList;
62+
return cropModels;
8063
}
8164

8265
public static int size()

0 commit comments

Comments
 (0)