Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Shiggiddie/6122672 to your computer and use it in GitHub Desktop.
Save Shiggiddie/6122672 to your computer and use it in GitHub Desktop.
Classes written for Udacity Lesson 8, Optional Challenge.
//Manager Class
import java.util.ArrayList;
public class Manager
{
private ArrayList<Photographer> photographers;
private ArrayList<Assignment> assignments;
private Portfolio portfolio;
public Manager() {
photographers = new ArrayList<Photographer>();
assignments = new ArrayList<Assignment>();
portfolio = new Portfolio();
}
/**
* Called when it's time to hire a new photographer.
*
* @param photographer the name of the photographer.
*/
public void hire(String photographer)
{
photographers.add(new Photographer(photographer));
}
/**
* Called when it's time for the daily meeting where
* the highest priority assignments are given to photographers.
* Each photographer can take one assignment. The highest priority
* assignment should be given out first and the highest priority
* assignment should go to the photographer who was hired first.
*/
public void giveOutAssignments()
{
for (Photographer photographer : photographers)
{
if (assignments.size() > 0)
{
String pic = photographer.takePic(assignments.remove(0).getDescription());
portfolio.addPicture(pic,photographer.getName());
}
}
}
/**
* A Customer came up with a new assignment. The manager should
* add it to the to-do list so that next time it's time to give
* out assignments, the new assignment will be a possibility.
*
* The new Assignment is added to the assignments ArrayList in descending priority.
* @param priority the new Assignment's priority
* @param description the new Assignment's description
*/
public void newAssignment(int priority, String description)
{
int index = 0;
int i = 0;
boolean found = false;
while (!found && i < assignments.size())
{
if (priority > assignments.get(i).getPriority())
{
index = i;
found = true;
}
else
{
i++;
}
}
if (found)
{
assignments.add(index, new Assignment(priority, description));
}
else
{
assignments.add(new Assignment(priority, description));
}
}
/**
* It's the end of the story for now. Time to look at all the
* work the company has done.
*/
public void checkPortfolio()
{
portfolio.draw();
}
}
//Photographer Class
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.File;
import java.io.FileNotFoundException;
public class Photographer
{
private Map<String, String> photos;
private String name;
/**
* Constructor for Photographer Class
*
* @param name the name of the photographer
*/
public Photographer(String name)
{
photos = new HashMap(); // An important line. Must go in the constructor.
readPhotos(); // A very important line. this must go in the Photographer
// constructor so that the photographer will be able to take Pictures.
this.name = name;
}
/**
* Get the name of the Photographer
* @return name the name of the Photographer
*/
public String getName()
{
return name;
}
public String takePic(String description)
{
return this.takePicture(description);
}
/**
* A helper method you will need to use.
*
* @param description the description of the desired photo.
* @return the name of a file with a photo matching the description.
*/
private String takePicture(String description)
{
return photos.get(description);
}
/**
* Don't look too hard at this method. this is how the photographer will
* be able to return a photo file name given a description. There is a
* secret index in photos.txt that is read by this method. Ideally, this
* method would involve a photographer going out and taking a picture and
* putting it in the system. That would take a lot of time though, so instead
* the photos are already included.
*/
private void readPhotos()
{
Pattern commentPattern = Pattern.compile("^//.*");
Pattern photoPattern = Pattern.compile("([a-zA-Z0-9\\.]+) (.*)");
try
{
Scanner in = new Scanner(new File("photos.txt"));
while (in.hasNextLine())
{
String line = in.nextLine();
Matcher commentMatcher = commentPattern.matcher(line);
Matcher photoMatcher = photoPattern.matcher(line);
if (commentMatcher.find())
{
// This line of the file is a comment. Ignore it.
}
else if (photoMatcher.find())
{
String fileName = photoMatcher.group(1);
String description = photoMatcher.group(2);
photos.put(description, fileName);
}
}
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
}
}
//The Assignment Class
public class Assignment
{
private int priority;
private String description;
/**
* Constructor for Assignment Class
*
* @param priority integer value representing the priority of the Assignment
* @param description String value containing the description of the Assignment
*/
public Assignment(int priority, String description)
{
this.priority = priority;
this.description = description;
}
/**
* Returns priority of the Assignment
* @return priority the priority of the Assignment
*/
public int getPriority()
{
return priority;
}
/**
* Returns description of the Assignment
* @return description the description of the Assignment
*/
public String getDescription()
{
return description;
}
}
//The Portfolio Class
import java.util.ArrayList;
public class Portfolio
{
private int curX;
private ArrayList<FinishedPhoto> finishedPhotos;
public Portfolio()
{
curX = 0;
finishedPhotos = new ArrayList<FinishedPhoto>();
}
/**
* Creates and adds a picture to the finishedPhotos ArrayList
* @param picName the picture's filename
* @param photographerName the name of the photographer that took the picture
*/
public void addPicture(String picName, String photographerName)
{
FinishedPhoto pic = new FinishedPhoto(picName, photographerName, curX);
curX = pic.getMaxX();
finishedPhotos.add(pic);
}
/**
* draws each finished photo in the portfolio
*/
public void draw()
{
for(FinishedPhoto photo : finishedPhotos)
{
photo.draw();
}
}
}
//The FinishedPhto Class
public class FinishedPhoto
{
private Picture pic;
private Text name;
/**
* Constructor for Finished Photo
* @param picName filename of the picture
* @param photographerName name of photographer who took the picture
* @param translateX the offset to X necessary to place the photo properly in the portfolio
*/
public FinishedPhoto(String picName, String photographerName, int translateX)
{
pic = new Picture(picName);
pic.translate(translateX, 0);
name = new Text(translateX, pic.getMaxY(), photographerName);
}
/**
* draws the FinishedPhoto
*/
public void draw()
{
pic.draw();
name.draw();
}
/**
* returns the maximum X of the FinishedPhoto
* @return maxX the maximum value of X
*/
public int getMaxX()
{
return pic.getMaxX();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment