cookiejar
asked on
Java - writing a method
I am trying to write a function but I am a beginner in JAVA. I would like to pass into the method a Value Object.
DepartVO
Section Count Expectation Status
A 11 12 1
B 5 5 0
C 21 20 -1
D 7 6 -1
E 18 17 -1
Department B
A 12 12 0
B 22 22 0
C 10 11 1
D 2 2 0
E 12 11 - 1
Write a method whose input parameters is a Value Object
I want to only adjust count values of Section A or C.
If there A status > 0 then
if C count > expectation
add C excess to A count
else
if C status > 0 then
if E count > expectation
add E excess to C count
I only want to process under these conditions
How do I loop through the ValueObject and how do I do a lookup
for the specific section (i.e. Section A or C or E).
Public string AddjustVal(DepartVO)
{
If section = A and status > 0 then
// I need to find section C - how do I do this?
If section C count > expectation then
Add the excess to section count
Else
If section C status > 0 then
// Find section E
If section E count > expectation
Add excess to Section C
}
Return ‘Success’;
DepartVO
Section Count Expectation Status
A 11 12 1
B 5 5 0
C 21 20 -1
D 7 6 -1
E 18 17 -1
Department B
A 12 12 0
B 22 22 0
C 10 11 1
D 2 2 0
E 12 11 - 1
Write a method whose input parameters is a Value Object
I want to only adjust count values of Section A or C.
If there A status > 0 then
if C count > expectation
add C excess to A count
else
if C status > 0 then
if E count > expectation
add E excess to C count
I only want to process under these conditions
How do I loop through the ValueObject and how do I do a lookup
for the specific section (i.e. Section A or C or E).
Public string AddjustVal(DepartVO)
{
If section = A and status > 0 then
// I need to find section C - how do I do this?
If section C count > expectation then
Add the excess to section count
Else
If section C status > 0 then
// Find section E
If section E count > expectation
Add excess to Section C
}
Return ‘Success’;
check this:
import java.util.Map;
import java.util.HashMap;
public class Department {
private String section;
private int count;
private int expectation;
private int status;
// create constructor in this way
public Department(String section, int count, int expectation, int status) {
this.section = section;
this.count = count;
this.expectation = expectation;
this.status = status;
}
public static void main(String[] args) {
Map<String, Department> departments = new HashMap<String, Department>();
departments.put("A", new Department("A", 11, 12, 1));
departments.put("B", new Department("B", 5, 5, 0));
departments.put("C", new Department("C", 21, 20, -1));
departments.put("D", new Department("D", 7, 6, -1));
departments.put("E", new Department("E", 18, 17, -1));
// departments.get("A") returns department A, departments.get("C") returns department "C"
// take a look at Map implementations
if (departments.get("A").getS tatus() > 0) {
if (departments.get("C").getC ount() > departments.get("C").getEx pectation( )) {
System.out.println("C count > expectation");
} else {
if (departments.get("C").getC ount() > 0) {
System.out.println("C count > 0");
}
}
}
} // end main method
public String getSection() { return section; }
public int getCount() { return count; }
public int getExpectation() { return expectation; }
public int getStatus() { return status; }
}
import java.util.Map;
import java.util.HashMap;
public class Department {
private String section;
private int count;
private int expectation;
private int status;
// create constructor in this way
public Department(String section, int count, int expectation, int status) {
this.section = section;
this.count = count;
this.expectation = expectation;
this.status = status;
}
public static void main(String[] args) {
Map<String, Department> departments = new HashMap<String, Department>();
departments.put("A", new Department("A", 11, 12, 1));
departments.put("B", new Department("B", 5, 5, 0));
departments.put("C", new Department("C", 21, 20, -1));
departments.put("D", new Department("D", 7, 6, -1));
departments.put("E", new Department("E", 18, 17, -1));
// departments.get("A") returns department A, departments.get("C") returns department "C"
// take a look at Map implementations
if (departments.get("A").getS
if (departments.get("C").getC
System.out.println("C count > expectation");
} else {
if (departments.get("C").getC
System.out.println("C count > 0");
}
}
}
} // end main method
public String getSection() { return section; }
public int getCount() { return count; }
public int getExpectation() { return expectation; }
public int getStatus() { return status; }
}
ASKER
Map<String, Department> departments = new HashMap<String, Department>();
The VO is dynamic not set to only A thru E .
So how would the put statement be constructed for the dynamic DepartmentVO?
The VO is dynamic not set to only A thru E .
So how would the put statement be constructed for the dynamic DepartmentVO?
The VO is dynamic not set to only A thru E .How is that a problem?
In point of fact, 'A thru E' is not an attribute of Department, it's an attribute of 'Section' (or what i called above 'SectionInfo')
What IS an attribute of Department is a List<SectionInfo>, as i said above
ASKER
As I've indicated, I am a java beginner, My question is what is the correct syntax for the Map and Put referencing the SectionInfo List to get specific departments? For example to get A C E?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:)
Open in new window