main/KoalaRescue.class
package main;
public synchronized class KoalaRescue {
public void KoalaRescue();
public static void main(String[]) throws java.io.FileNotFoundException;
private static void initRescue(java.util.Scanner, int) throws java.io.FileNotFoundException;
private static void writeUpdateCountOfTreesToFile(int[][]);
private static int[] toIntArray(String);
}
main/KoalaRescue.ctxt
#BlueJ class context
comment0.target=KoalaRescue
comment0.text=\r\n\ KoalaRescue\r\n\r\n\ @version\ 1.0\r\n\ @since\ 06-Jun-2020\r\n
comment1.params=args
comment1.target=void\ main(java.lang.String[])
comment2.params=in\ budget
comment2.target=void\ initRescue(java.util.Scanner,\ int)
comment3.params=updatedTreesCounts
comment3.target=void\ writeUpdateCountOfTreesToFile(int[][])
comment4.params=line
comment4.target=int[]\ toIntArray(java.lang.String)
numComments=5
main/KoalaRescue.java
main/KoalaRescue.java
package main;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import rescue.exceptions.BudgetExceededException;
import rescue.exceptions.InvalidOptionException;
import rescue.reserve.Reserve;
/**
* KoalaRescue
*
* @version 1.0
* @since 06-Jun-2020
*/
public class KoalaRescue {
public static void main( String[] args ) throws FileNotFoundException {
System.out.println("Welcome to koala rescue program.");
System.out.println("Enter rescue team leader name:");
Scanner in = new Scanner(System.in);
boolean isNameValid = false;
String name;
while (!isNameValid) {
name = in.nextLine();
if (name.isEmpty()) {
isNameValid = false;
System.out.println("Invalid name.\nEnter name again:");
}
else if (name.length() > 16) {
isNameValid = false;
System.out.println("Name must be 16 char at max.\nEnter name again:");
}
else {
isNameValid = true;
}
}
System.out.println("Enter budget for rescue:");
boolean isBudgetValid = false;
Integer budget = null;
while (!isBudgetValid) {
budget = in.nextInt();
if (budget < 100) {
isBudgetValid = false;
System.out.println("Budget not enough to start rescue program.\nEnter a different budget:");
}
else if (budget > 200) {
isBudgetValid = false;
System.out.println("Too much budget for rescue program.\nEnter a different budget:");
}
else {
isBudgetValid = true;
}
}
in.nextLine();
initRescue(in, budget);
}
private static void initRescue(Scanner in, int budget) throws FileNotFoundException {
Reserve reserve = new Reserve(budget);
File observationsFile = new File("main/trees-uj0vkyhe-bupqfoec.txt");
Scanner reader = new Scanner(observationsFile);
List
observations = new ArrayList<>();
while (reader.hasNextLine()) {
observations.add(reader.nextLine());
}
reader.close();
for(int i = 0; i < observations.size(); i++) {
String observation = observations.get(i);
reserve.setNewObservationPoint(i, toIntArray(observation));
// Actions for an observation continues as far as this is active observation is there
System.out.println("\nObservation No. " + (i + 1));
while (reserve.getActiveObservationPoint() != null) {
System.out.println();
reserve.showObservationRescueStatus();
reserve.showInstructions();
String command = in.nextLine();
try {
reserve.command(command);
}
catch(InvalidOptionException e) {
System.out.println("Only A, B, C & D are valid options. Please try Again.");
}
catch (BudgetExceededException e) {
System.out.println("Not enough money left for this option.");
}
}
}
reserve.showRescueMissionResult();
writeUpdateCountOfTreesToFile(reserve.getUpdatedTrees());
}
private static void writeUpdateCountOfTreesToFile(int [][] updatedTreesCounts) {
try {
File out = new File("main/updatedTrees.txt");
if (out.createNewFile()) {
FileWriter writer = new FileWriter(out);
for(int[] treeCounts : updatedTreesCounts) {
StringBuilder data = new StringBuilder();
for(int count : treeCounts) {
data.append(count).append(",");
}
String line = data.toString();
writer.write(line.substring(0, line.length() -1));
writer.write("\n");
}
writer.close();
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
private static int[] toIntArray(String line) {
line = line.trim();
String[] tokens = line.split(",");
if (tokens.length != 5) {
throw new IllegalArgumentException("entries of trees in input file for a observations must be 5.");
}
else {
int[] noOfTreesEachType = new int[5];
for (int i = 0; i < tokens.length; i++) {
noOfTreesEachType[i] = Integer.parseInt(tokens[i].trim());
}
return noOfTreesEachType;
}
}
}
main/package.bluej
#BlueJ package file
objectbench.height=92
objectbench.width=761
package.divider.horizontal=0.6
package.divider.vertical=0.8014354066985646
package.editor.height=396
package.editor.width=674
package.editor.x=12
package.editor.y=12
package.frame.height=600
package.frame.width=800
package.numDependencies=0
package.numTargets=1
package.showExtends=true
package.showUses=true
readme.height=58
readme.name=@README
readme.width=47
readme.x=10
readme.y=10
target1.height=70
target1.name=KoalaRescue
target1.showInterface=false
target1.type=ClassTarget
target1.width=100
target1.x=160
target1.y=10
main/trees-uj0vkyhe-bupqfoec.txt
2,4,5,0,6
4,2,1,5,10
3,0,2,6,9
2,1,5,2,8
2,6,5,0,7
0,12,1,2,8
4,2,0,5,4
7,8,10,5,9
2,1,5,2,5
2,3,5,0,4
main/updatedTrees.txt
2,4,5,0,6
4,2,1,5,9
3,0,2,6,9
2,1,4,2,8
2,6,4,0,7
0,11,1,1,8
4,1,0,4,3
7,7,10,4,9
2,0,5,2,5
2,3,4,0,4
package.bluej
#BlueJ package file
editor.fx.0.height=838
editor.fx.0.width=1550
editor.fx.0.x=-7
editor.fx.0.y=-7
objectbench.height=218
objectbench.width=1512
package.divider.horizontal=0.6
package.divider.vertical=0.6953463203463203
package.editor.height=508
package.editor.width=1424
package.editor.x=0
package.editor.y=0
package.frame.height=838
package.frame.width=1550
package.numDependencies=0
package.numTargets=2
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.height=58
readme.name=@README
readme.width=47
readme.x=10
readme.y=10
target1.height=62
target1.name=main
target1.type=PackageTarget
target1.width=80
target1.x=70
target1.y=10
target2.height=62
target2.name=rescue
target2.type=PackageTarget
target2.width=80
target2.x=160
target2.y=10
README.TXT
------------------------------------------------------------------------
This is the project README file. Here, you should describe your project.
Tell the reader (someone who does not know anything about this project)
all he/she needs to know. The comments should usually include at least:
------------------------------------------------------------------------
PROJECT TITLE: Koala rescue
PURPOSE OF PROJECT: To save koalas
VERSION or DATE: 1
HOW TO START THIS PROJECT: Run file main/KoalaRescue.java
AUTHORS:
USER INSTRUCTIONS:
rescue/exceptions/BudgetExceededException.class
package rescue.exceptions;
public synchronized class BudgetExceededException extends Exception {
public void BudgetExceededException();
}
rescue/exceptions/BudgetExceededException.ctxt
#BlueJ class context
comment0.target=BudgetExceededException
comment0.text=\r\n\ \r\n\ BudgetExceededException\r\n\
\r\n\ @version\ 1.0\r\n\ @since\ 06-Jun-2020\r\n
comment1.params=
comment1.target=BudgetExceededException()
numComments=2
rescue/exceptions/BudgetExceededException.java
rescue/exceptions/BudgetExceededException.java
package rescue.exceptions;
/**
*
* BudgetExceededException
*
* @version 1.0
* @since 06-Jun-2020
*/
public class BudgetExceededException extends Exception {
public BudgetExceededException() {
super("Do not have sufficient funds.");
}
}
rescue/exceptions/InvalidOptionException.class
package rescue.exceptions;
public synchronized class InvalidOptionException extends Exception {
public void InvalidOptionException();
}
rescue/exceptions/InvalidOptionException.ctxt
#BlueJ class context
comment0.target=InvalidOptionException
comment0.text=\r\n\ \r\n\ InvalidOptionException\r\n\
\r\n\ @version\ 1.0\r\n\ @since\ 06-Jun-2020\r\n
comment1.params=
comment1.target=InvalidOptionException()
numComments=2
rescue/exceptions/InvalidOptionException.java
rescue/exceptions/InvalidOptionException.java
package rescue.exceptions;
/**
*
* InvalidOptionException
*
* @version 1.0
* @since 06-Jun-2020
*/
public class InvalidOptionException extends Exception {
public InvalidOptionException() {
super("Invalid Option");
}
}
rescue/exceptions/package.bluej
#BlueJ package file
objectbench.height=92
objectbench.width=761
package.divider.horizontal=0.6
package.divider.vertical=0.8014354066985646
package.editor.height=396
package.editor.width=674
package.editor.x=32
package.editor.y=32
package.frame.height=600
package.frame.width=800
package.numDependencies=0
package.numTargets=2
package.showExtends=true
package.showUses=true
readme.height=58
readme.name=@README
readme.width=47
readme.x=10
readme.y=10
target1.height=50
target1.name=BudgetExceededException
target1.showInterface=false
target1.type=ClassTarget
target1.width=190
target1.x=10
target1.y=90
target2.height=50
target2.name=InvalidOptionException
target2.showInterface=false
target2.type=ClassTarget
target2.width=170
target2.x=10
target2.y=150
rescue/package.bluej
#BlueJ package...