Skip to content

Instantly share code, notes, and snippets.

@daniel-stoneuk
Last active February 2, 2017 12:44
Show Gist options
  • Save daniel-stoneuk/594cecc224fbe27992c537a61e3f107b to your computer and use it in GitHub Desktop.
Save daniel-stoneuk/594cecc224fbe27992c537a61e3f107b to your computer and use it in GitHub Desktop.
Pre release 2016
import java.text.DecimalFormat;
import java.util.Scanner;
public class Tasks {
public static void main(String[] args) {
task2();
}
public static void task1() {
// init variables
double[] dimensions = new double[3];
double weight = 0d;
// create error string with dummy variable
String message = "";
Scanner in = new Scanner(System.in);
for (int i = 0; i < dimensions.length; i++) {
while (true) {
try {
System.out.println("Enter a valid dimension for dimension " + (i + 1) + ": ");
dimensions[i]= in.nextDouble();
if (dimensions[i] >= 80 ) {
message += "\nDimension " + (i + 1) + " is too large";
}
break;
} catch (Exception e) {
// input value is not valid
in.next();
}
}
}
if (dimensions[0] + dimensions[1] + dimensions[2] > 200) {
message += "\nDimension total is too large";
}
while (true) {
try {
System.out.println("Enter a valid weight: ");
weight = in.nextDouble();
break;
} catch (Exception e) {
in.next();
}
}
if (weight < 1 || weight > 10) {
message += "\nWeight is not acceptable";
}
if (!message.equals("")) {
System.out.println("\nParcel rejected: " + message);
} else {
System.out.println("Parcel accepted :D");
}
}
public static void task2() {
DecimalFormat decimalFormat = new DecimalFormat("00.00");
Scanner in = new Scanner(System.in);
int numberInConsignment = -1;
while (numberInConsignment == -1) {
try {
System.out.println("How many parcels do you have? ");
numberInConsignment = in.nextInt();
} catch (Exception e) {
System.out.println("Please enter a valid integer");
in.next();
}
}
String rejectedmessage = "";
String acceptedmessage = "";
int accepted = 0;
double totalWeight = 0;
double totalCost = 0;
for (int j = 0; j < numberInConsignment; j++) {
System.out.println("Parcel " + (j+1) + ":");
// init variables
double[] dimensions = new double[3];
double weight = 0d;
double cost = 0;
String tempMessage = "";
for (int i = 0; i < dimensions.length; i++) {
while (true) {
try {
System.out.println("Enter a valid dimension for dimension " + (i + 1) + ": ");
dimensions[i]= in.nextDouble();
if (dimensions[i] >= 80 ) {
tempMessage += "\n - Dimension " + (i + 1) + " is too large";
}
break;
} catch (Exception e) {
// input value is not valid
in.next();
}
}
}
if (dimensions[0] + dimensions[1] + dimensions[2] > 200) {
tempMessage += "\n - Dimension total is too large";
}
while (true) {
try {
System.out.println("Enter a valid weight: ");
weight = in.nextDouble();
break;
} catch (Exception e) {
in.next();
}
}
if (weight < 1 || weight > 10) {
tempMessage += "\n - Weight is not acceptable";
} else if (weight <= 5) {
cost += 10d;
} else {
cost += 10d;
double weightInGrams = weight * 100;
while (weightInGrams > 0) {
weightInGrams -= 100;
cost += 0.1d;
}
}
if (!tempMessage.equals("")) {
rejectedmessage += "\nParcel " + (j+1) + " rejected: " + tempMessage;
} else {
acceptedmessage += "\nParcel " + (j+1) + " cost $" + decimalFormat.format(cost);
accepted ++;
totalCost += cost;
totalWeight += weight;
}
}
System.out.println(accepted + "/" + numberInConsignment + " parcels were accepted totalling a weight of " + decimalFormat.format(totalWeight) + "kg \n\nHere's the bill:" + acceptedmessage);
if (accepted != numberInConsignment && !rejectedmessage.equals("")) {
System.out.println("\nThese parcels were rejected: " + rejectedmessage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment