Initial Commit

This commit is contained in:
2022-10-07 00:44:12 -03:00
commit 8ff85da81c
308 changed files with 10106 additions and 0 deletions

8
Source Code/Assignment 5/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/../../../../../../../:\ProgrammingProjects\JavaProjects\JavaYear1\CS1083\Assignment 5\.idea/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@ -0,0 +1,7 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<ScalaCodeStyleSettings>
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
</ScalaCodeStyleSettings>
</code_scheme>
</component>

View File

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="PROJECT_FILES" />
</component>
</project>

6
Source Code/Assignment 5/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Assignment 5.iml" filepath="$PROJECT_DIR$/Assignment 5.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,103 @@
Enter number of passengers permitted on ferry: 10
Enter number of axels allowed on ferry: 10
Enter weight limit for ferry: 1600
Enter number of vehicles ferry can hold: 3
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
1
Enter number of passengers: 3
Enter number of axels: 4
Enter weight: 200
==> vehicle added
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
1
Enter number of passengers: 3
Enter number of axels: 4
Enter weight: 300
==> vehicle added
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
3
Enter vehicle ID: 101
Enter new number of passengers: 4
==> vehicle 101's passenger number changed
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
4
100 00,200.00 kg
101 00,300.00 kg
Total Weight: 00,500.00 kg
Total Axels: 8
Total Passengers: 7
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
5
1100.0
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
4
100 00,200.00 kg
101 00,300.00 kg
Total Weight: 00,500.00 kg
Total Axels: 8
Total Passengers: 7
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
2
Enter vehicle ID:
101
==> vehicle removed
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
6
Process finished with exit code 0

View File

@ -0,0 +1,4 @@
Enter number of passengers permitted on ferry: bob
Please enter a valid number
Process finished with exit code 0

View File

@ -0,0 +1,108 @@
Enter number of passengers permitted on ferry: 6
Enter number of axels allowed on ferry: 6
Enter weight limit for ferry: 1200
Enter number of vehicles ferry can hold: 2
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
2
Enter vehicle ID:
1001
The specified vehicle could not not be found
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
3
Enter vehicle ID: 1001
Enter new number of passengers: 5
The specified vehicle could not not be found
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
4
Total Weight: 00,000.00 kg
Total Axels: 0
Total Passengers: 0
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
5
1200.0
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
1
Enter number of passengers: 2
Enter number of axels: 2
Enter weight: 400
==> vehicle added
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
1
Enter number of passengers: 600
Enter number of axels: 2
Enter weight: 200
The vehicle would put the ferry over the passenger limit
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
1
Enter number of passengers: 2
Enter number of axels: 600
Enter weight: 200
The vehicle would put the ferry over the axel limit
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
1
Enter number of passengers: 2
Enter number of axels: 2
Enter weight: 7000
The vehicle would put the ferry over the weight limit
Please select one of these options:
1. Add a vehicle
2. Remove a vehicle
3. Change passengers in a vehicle
4. Print ferry contents list
5. Print weight left
6. Quit
6
Process finished with exit code 0

View File

@ -0,0 +1,8 @@
public class DuplicateVehicleException extends Exception{
public DuplicateVehicleException() {
super("The vehicle that is being added is already in the ferry");
}
public DuplicateVehicleException(String error) {
super(error);
}
}

View File

@ -0,0 +1,135 @@
import java.text.NumberFormat;
/**
Class representing a ferry containing vehicles.
*/
public class Ferry {
private final int maxPass;
private final int maxAxels;
private final double maxWeight;
private int totalPass;
private int totalAxels;
private double totalWeight;
private final Vehicle[] list;
private int comp;
public Ferry (int maxPass, int maxAxels, double maxWeight, int maxVehicles) {
this.maxPass = maxPass;
this.maxAxels = maxAxels;
this.maxWeight = maxWeight;
list = new Vehicle[maxVehicles];
totalPass = 0;
totalAxels = 0;
totalWeight = 0;
comp = 0;
}
public void addVehicle (Vehicle input) throws LimitExceededException, DuplicateVehicleException {
if (comp < list.length) {
if ((totalPass + input.getPass()) <= maxPass) {
if ((totalWeight + input.getWeight()) <= maxWeight) {
if ((totalAxels + input.getAxels()) <= maxAxels) {
if (findVehicle(input.getId()) == -1) {
//add vehicle to list
list[comp] = input;
comp++;
//update totals
totalPass += input.getPass();
totalAxels += input.getAxels();
totalWeight += input.getWeight();
}
else {
throw new DuplicateVehicleException();
}
}
else {
throw new LimitExceededException("The vehicle would put the ferry over the axel limit");
}
}
else {
throw new LimitExceededException("The vehicle would put the ferry over the weight limit");
}
}
else {
throw new LimitExceededException("The vehicle would put the ferry over the passenger limit");
}
}
else {
throw new LimitExceededException("The vehicle would put the ferry over the vehicle limit");
}
}
public void removeVehicle (int id) throws VehicleNotFoundException{
int index = findVehicle(id);
if (index != -1) {
//update totals
totalPass -= list[index].getPass();
totalAxels -= list[index].getAxels();
totalWeight -= list[index].getWeight();
comp--;
list[index] = list[comp];
}
else {
throw new VehicleNotFoundException();
}
}
public void updatePassengers (int id, int numberOfPass) throws VehicleNotFoundException, LimitExceededException{
//check if vehicle is on ferry and update is allowed
int index = findVehicle(id);
if (index != -1) {
int updatedPass = totalPass + numberOfPass - list[index].getPass();
if (updatedPass <= maxPass) {
totalPass = updatedPass;
list[index].setPass(numberOfPass);
}
else {
throw new LimitExceededException("The amount of passengers would exceed the limit of passengers");
}
}
else {
throw new VehicleNotFoundException();
}
}
private int findVehicle (int id) {
//returns the position in the list of the vehicle matching this id
//or -1 if not found
boolean found = false;
int index = -1;
for (int i = 0; i < comp && !found; i++) {
if (id == list[i].getId()) {
index = i;
found = true;
}
}
return index;
}
public double getAvailableWeight () {
return maxWeight - totalWeight;
}
public String toString () {
NumberFormat form = NumberFormat.getInstance();
form.setMaximumFractionDigits(2);
form.setMinimumFractionDigits(2);
form.setMinimumIntegerDigits(5);
String result = "";
for (int i = 0; i < comp; i++) {
result += list[i].getId() + "\t" + form.format(list[i].getWeight()) + " kg\n";
}
result += "Total Weight: \t\t" + form.format(totalWeight) + " kg\n" +
"Total Axels: \t\t" + totalAxels + "\n" +
"Total Passengers: \t" + totalPass;
return result;
}
}

View File

@ -0,0 +1,92 @@
import java.util.*;
/**
A test driver for the Ferry and Vehicle classes.
*/
public class FerryDriver {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int passengerLimit, axelLimit, vehicleLimit;
double weightLimit;
try {
System.out.print("Enter number of passengers permitted on ferry: ");
passengerLimit = sc.nextInt();
System.out.print("Enter number of axels allowed on ferry: ");
axelLimit = sc.nextInt();
System.out.print("Enter weight limit for ferry: ");
weightLimit = sc.nextDouble();
System.out.print("Enter number of vehicles ferry can hold: ");
vehicleLimit = sc.nextInt();
}
catch (InputMismatchException exception) {
System.out.println("Please enter a valid number");
return;
}
Ferry ferry = new Ferry(passengerLimit, axelLimit, weightLimit, vehicleLimit);
boolean done = false;
while (!done) {
try {
System.out.println("\nPlease select one of these options:");
System.out.println("\t1. Add a vehicle");
System.out.println("\t2. Remove a vehicle");
System.out.println("\t3. Change passengers in a vehicle");
System.out.println("\t4. Print ferry contents list");
System.out.println("\t5. Print weight left");
System.out.println("\t6. Quit");
int choice = sc.nextInt();
int passengers, axels;
double weight;
int id;
switch (choice) {
case (1):
sc.nextLine();
System.out.print("Enter number of passengers: ");
passengers = sc.nextInt();
System.out.print("Enter number of axels: ");
axels = sc.nextInt();
System.out.print("Enter weight: ");
weight = sc.nextDouble();
Vehicle v = new Vehicle(passengers, axels, weight);
ferry.addVehicle(v);
System.out.println("==> vehicle added");
break;
case (2):
System.out.println("Enter vehicle ID: ");
id = sc.nextInt();
ferry.removeVehicle(id);
System.out.println("==> vehicle removed");
break;
case (3):
System.out.print("Enter vehicle ID: ");
id = sc.nextInt();
System.out.print("Enter new number of passengers: ");
passengers = sc.nextInt();
ferry.updatePassengers(id, passengers);
System.out.println("==> vehicle " + id + "'s passenger number changed");
break;
case (4):
System.out.println(ferry.toString());
break;
case (5):
System.out.println(ferry.getAvailableWeight());
break;
case (6):
done = true;
break;
default:
System.out.println("Please enter a valid number");
}
}
catch (LimitExceededException | VehicleNotFoundException | DuplicateVehicleException exception) {
System.out.print(exception.getMessage());
}
catch (InputMismatchException exception) {
System.out.println("Please enter a valid number");
sc.next();
}
}
}
}

View File

@ -0,0 +1,8 @@
public class LimitExceededException extends Exception{
public LimitExceededException() {
super("The specified parameter exceeds the limit");
}
public LimitExceededException(String error) {
super(error);
}
}

View File

@ -0,0 +1,86 @@
/**
Represents a vehicle.
*/
public class Vehicle {
/**
The a unique id to be assigned to the next vehicle.
*/
private static int currentId = 100;
/**
The a unique id of the vehicle.
*/
private int id;
/**
The number of passengers in the vehicle.
*/
private int pass;
/**
The number of axels on the vehicle.
*/
private int axels;
/**
The weight of the vehicle in kg.
*/
private double weight;
/**
Constructs a vehicle with a specific number of passengers, axels, and weight.
The id is assigned a unique value.
@param pass The number of passengers in the vehicle.
@param axels The number of axels the vehicle has.
@param weight The weight of the vehicle.
*/
public Vehicle (int pass, int axels, double weight) {
this.pass = pass;
this.axels = axels;
this.weight = weight;
id = currentId;
currentId++;
}
/**
Accesses the number of passengers in the vehicle.
@return The number of passengers.
*/
public int getPass () {
return pass;
}
/**
Accesses the number of axels the vehicle has.
@return The number of axels.
*/
public int getAxels () {
return axels;
}
/**
Accesses the number weight of the vehicle.
@return The weight of the vehicle.
*/
public double getWeight () {
return weight;
}
/**
Accesses the id of the vehicle.
@return The id of the vehicle.
*/
public int getId () {
return id;
}
/**
Sets the number of passengers in the vehicle.
@param numPass The new number of passengers.
*/
public void setPass (int numPass) {
pass = numPass;
}
}

View File

@ -0,0 +1,8 @@
public class VehicleNotFoundException extends Exception{
public VehicleNotFoundException() {
super("The specified vehicle could not not be found");
}
public VehicleNotFoundException(String error) {
super(error);
}
}