Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java - writing a method

Posted on 2013-06-07
7
Medium Priority
?
292 Views
Last Modified: 2013-06-17
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’;
0
Comment
Question by:cookiejar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39231386
You first need to get your containment right, since it looks like you have nesting. It looks like

class Department {
  private List<SectionInfo> sections;
}

Open in new window

0
 
LVL 16

Expert Comment

by:Valeri
ID: 39233969
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").getStatus() > 0) {
             if (departments.get("C").getCount() > departments.get("C").getExpectation()) {
                 System.out.println("C count > expectation");
             } else {
                if (departments.get("C").getCount() > 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; }
}
0
 

Author Comment

by:cookiejar
ID: 39242207
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 39242262
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
0
 

Author Comment

by:cookiejar
ID: 39242502
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?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 39242755
afaics, you need something like this (of course you would NOT have a main method in Department but you can run this code as an app)

Section (maybe better than 'SectionInfo' [?])

public class Section {
    private String name;
    private int count;
    private int expectation;
    private int status;

    public Section() {
    }

    public Section(String name, int count, int expectation, int status) {
        this.name = name;
        this.count = count;
        this.expectation = expectation;
        this.status = status;
    }

    public String getName() {
        return this.name;
    }

    public int getCount() {
        return this.count;
    }

    public int getExpectation() {
        return this.expectation;
    }

    public int getStatus() {
        return this.status;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setExpectation(int expectation) {
        this.expectation = expectation;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String toString() {
        return String.format("%s=%s,%s=%d,%s=%d,%s=%d", "name", name, "count",
            count, "expectation", expectation, "status", status);
    }
}

Open in new window

Department
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

public class Department {
    private List<Section> sections;

    public Department() {
	sections = new ArrayList<Section>();
    }

    public Department(List<Section> sections) {
	this();
        this.sections.addAll(sections);
    }

    public void addSection(Section section) {
	sections.add(section);
    }

    public List<Section> getSections() {
        return this.sections;
    }

    public void setSections(List<Section> sections) {
        this.sections = sections;
    }

    public String toString() {
        return String.format("%s=%s", "sections", sections);
    }

    public static void main(String[] args) {	
	// TESTING ONLY
	Section s = new Section("A", 11, 2, 1);
	Department d = new Department();
	Map<String, Department> sectionNameToDepartment = new HashMap<String, Department>();
	d.addSection(s);
	sectionNameToDepartment.put(s.getName(), d);
	System.out.println(sectionNameToDepartment.get("A"));
    }
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39254892
:)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question