Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help finishing or making sure this project is correct. Please comment where and what you fixed, so I can understand my mistakes. Here

I need help finishing or making sure this project is correct. Please comment where and what you fixed, so I can understand my mistakes. Here is the project and under it will be my classes. Thank you.

Project 4

Specific Goals: Extend project 3 to include making jobs wait until people with the resources required by the job are available at the port.

Elaboration:

1. Reading Job specifications from a data file and adding the required resources to each Job instance.

2. Resource pools - SeaPort.ArrayList list of persons with particular skills at each port, treated as resource pools, along with supporting assignment to ships and jobs.

3. Job threads - using the resource pools and supporting the concept of blocking until required resources are available before proceeding.

4. The Job threads should be efficient:

1. If the ship is at a dock and all the people with required skills are available, the job should start.

2. Otherwise, the Job should not hold any resources if it cannot progress.

3. Use synchronization to avoid race conditions.

4. Each Job thread should hold any required synchronization locks for a very short period.

5. When a job is over, all the resources used by the job (the people) should be released back to the port.

6. When all the jobs of a ship are done, the ship should depart the dock and if there are any ships in the port que, one of then should should be assigned to the free dock, and that ships jobs can now try to progress.

7. NOTE: If a job can never progress because the port doesn't have enough skills among all the persons at the port, the program should report this and cancel the job.

5. GUI showing:

o Resources in pools - how many people with skill are currently available

o Thread progress, resources acquired, and resources requests still outstanding

Project 4 General Objectives

Project 4 - Concurrency

Resource pools

o Threads competing for multiple resources

Blocking threads

Extending the GUI interface to visualize the resource pools and progress of the various threads.

------- MY CLASSES

The class CargoShip

import java.util.Scanner;

class CargoShip extends Ship {

double cargoValue;

double cargoVolume;

double cargoWeight;

public CargoShip(Scanner scan) {

super(scan);

if(scan.hasNextDouble()) {

cargoWeight = scan.nextDouble();

}

if(scan.hasNextDouble()) {

cargoVolume = scan.nextDouble();

}

if(scan.hasNextDouble()) {

cargoValue = scan.nextDouble();

}

} // end Scanner Constructor

public String toString() {

String string = "Cargo ship: " + super.toString();

if(jobs.size() == 0) {

return string;

}

for(Job temp: jobs) {

string += " - " + temp;

}

return string;

} // end method toString

public double getCargoValue() {

return cargoValue;

}

public void setCargoValue(double cargoValue) {

this.cargoValue = cargoValue;

}

public double getCargoVolume() {

return cargoVolume;

}

public void setCargoVolume(double cargoVolume) {

this.cargoVolume = cargoVolume;

}

public double getCargoWeight() {

return cargoWeight;

}

public void setCargoWeight(double cargoWeight) {

this.cargoWeight = cargoWeight;

}

} // end class CargoShip

The class Dock

import java.util.Scanner;

class Dock extends Thing {

Ship ship;

public Dock(Scanner scan) {

super(scan);

}

public String toString() {

String string = "Dock: " + super.toString();

if(ship == null) {

return string;

}

string += " " + ship;

return string;

} // end method toString

public Ship getShip() {

return ship;

}

public void setShip(Ship ship) {

this.ship = ship;

}

} // end class Dock

The class Job

import java.util.ArrayList;

import java.util.Scanner;

class Job extends Thing implements Runnable {

double duration;

ArrayList requirements = new ArrayList (); // skills of the persons

private ArrayList skills;

public Job(Scanner scan) {

super(scan);

if(scan.hasNextDouble()) {

duration = scan.nextDouble();

}

while(scan.hasNext()) {

String string = scan.next();

for(String temp: requirements) {

if(temp.equals(string)) {

string = null;

}

}

if(string != null) {

requirements.add(string);

}

skills = new ArrayList();

skills.add("Test");

}

} // end Scanner constructor

public String toString() {

String string = "Job: " + super.toString() + " " + requirements;

return string;

} // end method toString

@Override

public void run() {

}

public double getDuration() {

return duration;

}

public void setDuration(double duration) {

this.duration = duration;

}

public ArrayList getRequirements() {

return requirements;

}

public void setRequirements(ArrayList requirements) {

this.requirements = requirements;

}

public ArrayList getSkills() {

return skills;

}

public void setSkills(ArrayList skills) {

this.skills = skills;

}

} // end class Job

The class PassengerShip

import java.util.Scanner;

class PassengerShip extends Ship {

int numberOfOccupiedRooms;

int numberOfPassengers;

int numberOfRooms;

public PassengerShip(Scanner scan) {

super(scan);

if(scan.hasNextInt()) {

numberOfPassengers = scan.nextInt();

}

if(scan.hasNextInt()) {

numberOfRooms = scan.nextInt();

}

if(scan.hasNextInt()) {

numberOfOccupiedRooms = scan.nextInt();

}

} // end Scanner Constructor

public String toString() {

String string = "Passenger ship: " + super.toString();

if(jobs.size() == 0) {

return string;

}

for(Job temp: jobs) {

string += " - " + temp;

}

return string;

} // end method toString

public int getNumberOfOccupiedRooms() {

return numberOfOccupiedRooms;

}

public void setNumberOfOccupiedRooms(int numberOfOccupiedRooms) {

this.numberOfOccupiedRooms = numberOfOccupiedRooms;

}

public int getNumberOfPassengers() {

return numberOfPassengers;

}

public void setNumberOfPassengers(int numberOfPassengers) {

this.numberOfPassengers = numberOfPassengers;

}

public int getNumberOfRooms() {

return numberOfRooms;

}

public void setNumberOfRooms(int numberOfRooms) {

this.numberOfRooms = numberOfRooms;

}

} // end class PassengerShip

The class Person

import java.util.Scanner;

class Person extends Thing {

String skill;

public Person(Scanner scan) {

super(scan);

if(scan.hasNext()) {

skill = scan.next();

}

} // end Scanner constructor

public String toString() {

String string = "Person: " + super.toString() + " " + skill;

return string;

} // end method toString

public String getSkill() {

return skill;

}

public void setSkill(String skill) {

this.skill = skill;

}

} // end class Person

The class PortTime

public class PortTime {

int time;

public PortTime(int time) {

this.time = time;

}

public int getTime() {

return time;

}

public void setTime(int time) {

this.time = time;

}

} // end class PortTime

The class SeaPort

import java.util.ArrayList;

import java.util.Scanner;

import javax.swing.JTextArea;

class SeaPort extends Thing {

ArrayList docks = new ArrayList (); // the list of docks at the port

ArrayList queue = new ArrayList (); // the list of ships waiting to dock

ArrayList ships = new ArrayList (); // the list of all the ships at this port

ArrayList person = new ArrayList (); // people with skills at this port

public SeaPort(Scanner scan) {

super(scan);

}

public String toString() {

String string = " SeaPort: " + super.toString() + ' ';

for(Dock temp: docks) {

string += " " + temp;

}

string += " --- List of all ships in queue:";

for(Ship temp: queue) {

string += " > " + temp;

}

string += " --- List of all ships:";

for(Ship temp: ships) {

string += " > " + temp;

}

string += " --- List of all person:";

for(Person temp: person) {

string += " > " + temp;

}

return string;

} // end method toString

public ArrayList getDocks() {

return docks;

}

public void setDocks(ArrayList docks) {

this.docks = docks;

}

public ArrayList getQueue() {

return queue;

}

public void setQueue(ArrayList queue) {

this.queue = queue;

}

public ArrayList getShips() {

return ships;

}

public void setShips(ArrayList ships) {

this.ships = ships;

}

public ArrayList getPerson() {

return person;

}

public void setPerson(ArrayList person) {

this.person = person;

}

public void beginJobs(JTextArea jText) {

if(ships.isEmpty()) {

Ship tempShip = queue.remove(0);

ships.add(tempShip);

}

Ship currentShip = ships.get(0);

currentShip.work(jText);

}

}// end class SeaPort

The class SeaportProgram

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.util.HashMap;

import java.util.Scanner;

import javax.swing.AbstractButton;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class SeaPortsProgram extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

World world; // Initiates and runs on all created objects

HashMap hashMapShip = new HashMap();

HashMap hashMapDock = new HashMap();

HashMap hashMapSeaPort = new HashMap();

private JButton sortButton, openButton, readButton, searchButton;

private JPanel jPanel;

private JFileChooser fileChooser;

String[] sortingStrings = { "weight", "length", "width", "draft" };;

JTextArea jText = new JTextArea();

JComboBox jCombo;

JTextField jTextF;

Scanner scan;

File selectedFile;

public SeaPortsProgram() {

setTitle("Sea Ports");

setSize(1000, 1000);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

JScrollPane jScroll = new JScrollPane(jText);

add(jScroll, BorderLayout.CENTER);

sortButton = new JButton("Sort");

fileChooser = new JFileChooser();

openButton = new JButton("Open a File...");

openButton.addActionListener(this);

readButton = new JButton("Read");

readButton.addActionListener(this);

searchButton = new JButton("Search");

searchButton.addActionListener(this);

JLabel jLabelST = new JLabel("Search Target");

jTextF = new JTextField(10);

jCombo = new JComboBox();

jCombo.addItem("Index");

jCombo.addItem("Skill");

jCombo.addItem("Name");

final JComboBox jcbsort = new JComboBox(sortingStrings);

jPanel = new JPanel();

jPanel.add(openButton);

jPanel.add(readButton);

jPanel.add(jLabelST);

jPanel.add(jTextF);

jPanel.add(jCombo);

jPanel.add(searchButton);

jPanel.add(jcbsort);

jPanel.add(sortButton);

add(jPanel, BorderLayout.PAGE_START);

validate();

}

public void readFile(File f)

{

try

{

BufferedReader input=new BufferedReader(new FileReader(f));

HashMap hashmap=new HashMap();

int counter = 0;

while(input.ready())

{

String string=input.readLine().trim();

Scanner scan=new Scanner(string);

if(world == null) {

world = new World(scan);

}

if(!string.startsWith("//"))

{

String type="";

if(scan.hasNext())

{

type=scan.next();

}

if(type.equalsIgnoreCase("port"))

{

world.addPort(scan);

}

else if(type.equalsIgnoreCase("dock"))

{

world.addDock(scan);

}

else if(type.equalsIgnoreCase("ship"))

{

Ship s=new Ship(scan);

hashmap.put(counter, s);

counter++;

world.assignShip(s);

}

else if(type.equalsIgnoreCase("pship"))

{

Ship scanner=new PassengerShip(scan);

hashmap.put(counter, scanner);

counter++;

world.assignShip(scanner);

}

else if(type.equalsIgnoreCase("cship"))

{

Ship scan1=new CargoShip(scan);

hashmap.put(counter ,scan1);

counter++;

world.assignShip(scan1);

}

else if(type.equalsIgnoreCase("job"))

{

Job job1 = new Job(scan);

(new Thread(job1)).start();

}

else if(type.equalsIgnoreCase("person"))

{

world.addPerson(scan);

}

}

}

jText.setText(world.toString());

}

catch (Exception e)

{

e.printStackTrace();

System.out.println(e+"-----");

}

}

public void search(String type, String target) {

jText.append(" Search for " + type.toLowerCase() + " " + target + ": ");

jText.append("\t" + world.search(type, target));

} // end method search

public static void main(String [] args) {

SeaPortsProgram seaPort = new SeaPortsProgram();

} // end main

@Override

public void actionPerformed(ActionEvent e) {

//Handle open button action.

if (e.getSource() == openButton) {

int returnVal = fileChooser.showOpenDialog(null);

if (returnVal == JFileChooser.APPROVE_OPTION) {

File file = fileChooser.getSelectedFile();

//This is where a real application would open the file.

} else {

}

}

else if (e.getSource() == readButton) {

readFile(fileChooser.getSelectedFile());

}

else if (e.getSource() == searchButton) {

System.out.println("Searching type " + jCombo.getSelectedItem().toString());

search(jCombo.getSelectedItem().toString(), jTextF.getText());

}

}

} // end class SeaPortsProgram

The class Ship

import java.util.ArrayList;

import java.util.Iterator;

import java.util.Scanner;

import javax.swing.JTextArea;

class Ship extends Thing {

PortTime arrivalTime = new PortTime(index);

PortTime dockTime = new PortTime(index);

double draft, length, weight, width;

ArrayList jobs = new ArrayList ();

boolean busyFlag = false;

public Ship(Scanner scan) {

super(scan);

if(scan.hasNextDouble()) {

weight = scan.nextDouble();

}

if(scan.hasNextDouble()) {

length = scan.nextDouble();

}

if(scan.hasNextDouble()) {

width = scan.nextDouble();

}

if(scan.hasNextDouble()) {

draft = scan.nextDouble();

}

} // end Scanner constructor

public String toString() {

String string = "Ship: " + super.toString();

return string;

} // end method toString

public double getDraft() {

return draft;

}

public void setDraft(double draft) {

this.draft = draft;

}

public double getLength() {

return length;

}

public void setLength(double length) {

this.length = length;

}

public PortTime getDockTime() {

return dockTime;

}

public void setDockTime(PortTime dockTime) {

this.dockTime = dockTime;

}

public double getWeight() {

return weight;

}

public void setWeight(double weight) {

this.weight = weight;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public void work(JTextArea jText) {

for(Iterator iter = jobs.iterator(); iter.hasNext();) {

Job tempJob = iter.next();

try {

jText.append("Processing job.");

Thread.sleep((long) tempJob.getDuration());

jText.append("Job complete.");

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

} // end class Ship

The class Thing

import java.util.Scanner;

class Thing implements Comparable {

int index;

String name;

int parent;

public Thing() {

name = "";

index = 0;

parent = 0;

} // end no parameter constructor

public Thing(Scanner scan) {

if(scan.hasNext()) {

name = scan.next();

}

if(scan.hasNextInt()) {

index = scan.nextInt();

}

if(scan.hasNextInt()) {

parent = scan.nextInt();

}

} // end Scanner constructor

public String toString() {

String string = name + " " + index;

return string;

} // end toString method

@Override

public int compareTo(Thing o) {

return 0;

}

public Object getName() {

return null;

}

public int getIndex() {

return index;

}

public void setIndex(int index) {

this.index = index;

}

public int getParent() {

return parent;

}

public void setParent(int parent) {

this.parent = parent;

}

} // end class Thing

The class world

import java.util.ArrayList;

import java.util.Iterator;

import java.util.Scanner;

import javax.swing.JTextArea;

class World extends Thing {

ArrayList ports = new ArrayList ();

PortTime time = new PortTime(index);

public World(Scanner scan) {

super();

while(scan.hasNextLine()) {

process(scan.nextLine());

}

}

public String toString() {

String string = " ------- The World -------";

if(ports.size() == 0) {

return string;

}

for(SeaPort temp: ports) {

string += temp;

}

return string;

}

public void process(String string) {

Scanner scan = new Scanner(string);

if(!scan.hasNext()) {

return;

}

String scanVal = scan.next();

if(scanVal.equals("port")) {

addPort(scan);

}

else if(scanVal.equals("dock")) {

addDock(scan);

}

else if(scanVal.equals("pship")) {

addPassengerShip(scan);

}

else if(scanVal.equals("cship")) {

addCargoShip(scan);

}

else if(scanVal.equals("person")) {

addPerson(scan);

}

else if(scanVal.equals("job")) {

addJob(scan);

}

else;

} // end process

public void addPort(Scanner scan) {

ports.add(new SeaPort(scan));

}

public void addDock(Scanner scan) {

Dock tempDock = new Dock(scan);

for(SeaPort temp: ports) {

if(temp.index == tempDock.parent) {

temp.docks.add(tempDock);

}

}

}

public void addPassengerShip(Scanner scan) {

PassengerShip tempPShip = new PassengerShip(scan);

assignShip(tempPShip);

} // end method addPassengerShip

public void addCargoShip(Scanner scan) {

CargoShip tempCShip = new CargoShip(scan);

assignShip(tempCShip);

} // end method addCargoShip

public void addPerson(Scanner scan) {

Person tempPerson = new Person(scan);

for(SeaPort temp: ports) {

if(temp.index == tempPerson.parent) {

temp.person.add(tempPerson);

}

}

} // end method addPerson

public void addJob(Scanner scan) {

} // end method addJob

public SeaPort getSeaPortByIndex(int example) {

for(SeaPort temp: ports) {

if(temp.index == example) {

return temp;

}

}

return null;

} // end method getSeaPortByIndex

public Dock getDockByIndex(int example) {

for(SeaPort temp: ports) {

for(Dock temp1: temp.docks) {

if(temp1.index == example) {

return temp1;

}

}

}

return null;

} // end method getDockByIndex

public Ship getShipByIndex(int x) {

for(Iterator iter = ports.iterator(); iter.hasNext();) {

SeaPort tempPort = iter.next();

for(Iterator iter2 = tempPort.getShips().iterator(); iter2.hasNext();) {

Ship tempShip = iter2.next();

if(tempShip.index == x) {

return tempShip;

}

}

}

return null;

} // end method getShipByIndex

public Person getPersonByIndex(int x) {

for(SeaPort temp: ports) {

for(Person temp1: temp.person) {

if(temp1.index == x) {

return temp1;

}

}

}

return null;

} // end method getPersonByIndex

public void assignShip(Ship temp) {

Dock temp1 = getDockByIndex(temp.parent);

if(temp1 == null) {

getSeaPortByIndex(temp.parent).ships.add(temp);

getSeaPortByIndex(temp.parent).queue.add(temp);

}

else{

if(temp1.ship != null) {

temp1.ship = temp;

}

else {

getSeaPortByIndex(temp1.parent).queue.add(temp);

}

getSeaPortByIndex(temp1.parent).ships.add(temp);

}

} // end method assignShip

void assignPerson(Person port)

{

for(SeaPort seaPort1:ports)

{

if(seaPort1.getIndex()==port.getParent())

{

seaPort1.person.add(port);

}

}

} // end assignPerson

void assignDock(Dock dock)

{

for(SeaPort seaPort1:ports)

{

if(seaPort1.getIndex()==dock.getParent())

{

seaPort1.docks.add(dock);

}

}

}//end assignDock

void assignPort(SeaPort port)

{

ports.add(port);

}

public String search(String type, String target) {

String string = "";

if(type.equals("Name")) {

string = searchName(target);

}

else if(type.equals("Index")) {

try {

string += searchIndex(Integer.parseInt(target));

}

catch(NumberFormatException example) {

string += "Not a valid search target for Index";

}

}

if(type.equals("Skill")) {

string = searchType(type);

}

else;

return string;

}

public String searchName(String target) {

for(SeaPort temp: ports) {

if(temp.name.toLowerCase().trim().equals(target.toLowerCase().trim())) {

return temp.toString();

}

for(Dock temp1: temp.docks) {

if(temp1.name.toLowerCase().trim().equals(target.toLowerCase().trim())) {

return temp1.toString();

}

}

for(Ship temp2: temp.ships) {

if(temp2.name.toLowerCase().trim().equals(target.toLowerCase().trim())) {

return temp2.toString();

}

}

for(Person temp3: temp.person) {

if(temp3.name.toLowerCase().trim().equals(target.toLowerCase().trim())) {

return temp3.toString();

}

}

}

return "Target Not Found";

} //end class searchName

public String searchIndex(int target) {

for(SeaPort temp: ports) {

if(temp.index == target) {

return temp.toString();

}

for(Dock temp1: temp.docks) {

if(temp1.index == target) {

return temp1.toString();

}

}

for(Ship temp2: temp.ships) {

if(temp2.index == target) {

return temp2.toString();

}

}

for(Person temp3: temp.person) {

if(temp3.index == target) {

return temp3.toString();

}

}

}

return "Target Not Found";

} //end class searchIndex

public String searchType(String target) {

String string = "";

for(SeaPort temp: ports) {

for(Person temp1: temp.person) {

if(temp1.skill.equals(target)) {

string += temp1.toString() + ' ';

}

}

}

if(string == "") {

return "Target Not Found";

}

return string;

}

} // end class World

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions

Question

define what is meant by the term human resource management

Answered: 1 week ago