-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommodity.java
More file actions
87 lines (77 loc) · 2.44 KB
/
Commodity.java
File metadata and controls
87 lines (77 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package nrftw_trade;
import java.util.Iterator;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* @author sandy
*/
public class Commodity implements Comparable{
public ArrayList theTags;
public String theName;
public int theStockLevel;
public int thePrice;
Commodity(){
}
Commodity(String aName){
theName = aName;
setupTags();
}
Commodity(String aName, int aStock, int aPrice){
theName = aName;
theStockLevel = aStock;
thePrice = aPrice;
setupTags();
}
/**
* Prints the name and grade of the tag relating to this commodity.
*/
private void displayTags(){
for(Iterator<Tag> aTag = this.theTags.iterator(); aTag.hasNext();){
Tag item = aTag.next();
System.out.println("Tag: "+item.theName+" grade: "+item.theGrade);
}
}
/**
* Sets up the array list of tags relating to this commodity
*/
private void setupTags(){
ArrayList someTags = new ArrayList();
if(theName != null){
String tagsQuery = "select tag, grade from commodities_tags where commodity ='"+theName+"'";
ResultSet results = NRFTW_Trade.dBQuery(tagsQuery);
//theTags = someTags;
try{
//for each name returned
while(results.next()){
//get tags strings
String aTagName = results.getString(1);
int aGrade = results.getInt(2);
//create tag object
Tag aNewTag = new Tag(aTagName,aGrade);
//add to tag arraylist
someTags.add(aNewTag);
}
}catch(SQLException ex){
System.out.println(ex);
}
try{
results.close();
}catch(SQLException ex){
System.out.println(ex);
}
}
theTags = someTags;
}
public int compareTo(Object anotherCommodity) throws ClassCastException {
if (!(anotherCommodity instanceof Commodity))
throw new ClassCastException("A Person object expected.");
int anotherCommodityPrice = ((Commodity) anotherCommodity).thePrice;
return this.thePrice - anotherCommodityPrice;
}
}