Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am supposed to create a public StudentDatabase() which should create an empty database without using an java api class or method. There is supposed to be a method: public boolean add(String macid, int exam1, int exam2) which adds a record to the database. If the student whose MacID is macid is already in the database, this method updates his/her grades with exam1 and exam 2 and returns false; otherwise, it creates macid, exam1 and exam 2 as a new record in the database and returns true. I have gotten this far:

    public class StudentDatabase
    {
public StudentDatabase()
{
   Map<String, Set<int>> = new HashMap<String,List<int>>();
}

public boolean add(String macid, int exam1, int exam2)
{
    remove(0);
    return false;
}

public int getExam1(String macid)
{
if (sdb.macid("ID_") = macid);
else
    return -1;
}

public int getExam2(String macid)
{
    return -1;
}

public boolean remove(String macid)
{

            if (StudentDatabase.macid == macid[]) { 
                for (int j = i; j < (size - 1); j++) 
                {
                    ARRAY[j] = ARRAY[j + 1]; //shift elements left
                }

            return true;
        }

    return false;
}

public int query(int exam1Low, int exam1High, int exam2Low, int exam2High)
{
    return 0;
}
}

I dont understand how I can create a database using Map. Any help would be greatly appreciated. Thank you

share|improve this question

2 Answers

Because you know the number of exams, you may want to save an ExamRecord instead of a list of integers

ExamRecord implements Comparable<ExamRecord>, Serializable {
    private int exam1;
    private int exam2;

    public int compareTo(ExamRecord other) { }

    public int hashCode() { }
}

Map<String, ExamRecord>

As for how you can create a database using Map, you need one file called "index" that lists all of the student names (the keys), then you create one file per student that has the same name as the student (so "John Smith" is saved to "John Smith.dat" or "John_Smith.dat" or whatever); this file either contains the student's serialized ExamRecord, or else it contains a list of exam scores (e.g. if the students' ExamRecord is exam1=50, exam2=80 then you would save 50;80, then you would parse these values back into an ExamRecord when reading the database back into memory).

Another option is to just serialize/deserialize the entire database, but if you're doing this for a class project then the professor might think that this was cheating.

share|improve this answer
thank you! I really appreciate your help – Waleed Apr 25 at 1:59

I don't think you are thinking about the map class properly. I think you should change the creation of the map to this.

Map<String,List<int>> index = new HashMap<String,List<int>>();

From there you can just use the hashmap methods to add and remove students. HashMap API found here
This would be the start of your code

public boolean inIndex(String key)
    {
        return index.containsKey(key);
    }
public void index(String val,int res1,int res2)
    {   
            List<Integer> intList = new LinkedList<Integer>();         

            if(inIndex(val)==false)
            {
            index.put(val,intList);                                    
            }
            index.get(val).add(res1);
            index.get(val).add(res2);  
        }

    }
share|improve this answer
thank you! I really appreciate your help – Waleed Apr 25 at 1:58
All good, remember to click answered please :) – Mike Betterton Apr 25 at 2:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.