CS1020 Modules Lab2 Ex2

Link to the question – Modules Lab 2 Ex 2

import java.util.*;

class Schedule {
	// declare the attributes
	private String day;
	private int startTime;
	private int endTime;
	// declare the constructor
	public Schedule(String day, int startTime, int endTime)
	{
		this.day = day;
		this.startTime = startTime;
		this.endTime = endTime;
	}
	/* clashWith: to check whether this schedule clash with a Schedule called otherSchedule
	 * 		PRE-Condition  :
	 * 		POST-Condition :
	 */
	public boolean clashWith(Schedule otherSchedule) {
		// implementation
		return false;
	}
	
	public String getDay()
	{
		return day;
	}
	
	public int getStartTime()
	{
		return startTime;
	}

	public int getEndTime()
	{
		return endTime;
	}

	public boolean equals(Object obj)
	{
		Schedule sch = (Schedule) obj;
		if(this.day.equals(sch.getDay()))
		{
			if((sch.getStartTime() < this.endTime) && (sch.getEndTime() > this.startTime))
			{
				return true;
			}
		}
		return false;
	}
}

class Module {
	// declare the attributes
	private String code;
	private Schedule lectureSchedule;
	private Schedule tutorialSchedule;
	private Schedule labSchedule;
	// declare the constructor
	public Module(String code, Schedule lectureSchedule, Schedule tutorialSchedule, Schedule labSchedule)
	{
		this.code = code;
		this.lectureSchedule = lectureSchedule;
		this.tutorialSchedule = tutorialSchedule;
		this.labSchedule = labSchedule;
	}
	/* count: to count number of classes(lecture, tutorial, and lab of only this Module) on day.
	 * 	      For example: when day = "Monday", lecture is on Monday, tutorial is on Monday
	 *        but lab is on Tuesday, then return 2. (lecture and tutorial are on Monday).
	 * 		PRE-Condition  :
	 * 		POST-Condition :
	 */
	public int count(String day) {
		// implementation
		return 0;
	}

	/* clashWith: to check whether this module clash with a Module called otherModule
	 * 		PRE-Condition  :
	 * 		POST-Condition :
	 */
	public boolean clashWith(Module otherModule) {
		// implementation
		return false;
	}

	public Schedule getLectureSchedule()
	{
		return lectureSchedule;
	}

	public Schedule getTutorialSchedule()
	{
		return tutorialSchedule;
	}

	public Schedule getLabSchedule()
	{
		return labSchedule;
	}
}

class Timetable {
	// declare the attributes
	private List<Module> listOfModules;
	// declare the constructor
	public Timetable()
	{
		listOfModules = new ArrayList<Module>();
	}
	/* checkClash: to check whether otherModule clash with one of 
	 * 			   the modules in our timetable list.
	 * 		PRE-Condition  :
	 * 		POST-Condition :
	 */
	public boolean checkClash(Module otherModule) {
		// implementation
		Schedule otherLecSch = otherModule.getLectureSchedule();
		Schedule otherTutSch = otherModule.getTutorialSchedule();
		Schedule otherLabSch = otherModule.getLabSchedule();
		if(listOfModules.size() == 0)
		{
			return false;
		}
		else
		{
			for(Module m : listOfModules)
			{
				Schedule mLec = m.getLectureSchedule();
				Schedule mTut = m.getTutorialSchedule();
				Schedule mLab = m.getLabSchedule();
				if(mLec.equals(otherLecSch) || mLec.equals(otherTutSch) || mLec.equals(otherLabSch))
				{
					return true;
				}
                                if(mTut.equals(otherLecSch) || mTut.equals(otherTutSch) || mTut.equals(otherLabSch))
				{
                                         return true;
                                }
				if(mLab.equals(otherLecSch) || mLab.equals(otherTutSch) || mLab.equals(otherLabSch))
				{
					return true;
				}
			}
		}
		return false;
	}

	/* add: to add a new module to the timetable list.
	 * 		PRE-Condition  :
	 * 		POST-Condition :
	 */
	public void add(Module module) {
		// implementation
		if(!checkClash(module))
		{
			//check for clashes
			listOfModules.add(module);
			System.out.println("Added");	
		}
		else
		{
			System.out.println("Clashed");
		}
	}

	/* count: to count number of classes on day.
	 * 		PRE-Condition  :
	 * 		POST-Condition :
	 */
	public int count(String day) {
		// implementation
		int count = 0;
		if(listOfModules != null)
		{
			for(Module mod : listOfModules)
			{
				if(mod.getLectureSchedule().getDay().equals(day))
				{
					count++;
				}
				if(mod.getTutorialSchedule().getDay().equals(day))
				{
					count++;
				}
				if(mod.getLabSchedule().getDay().equals(day))
				{
					count++;
				}
			}
		}
		return count;
	}
}

public class Main {

	public static void main(String[] args) {
		// declare the necessary variables
		Timetable timeTable = new Timetable();
		// declare a Scanner object to read input
		Scanner sc = new Scanner(System.in);
		// read input and process them accordingly
		int numOfOperations = sc.nextInt();
		if(numOfOperations >= 1 && numOfOperations <= 30)
		{
			for(int i = 0; i < numOfOperations; i++)
			{
				String opType = sc.next();
				if(opType.equals("MODULE"))
				{
					String code = sc.next();
					String lectSchedule = sc.next();
					int lectStartTime = sc.nextInt();
					int lectEndTime = sc.nextInt();
					Schedule lectSch = new Schedule(lectSchedule, lectStartTime, lectEndTime);
					String tutSchedule = sc.next();
					int tutStartTime = sc.nextInt();
					int tutEndTime = sc.nextInt();
					Schedule tutSch = new Schedule(tutSchedule, tutStartTime, tutEndTime);
					String labSchedule = sc.next();
					int labStartTime = sc.nextInt();
					int labEndTime = sc.nextInt();
					Schedule labSch = new Schedule(labSchedule, labStartTime, labEndTime);
					Module m = new Module(code, lectSch, tutSch, labSch);
					timeTable.add(m);
				}
				else if(opType.equals("COUNT"))
				{
					String day = sc.next();
					System.out.println(timeTable.count(day));
				}
			}
		}
		else
		{
			return;
		}
		// simulate the problem

		// output the result
	}
}
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