Skip to content

Instantly share code, notes, and snippets.

@desrtfx
Last active August 29, 2015 14:18
Show Gist options
  • Save desrtfx/f96558e24e9e8e41d680 to your computer and use it in GitHub Desktop.
Save desrtfx/f96558e24e9e8e41d680 to your computer and use it in GitHub Desktop.
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class HireContract {
// Initialise the properties of the Hire Contract class
private String refNo;
private String customerReference;
private double priceDaily;
private Calendar startDate;
private Calendar endDate;
private Tent tentHired;
// Creating Constructor
public HireContract() {
}
// rmoved the EndDate from the constructor as new contracts will most likely
// not have an end data yet.
// Also removed the tent because the tent should be set with the "setTent"
// method
public HireContract(String refNo, String customerReference,
double priceDaily, Calendar startDate/*
* , Calendar endDate, Tent
* tentHired
*/) {
this.refNo = refNo;
this.customerReference = customerReference;
this.priceDaily = priceDaily;
this.startDate = startDate;
// this.endDate = endDate;
this.endDate = null;
this.tentHired = null;
}
// Creating the get methods for properties
public String getRefNo() {
return refNo;
}
public String getCustomerReference() {
return customerReference;
}
public double getPriceDaily() {
return priceDaily;
}
public Calendar getStartDate() {
return startDate;
}
public Calendar getEndDate() {
return endDate;
}
public Tent getTentHired() {
return tentHired;
}
// Creating the set methods for properties
public void setRefNo(String RefNo) {
this.refNo = RefNo;
}
public void setCustomerReference(String customerReference) {
this.customerReference = customerReference;
}
public void setPriceDaily(double priceDaily) {
this.priceDaily = priceDaily;
}
public void setStartDate(Calendar startDate) {
this.startDate = startDate;
}
public void setEndDate(Calendar endDate) {
this.endDate = endDate;
}
public void setTentHired(Tent tentHired) {
this.tentHired = tentHired;
}
// This method should be called when a Contract gets a Tent
public void setTent(Tent tent) {
// Assign the tent to a contract
tentHired = tent;
// The tent becomes unavailable (false) when the tent is hired.
tentHired.setAvailable(false);
}
// This method should take only one parameter as per assignment and that is
// the endDate
public void terminateHire(/* HireContract contract, */Calendar endDate/*
* ,
* Calendar
* startDate
*/)/*
* throws
* ParseException
*/{
// Just to be sure that there has been a tent assigned:
if (tentHired != null) {
// set the end Date for the contract
this.endDate = endDate;
// Return the tent
tentHired.returnTent(startDate, endDate);
// the following line is per assignment, but actually unnecessary
// since the returnTent
// method (as per assignment) updates the lastReturned date of the
// tent
tentHired.setLastReturned(endDate);
}
}
// This method needs to return a double for the cost of the hire
public/* void */double getTotalCost() {
long time1 = startDate.getTimeInMillis();
long time2 = endDate.getTimeInMillis();
long timeDifference = time2 - time1;
long convertToDays = timeDifference / 1000 / 60 / 60 / 24;
// double PriceDaily = getPriceDaily(); // PriceDaily is a field of the
// class,
// so it can be accessed directly
double totalCost = priceDaily * convertToDays;
// System.out.println("The Total Cost of the Hire is: " + totalCost); //
// Don't print here, just return the result
return totalCost;
}
// For a better version, see the commented toString method in Tent.java
@Override
public String toString() {
// Create an empty String
String info = "";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.UK); // Set
// up
// sdf
if ((refNo != null) && (!refNo.isEmpty())) {
info += "Contract Number: " + refNo + "\n";
}
if ((customerReference != null) && (!customerReference.isEmpty())) {
info += "Customer Reference: " + customerReference + "\n";
}
info += "Daily Price: " + priceDaily;
if (startDate != null) {
info += "Start Date of Rent: " + sdf.format(startDate.getTime())
+ "\n";
}
if (endDate != null) {
info += "End Date of Rent: " + sdf.format(endDate.getTime()) + "\n";
} else {
// Special case. End date is not set yet, but start date is
// this means that the rent contract is ongoing
if (startDate != null) {
info += "Currently active. \n";
}
}
if (tentHired != null) {
info += "Tent Ref.No.: " + tentHired.getRefNo();
}
return info;
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class Tent {
// These are the properties of the tent class as seen in the UML diagram
private String refNo; // changed from RefNo - variables start by convention
// always with a lowercase letter
private String model; // changed from Model - variables start by convention
// always with a lowercase letter
private int width;
private int height;
private int depth;
private int buildTime;
private int totalDaysHired;
private Calendar lastReturned;
// private String availability;
private boolean available; // the assignment states "whether" available -
// best resolved with a boolean
// name "available" is clearer.
private HireContract mostRecentContract;
// Scanner myEndScanner = new Scanner(System.in); // No scanner needed in
// this class.
// Input should come from the main program
// Initialising the Constructor
public Tent() {
}
// Shorthand constructor for a completely new tent (0 days hired &
// available)
public Tent(String RefNo, String Model, int width, int height, int depth,
int buildTime) {
// call the full constructor below
this(RefNo, Model, width, height, depth, buildTime, 0, true);
}
// Full constructor, fills all necessary parameters.
public Tent(String RefNo, String Model, int width, int height, int depth,
int buildTime, int totalDaysHired, boolean available) {
this.refNo = RefNo;
this.model = Model;
this.width = width;
this.height = height;
this.depth = depth;
this.buildTime = buildTime;
this.totalDaysHired = totalDaysHired;
this.available = available;
}
// The following should not be a constructor, it should be handled by
// getter/setter methods
/*
* public Tent(Calendar lastReturned, HireContract mostRecentContract) {
*
* this.lastReturned = lastReturned; this.mostRecentContract =
* mostRecentContract; }
*/
// This constructor is unnecessary - no contract when Tent is created
/*
* public Tent(String RefNo, String Model, int width, int height, int depth,
* int buildTime, int totalDaysHired, Calendar lastReturned, boolean
* availability, HireContract mostRecentContract) {
*
* this.RefNo = RefNo; this.Model = Model; this.width = width; this.height =
* height; this.depth = depth; this.buildTime = buildTime;
* this.totalDaysHired = totalDaysHired; this.lastReturned = lastReturned;
* this.availability = availability; this.mostRecentContract =
* mostRecentContract; }
*/
// Creating the get methods for each of the properties
public String getRefNo() {
return refNo;
}
public String getModel() {
return model;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getDepth() {
return depth;
}
public int getBuildTime() {
return buildTime;
}
public int getTotalDaysHired() {
return totalDaysHired;
}
public Calendar lastReturned() {
return lastReturned;
}
public boolean isAvailable() {
return available;
}
public HireContract getMostRecentContract() {
return mostRecentContract;
}
// Creating the set methods for each property
public void setRefNo(String RefNo) {
this.refNo = RefNo;
}
public void setModel(String Model) {
this.model = Model;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public void setDepth(int depth) {
this.depth = depth;
}
public void setBuildTime(int buildTime) {
this.buildTime = buildTime;
}
public void setTotalDaysHired(int totalDaysHired) {
this.totalDaysHired = totalDaysHired;
}
public void setLastReturned(Calendar lastReturned) {
this.lastReturned = lastReturned;
}
public void setAvailable(boolean available) {
this.available = available;
}
public void setHireContract(HireContract mostRecentContract) {
this.mostRecentContract = mostRecentContract;
}
// Creating the method for the days hired
public void addDaysHired(Calendar startDate, Calendar endDate) {
long startTime = startDate.getTimeInMillis();
long endTime = endDate.getTimeInMillis();
long timeDifference = endTime - startTime;
long daysDiff = timeDifference / 1000 / 60 / 60 / 24;
// the following is unnecessary since you are inside the class
// setTotalDaysHired(getTotalDaysHired() + (int)daysDiff);
totalDaysHired += (int) daysDiff; // that's how to change class fields
// from inside
}
// here you are going wrong.
// The assignment states that "returnTent" should take 2 parameters
// start date and end date of hire
// public void returnTent(HireContract contract, Calendar endDate, Tent
// Availability) throws ParseException{
public void returnTent(Calendar startDate, Calendar endDate) {
// Step 1 - Adjust the dateLastReturned
lastReturned = endDate;
// Step2 - Make the tent available again
available = false;
// Step 3 - create the days difference and add it to the totalHire
addDaysHired(startDate, endDate);
// Forget the code below until ---------------------------------
// This code is unnecessary.
// Creates the end date of the tent being hired and makes the tent
// available for hire again
/*
* SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy",
* Locale.ENGLISH.UK);
* System.out.println("Enter the end date of the new contract."); String
* endInput = myEndScanner.nextLine();
* endCal.setTime(sdf.parse(endInput)); String formatPlsWorkGod =
* sdf.format(endCal.getTime()); // Set instance to cal
* contract.setEndDate(endCal); myEndScanner.close();
*
* setLastReturned(endDate); setAvailability("Available");
* System.out.println(Availability); //} // Have to update
* TotalDaysHired to reflect new hire status //long time1 =
* startDate.getTimeInMillis(); //long time2 =
* endDate.getTimeInMillis(); //long timeDifference = time2-time1;
* //long dayDiff = timeDifference/1000/60/60/24;
* //setTotalDaysHired(getTotalDaysHired() + (int)dayDiff);
*/
// ------------------------------------------------------------- Forget
// up to here */
}
@Override
public String toString() { // This needs a workover as the
// assignment says "where applicable"
// Start by reserving a String
String info = "";
// The if below checks that refNo is not null (i.e. something has
// already
// been assigned to it)
// and that refNo is not empty
if ((refNo != null) && (!refNo.isEmpty())) {
info += "RefNo: " + refNo + "\n";
}
if ((model != null) && (!model.isEmpty())) {
info += "Model: " + model + "\n";
}
if (width > 0) {
info += "Width: " + width + "\n";
}
if (height > 0) {
info += "Height: " + height + "\n";
}
if (depth > 0) {
info += "Depth: " + depth + "\n";
}
if (buildTime > 0) {
info += "Time to build: " + buildTime + " minutes\n";
}
info += "Total days hired so far: " + totalDaysHired + "\n";
// There would be a more elegant way for achieving the above
// with the ternary operator
// I'll show the code - if you heard about the ternary operator, use it
// and remove the
// code above
info += "The tent is currently "
+ (available ? "available" : "rented out") + ".\n";
if (lastReturned != null) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.UK); // Set up sdf
info += "Date last returned: " + sdf.format(lastReturned.getTime()) + "\n";
}
if (mostRecentContract != null) {
info += "Most recent contract Ref: "
+ mostRecentContract.getRefNo();
}
return info;
}
/*
* The method below is the more elegant way of creating the
* toString method.
* It uses a StringBuilder to generate the string
* StringBuilder is a special class made for strings that need assembly
*
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
// The if below checks that refNo is not null (i.e. something has
// already been assigned to it)
// and that refNo is not empty
if ((refNo != null) && (!refNo.isEmpty())) {
sb.append("RefNo: " + refNo + "\n");
}
if ((model != null) && (!model.isEmpty())) {
sb.append("Model: " + model + "\n");
}
if (width > 0) {
sb.append("Width: " + width + "\n");
}
if (height > 0) {
sb.append("Height: " + height + "\n");
}
if (depth > 0) {
sb.append("Depth: " + depth + "\n");
}
if (buildTime > 0) {
sb.append("Time to build: " + buildTime + " minutes\n");
}
sb.append("Total days hired so far: " + totalDaysHired + "\n");
sb.append("The tent is currently "
+ (available ? "available" : "rented out") + ".\n");
if (lastReturned != null) {
sb.append("Date last returned: " + lastReturned.toString() + "\n");
}
if (mostRecentContract != null) {
sb.append(info += "Most recent contract Ref: "
+ mostRecentContract.getRefNo());
}
return sb.toString();
} */
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.Scanner;
public class useTent {
static Scanner keyboard = new Scanner(System.in); // Initialising a scanner
// static Scanner myScanner = new Scanner(System.in); // One Scanner is
// enough
public static void main(String[] args) throws ParseException {
Tent tent1; // Create instance of Tent
HireContract contract1; // Create instance of HireContract
// Calling methods
tent1 = enterTentDetails();
displayTentDetails(tent1);
// enterContractDetails(contract1);
contract1 = processHire(tent1);
terminateHireContract(contract1);
}
// The enterTentDetails returns a Tent object, and doesn't need one passed
// in
public static Tent enterTentDetails() {
// Let the user know that they will be inputting details
// Prompt user to enter Ref No.
System.out.print("Enter the Reference Number of the Tent: ");
String refNo = keyboard.nextLine();
// a.setRefNo(RefNo);
// Prompt user to enter tent model
System.out.print("Enter the Model Name of the Tent: ");
String model = keyboard.nextLine();
// a.setModel(Model);
// Prompt user to enter the width
System.out.print("Enter the width of the Tent in cm: ");
int width = keyboard.nextInt();
// a.setWidth(Width);
// Prompt user to enter the height
System.out.print("Enter the height of the Tent in cm: ");
int height = keyboard.nextInt();
// a.setHeight(Height);
// Prompt user to enter the depth
System.out.print("Enter the depth of the Tent in cm: ");
int depth = keyboard.nextInt();
// a.setDepth(Depth);
// Prompt user to enter the build time
System.out.print("Enter the Time taken to build the Tent in minutes: ");
int buildTime = keyboard.nextInt();
// a.setBuildTime(buildTime);
keyboard.nextLine();
// All necessary data acquired, create the tent:
Tent tent = new Tent(refNo, model, width, height, depth, buildTime);
return tent;
}
// This should not be here as per assignment. This should be done inside the
// assignment
/*
* public static void enterContractDetails(HireContract a) {
*
* // Prompt the user to enter Reference Number
* System.out.println("Enter the Reference Number of the Contract."); String
* RefNo = keyboard.nextLine(); a.setRefNo(RefNo);
*
* // Prompt the user to enter the daily price
* System.out.println("Enter the Daily Price for hiring this Tent."); double
* priceDaily = keyboard.nextDouble(); a.setPriceDaily(priceDaily); }
*/
public static HireContract processHire(Tent tent) throws ParseException {
// Prompt the user to enter Reference Number
System.out.print("Enter the Reference Number of the Contract: ");
String refNo = keyboard.nextLine();
// a.setRefNo(RefNo);
// Prompt for a customer number
System.out.print("Enter the Customer number: ");
String custNo = keyboard.nextLine();
// Prompt the user to enter the daily price
System.out.print("Enter the Daily Price for hiring this Tent:");
double priceDaily = Double.parseDouble(keyboard.nextLine());
// a.setPriceDaily(priceDaily);
// Create instance of calendar
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.UK); // Set
// up
// sdf
System.out.print("Enter the start date of the new contract: ");
String plsWork = keyboard.nextLine();
cal.setTime(sdf.parse(plsWork)); // Sets calendar time to the parsed
// string
// String formatPlsWorkGod = sdf.format(cal.getTime());
// Set instance to cal
// contract.setStartDate(cal);
// Don't close a scanner except in Main
// myScanner.close();
// Now we have all necessary information, create the contract
HireContract contract = new HireContract(refNo, custNo, priceDaily, cal);
// Set the tent to the contract
contract.setTent(tent);
tent.setHireContract(contract);
displayTentDetails(tent);
return contract;
}
public static void terminateHireContract(HireContract contract)
throws ParseException {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.UK); // Set
// up
// sdf
System.out.print("Enter the end date of the contract: ");
String plsWork = keyboard.nextLine();
cal.setTime(sdf.parse(plsWork)); // Sets calendar time to the parsed
// string
// String formatPlsWorkGod = sdf.format(cal.getTime());
// Set instance to cal
contract.setEndDate(cal);
// myScanner.close();
// Display the tent status before
System.out.println("Tent details before contract termination: ");
displayTentDetails(contract.getTentHired());
// Trying to link to terminateHire
contract.terminateHire(cal);
System.out.println();
System.out.println("The total cost of the hire is: "
+ contract.getTotalCost(
));
System.out.println();
System.out.println("Tent details after contract termination: ");
displayTentDetails(contract.getTentHired());
}
public static void displayTentDetails(Tent a) {
System.out.println("- - - - - - - - - -");
System.out.println("Tent Details");
System.out.println("- - - - - - - - - -");
System.out.println(a.toString());
System.out.println("- - - - - - - - - -");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment