Question

Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

Project 1 Introduction the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define .SeaPortProgram extends JFrame o variables used by the GUI interface o world: World Thing implement Comparable <Thing> o index: int o name: String o parent: int World extends Thing o ports: ArrayList <SeaPort> o time: PortTime SeaPort extends Thing o docks: Arrayist<Dock> o que: ArrayList <Ship> //the list of ships waiting to dock o ships: ArrayList <Ship> // a list of all the ships at this port o persons: ArrayList <Person>// people with skills at this port Dock extends Thing o ship: Ship Ship extends Thing o arrivalTime, dockTime: PortTimee o draft, length, weight, width: double o jobs: ArrayList <Job> PassengerShip extends Ship o numberOfOccupiedRooms: int o numberOfPassengers: int o numberOfRooms: int CargoShip extends Ship o cargoValue: double o cargoVolume: double o cargoWeight: double .Person extends Thing o skill: String Job extends Thing optional till Projects 3 and 4 o duration: double o requirements: ArrayList<String> // should be some of the skills of the persons PortTime o time: int Eventually, in Projects 3 and 4, you will be asked to show the progress of the jobs using JProgressBars.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

note: due to limited character i cant able to post all files

TestProgram.java

import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class TestProgram {
    private static final World theWorld = new World(new Scanner("JesseWorld 1 0"));
  
    public static void main(String[] args) {
        JFileChooser chooseDataFile = new JFileChooser(".");
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt");
        chooseDataFile.setFileFilter(filter);
        int returnVal = chooseDataFile.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            DataLoader dl = new DataLoader(chooseDataFile.getSelectedFile().getName()); //loads the text file into ArrayList<String>
            for (String line : dl.getData())
                theWorld.process(line.trim()); //load each line into actual objects
        }
      
        Debug.println(theWorld.toString());
    }
    public static void test() {
        theWorld.process("port TestPort1 10001 1");
        theWorld.process("port TestPort2 10002 1");
        theWorld.process("dock TestDock1 20000 10001 30000");
        theWorld.process("dock TestDock2 20001 10002 30001");
        theWorld.process("dock TestDock3 20002 10002 30000");
        theWorld.process("dock TestDock4 20003 10002 30001");
        theWorld.process("dock TestDock5 20004 10002 30001");
        theWorld.process("pship TestPass1 30000 10001 6000.25 200.5 100.9 50.14"); //ship parent should be port here
        theWorld.process("pship TestPass2 30001 10002 5424.1 310.2 123.9 45.44 12 4 3");
        theWorld.process("pship TestPass3 30002 10001 100.1 200.2 300.9 400.44 1 1 0");
        theWorld.process("cship TestCargo1 40000 10002 321.0 488.23 9.1 999.99 222.2 111.1 10000.00");
        theWorld.assignShip(new PassengerShip(new Scanner("TestPass4 30003 20004"))); //create and add ship directly to dock
        theWorld.assignShip(new CargoShip("TestCargo2 40001 10001 555.0 666.23 77.1 453.78 456.2 8.1 8001.00")); //test string constructor
      
        theWorld.getSeaPortByIndex(10001).processQue();
        theWorld.getSeaPortByIndex(10002).processQue();
      
        //think about how to release ships based on their index
        theWorld.getSeaPortByIndex(10001).releaseShipAtDock(29999); //no dock with this id
        theWorld.getSeaPortByIndex(10001).releaseShipAtDock(20000);
        theWorld.getSeaPortByIndex(10001).processQue();
      
        Debug.println(theWorld.toString());
    }
  
    public static void test2() {
        DataLoader dl = new DataLoader();
        dl.loadFromFile("Y:\\Programming\\Java\\Projects\\Young_Jesse_Project_1\\testRead.txt");
        for (String line : dl.getData())
            theWorld.process(line.trim());
        dl.saveToFile("Y:\\Programming\\Java\\Projects\\Young_Jesse_Project_1\\testWrite.txt");
      
        Debug.println(theWorld.toString());
    }
}

PassengerShip.java

import java.util.Scanner;

public class PassengerShip extends Ship {
    private int numberOfOccupiedRooms;
    private int numberOfPassengers;
    private int numberOfRooms;
  
    public PassengerShip() {
        this.numberOfOccupiedRooms = 0;
        this.numberOfPassengers = 0;
        this.numberOfRooms = 0;
    } // end default constructor
  
    public PassengerShip(Scanner sc) {
        super (sc);
        if (sc.hasNextInt()) this.numberOfPassengers = sc.nextInt();
        if (sc.hasNextInt()) this.numberOfRooms = sc.nextInt();
        if (sc.hasNextInt()) this.numberOfOccupiedRooms = sc.nextInt();
    } // end Scanner constructor
  
    public PassengerShip(String st) {
        this(new Scanner(st));
    }
  
    @Override
    public String toString () {
        String st = "Pass. ship:\t" + super.toString();
        st += "\tNumPassengers: " + String.valueOf(getNumberOfPassengers());
        st += "\tNum Rooms: " + String.valueOf(getNumberOfRooms());
        st += "\t\tNum Occupied: " + String.valueOf(getNumberOfOccupiedRooms());
        if (getJobs().isEmpty())
           return st;
        for (Job mj: getJobs()) st += "\n       - " + mj;
        return st;
    } // 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;
    }
}

CargoShip.java


import java.util.Scanner;

public class CargoShip extends Ship {
    private double cargoValue;
    private double cargoVolume;
    private double cargoWeight;

    public CargoShip() {
        this.cargoValue = 0.0;
        this.cargoVolume = 0.0;
        this.cargoWeight = 0.0;
    } // end default constructor
  
    public CargoShip(Scanner sc) {
        super(sc);
        if (sc.hasNextDouble()) this.cargoWeight = sc.nextDouble();
        if (sc.hasNextDouble()) this.cargoVolume = sc.nextDouble();
        if (sc.hasNextDouble()) this.cargoValue = sc.nextDouble();
    } // end Scanner constructor
  
    public CargoShip(String st) {
        this(new Scanner(st));
    }

    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;
    }
  
    @Override
    public String toString () {
        String st = "Cargo ship:\t" + super.toString();
        st += "\tCargo Weight: " + String.valueOf(getCargoWeight());
        st += "\tCargo Volume: " + String.valueOf(getCargoVolume());
        st += "\tCargo Value: " + String.valueOf(getCargoValue());
        if (getJobs().isEmpty())
           return st;
        for (Job mj: getJobs()) st += "\n       - " + mj;
        return st;
    } // end method toString
}

Ship.java


import java.util.ArrayList;
import java.util.Scanner;
public class Ship extends Thing {
    private PortTime arrivalTime;
    private PortTime dockTime;
    private double draft;
    private double length;
    private double weight;
    private double width;
    private ArrayList<Job> jobs;
  
    public Ship() {
        this.initObjects();
        this.draft = 0.0;
        this.length = 0.0;
        this.weight = 0.0;
        this.width = 0.0;
    } // end default constructor
  
    public Ship(Scanner sc) {
        super (sc);
        this.initObjects();
        if (sc.hasNextDouble()) this.weight = sc.nextDouble();
        if (sc.hasNextDouble()) this.length = sc.nextDouble();
        if (sc.hasNextDouble()) this.width = sc.nextDouble();
        if (sc.hasNextDouble()) this.draft = sc.nextDouble();
    } // end Scanner constructor
  
    public Ship(String st) {
        this(new Scanner(st));
    }
  
    //ensure memory allocation
    private void initObjects() {
        this.arrivalTime = new PortTime();
        this.dockTime = new PortTime();
        this.jobs = new ArrayList<>();
    }
  
    @Override
    public String toString() {
        String st = super.toString();
        st += "\tWeight: " + String.valueOf(getWeight());
        st += "\tLength: " + String.valueOf(getLength());
        st += "\tWidth: " + String.valueOf(getWidth());
        st += "\tDraft: " + String.valueOf(getDraft());
        return st;
    }

    public PortTime getArrivalTime() {
        return arrivalTime;
    }

    public void setArrivalTime(PortTime arrivalTime) {
        this.arrivalTime = arrivalTime;
    }

    public PortTime getDockTime() {
        return dockTime;
    }

    public void setDockTime(PortTime dockTime) {
        this.dockTime = dockTime;
    }

    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 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 ArrayList<Job> getJobs() {
        return jobs;
    }

    public void setJobs(ArrayList<Job> jobs) {
        this.jobs = jobs;
    }
  
    public void addJob(Job job) {
        this.jobs.add(job);
    }
  
    public void removeJob(Job job) {
        this.jobs.remove(job);
    }
  
    public void removeJob(int index) {
        this.jobs.remove(index);
    }
}

Thing.java

import java.util.Scanner;

public class Thing implements Comparable<Thing> {
    private int index;
    private String name;
    private int parent;

    public Thing() {
        this.index = 0;
        this.name = "";
        this.parent = 0;
    } // end default constructor
  
    public Thing(Scanner sc) {
        if (sc.hasNext()) this.name = sc.next();
        if (sc.hasNextInt()) this.index = sc.nextInt();
        if (sc.hasNextInt()) this.parent = sc.nextInt();
    } // end Scanner constructor
  
    public Thing(String st) {
        this(new Scanner(st));
    }
  
    @Override
    public String toString() {
        return "Name: " + this.getName() + ((this.getName().length() < 10) ? "\t\t" : "\t")
             + "Index: " + String.valueOf(this.getIndex()) + "\tParent: " + String.valueOf(this.getParent());
    }
  
    @Override
    public int compareTo(Thing o) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getParent() {
        return parent;
    }

    public void setParent(int parent) {
        this.parent = parent;
    }
}

PortTime.java


import java.util.Scanner;

public class PortTime {
    private int time;
  
    public PortTime() {
        this.time = 0;
    } // end default constructor
  
    public PortTime(Scanner sc) {
        if (sc.hasNextInt()) this.time = sc.nextInt();
    } // end Scanner constructor

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }
  
    @Override
    public String toString() {
        return "PortTime: Time is " + String.valueOf(time);
    }
}

Job.java


import java.util.ArrayList;
import java.util.Scanner;

public class Job extends Thing {
    private double duration;
    private ArrayList<String> requirements; // should be some of the skills of the persons

    public Job() {
        this.initObjects();
        this.duration = 0.0;
    } // end default constructor
  
    public Job(Scanner sc) {
        super(sc);
        this.initObjects();
        if (sc.hasNextDouble()) this.duration = sc.nextDouble();
        while(sc.hasNext()) //scan for array of job requirements
            this.requirements.add(sc.next());
    } // end Scanner constructor

    //ensure memory allocation
    private void initObjects() {
        this.requirements = new ArrayList<>();
    }
  
    public double getDuration() {
        return duration;
    }

    public void setDuration(double duration) {
        this.duration = duration;
    }

    public ArrayList<String> getRequirements() {
        return requirements;
    }

    public void setRequirements(ArrayList<String> requirements) {
        this.requirements = requirements;
    }
    public void addRequirement(String st) {
        this.requirements.add(st);
    }
    public void removeRequirement(String st) {
        this.requirements.remove(st);
    }
    public void removeRequirement(int index) {
        this.requirements.remove(index);
    }
  
    @Override
    public String toString () {
        String st = "Job:\t" + super.toString();
        st += "\tDuration: " + this.duration + " ";
        st += "\tRequirements: ";
        if(requirements.isEmpty()) {
            st += "none";
            return st;
        } else {
            for (String req : requirements)
                st += req + " ";
            return st;
        }
    } // end method toString
}

DataLoader.java

import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DataLoader {
    private ArrayList<String> data;
  
    DataLoader() {
        data = new ArrayList<>();
    }
    DataLoader(String fromFileName) {
        data = new ArrayList<>();
        loadFromFile(fromFileName);
    }
  
    public void addLine(String toAdd) {
        data.add(toAdd);
    }
    public void removeLine(String toRemove) {
        data.remove(toRemove);
    }
    public void removeLine(int index) {
        data.remove(index);
    }
    public void clearAll() {
        data.clear();
    }
    public boolean isEmpty() {
        return data.isEmpty();
    }
    public int getDataSize() { //returns the total amount of lines in the file
        return data.size();
    }
  
    public ArrayList<String> getData() {
        return data;
    }
    public void setData(ArrayList<String> data) {
        this.data = data;
    }
  
    public final boolean loadFromFile(String fileName) {
        FileReader theFileReader;
      
        //try creation of filereader so we can input questions
        try {
            theFileReader = new FileReader(fileName);
            int ch = theFileReader.read();
          
            //parse file
            while(ch != -1) {
                if(ch == (int)'/') { //commented line, go to next line
                    while(theFileReader.read() != (int)'\n') { } //scan to the end of the line
                    ch = theFileReader.read();
                    continue;
                }
              
                //actual line, store in String
                String tempString = "";
                while(ch != (int)'\n') {
                    tempString += (char)ch;
                    ch = theFileReader.read();
                }
              
                if(!tempString.equals("\r") && !tempString.equals("")) { //do not add empty lines
                    addLine(tempString);
                }
                ch = theFileReader.read(); //get next character after newline
            }
          
            theFileReader.close();
        } catch (IOException ex) {
            Logger.getLogger(DataLoader.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      
        return true;
    }
  
    public final boolean saveToFile(String fileName) {
        PrintWriter thePrintWriter;
      
        //try creation of filereader so we can input questions
        try {
            thePrintWriter = new PrintWriter(fileName);
          
            for (String st : getData())
                thePrintWriter.println(st);
          
            thePrintWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(DataLoader.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      
        return true;
    }
}

Add a comment
Know the answer?
Add Answer to:
Introduction - the SeaPort Project series For this set of projects for the course, we wish...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT