huong
huong

Reputation: 4564

Java: Count the number of times a value is input

I'm looking for a way to count the number of times a value is input in Java. To be more specific, I have to manage a Module class which has the properties: semester and code. If it is the first module to be added for semester 1, its code should be 101. The second for that semester would be 102 and similarly, the first module entered for semester 2 would be 201 and so on.

I've tried using a static instance variable count, but I cannot somehow manage to make it change based on the value of semester entered.

Any suggestion is appreciated. Thanks in advance!

--EDIT--

Below is the entire code of my class. I'm sorry if it's too lengthy.

    package courseman;

/**
 * @overview   <code>Module</code> represents the module object of a CourseMan program
 * @author
 *
 */
public class Module {
    private static int count = 1;
    private String name;
    private int semester;
    private int credits;
    private String code;

/**
 * @effects   Initializes a <code>Module</code> object
 * 
 */
public Module(){
    name = "No name";
    semester = 1;
    credits = 0;
    code = "M000";
}

/**
 * @param newModule   a <code>Module</code> object
 * @effects           Creates a <code>Module</code> object with <code>newModule</code>
 * 
 */
public Module (Module newModule){
    if (newModule == null){
        System.out.println("Fatal error creating module.");
        System.exit(0);
    }
    name = newModule.name;
    semester = newModule.semester;
    credits = newModule.credits;
    code = newModule.code;
}

/**
 * 
 * @param newName       a <code>String</code> to be set as a module's name
 * @param newSemester   an <code>integer</code> to be set as a module's semester
 * @param newCredits    an <code>integer</code> to be set as a module's credits
 * @effects             Creates a new <code>Module</code> object with predefined name, semester and credits 
 */
public Module(String newName, int newSemester, int newCredits){
    if(newName == null || newSemester <= 0 || newCredits <= 0){
        System.out.println("Fatal error creating module.");
        System.exit(0);
    }
    name = newName;
    semester = newSemester;
    credits = newCredits;

    int no = semester*100 + count;
    code = "M" + no;
    count++;
}

/**
 * 
 * @param newName   a <code>String</code> to be set as a module's name
 * @modifies        <code>this.name</code>
 * @effects         Takes <code>newName</code> to update a module's name
 * 
 */
public void setName(String newName){
    if(newName == null){
        System.out.println("Fatal error setting module's name.");
        System.exit(0);
    }
    name = newName;
}

/**
 * 
 * @param newSemester   an <code>int</code> to be set as a module's semester
 * @modifies            <code>this.semester</code>
 * @effects             Takes <code>newSemester</code> to update a module's semester
 * 
 */
public void setSemester(int newSemester){
    if (newSemester <= 0){
        System.out.println("Fatal error setting module's semester.");
        System.exit(0);
    }
    semester = newSemester;
    int no = (int) Integer.parseInt(code.substring(1));
    if (((no - count - 1)/100) < semester){
        no = semester*100 + (count - 1);
        code = "M" + no;
    }
}

/**
 * 
 * @param newCredits    an <code>integer</code> to be set as a module's number of credits
 * @modifies            <code>this.credits</code>
 * @effects             Takes <code>newCredits</code> to update a module's number of credits
 * 
 */
public void setCredits (int newCredits){
    if (newCredits <= 0){
        System.out.println("Fatal error setting module's credits.");
        System.exit(0);
    }
    credits = newCredits;
}

/**
 * 
 * @effects   Returns a module's code
 */
public String getCode(){
    return code;
}

/**
 * @effects   Returns a module's name
 * 
 */
public String getName(){
    return name;
}

/**
 * @effects   Returns a module's semester
 * 
 */
public int getSemester(){
    return semester;
}

/**
 * @effects   Returns a module's number of credits
 * 
 */
public int getCredits(){
    return credits;
}

/**
 * @effects   Returns <code>String</code> representation of a <code>Module</code> object
 */
public String toString(){
    return "Module: " + code + " - " + name + " - semester " + semester + " - " + credits + " credits";
}

/**
 * 
 * @param otherModule   a <code>Module</code> object to compare
 * @effects             Returns <code>true</code> if <code>this</code> points to the same
 *                      object as <code>otherModule</code>, otherwise returns <code>false</code>
 */
public boolean equals(Module otherModule){
    return (code.equals(otherModule.code));
}

}

Upvotes: 0

Views: 3014

Answers (2)

twain249
twain249

Reputation: 5706

Edit:

I thought the semester was it's own class. If you want to have a separate count for each semester you need to create a list of counts you can do this with a Map that has the Semester as a key and its count as its value.

Upvotes: 0

Irfy
Irfy

Reputation: 9587

You should manage a separate counter for semesters! Try using a static HashMap which maps the semester to the number of courses in that semester. When adding a new Module, you need to increment the appripriate count (or initialize it to 1 if absent).

Here's a skeleton:

class Module {
    private static HashMap<Integer, Integer> courseCounts = new HashMap<Integer, Integer>();

    public Module(int semester, String name) {
        this.semester = semester;
        this.name = name;
        Integer count = courseCounts.get(semester);
        if (count == null)
            count = 0;
        ++count;
        this.code = "M" + (semester * 100 + count);
        courseCounts.put(semester, count);
    }
}

Upvotes: 1

Related Questions