CS1020 Scholars Lab2 Ex3

Link to the question – Scholars Lab2 Ex3

import java.util.*;

class Scholar
{
	private String mName;
	private ArrayList<String>mProjects;
	
	public Scholar(String pName)
	{
		this.mName = pName;
		this.mProjects = new ArrayList<String>();
	}
	
	public String getName()
	{
		return mName;
	}
	
	public void addProject(String pProject)
	{
		mProjects.add(pProject);
	}
	
	public ArrayList<String> getProjects()
	{
		return mProjects;
	}
}

class Field
{
	private String mName;
	private ArrayList<String>mProjects;
	
	public Field(String pName)
	{
		this.mName = pName;
		this.mProjects = new ArrayList<String>();
	}
	
	public void addProject(String pProject)
	{
		mProjects.add(pProject);
	}
	
	public ArrayList<String> getProjects()
	{
		return mProjects;
	}
}


class Scholars 
{
	static ArrayList<Scholar> schList = new ArrayList<Scholar>();
	static ArrayList<Field> fieldList = new ArrayList<Field>();
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int numScholars = sc.nextInt();
		for(int i = 0; i < numScholars; i++)
		{
			String nameSch = sc.next();
			Scholar sch = new Scholar(nameSch);
			int numProj = sc.nextInt();
			for(int j = 0; j < numProj; j++)
			{
				String projName = sc.next();
				sch.addProject(projName);
			}
			schList.add(sch);
		}
		int numField = sc.nextInt();
		for(int i = 0; i < numField; i++)
		{
			String nameField = sc.next();
			Field field = new Field(nameField);
			int numProj = sc.nextInt();
			for(int j = 0; j < numProj; j++)
			{
				String projName = sc.next();
				field.addProject(projName);
			}
			fieldList.add(field);
		}
		for(Scholar s : schList)
		{
			int count = 0;
			HashSet t = new HashSet();
			for(String sProj : s.getProjects())
			{
				for(Field field : fieldList)
				{
					for(String fName : field.getProjects())
					{
						if(sProj.equals(fName))
						{
							t.add(field);
							break;
						}
					}
				}
			}
			System.out.println(s.getName() + " " + t.size());
		}
	}
}
Advertisements
This entry was posted in NUS, SoC and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s