Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- /**
- * Coursework 1 Introduction to Programming
- * Write a class for properties. It must be able to create new
- * properties, set and remove tenants, change the occupied status, number of bedrooms, and
- * update the monthly rent. In addition there must be the basic information
- * available about the flat: address and the location in London
- *
- * On top of that there is a report please check the pdf file
- *
- * @author Richard Schwabe
- * @version 1.0.1
- */
- public class Property
- {
- // instance variables
- private String address;
- private char location;
- private Integer monthlyRent;
- private Integer numberOfBedrooms;
- private Boolean occupied;
- private String tenantName;
- /**
- * Constructor for objects of class Property
- */
- public Property(String addressInput, char locationInput, Integer monthlyRentInput, Integer numberOfBedroomsInput)
- {
- // initialise instance variables
- address = addressInput;
- location = locationInput;
- monthlyRent = monthlyRentInput;
- numberOfBedrooms = numberOfBedroomsInput;
- //Default Values:
- tenantName = "";
- occupied = false;
- }
- //##########################################################
- // Print Details Methods
- // The following Methods are going to print out the set Attributes
- // of this Class. See meethod Description for details
- //##########################################################
- /**
- * Prints out the address of the property
- *
- * Example:
- * The address is:
- * 123 Kings Road, SW3 3TT London
- */
- public void printAddress()
- {
- System.out.println( "The address is: " );
- System.out.println( address );
- }
- /**
- * Prints out the location of the property
- *
- * Example:
- * The location is: S
- */
- public void printLocation()
- {
- System.out.println( "The location is: " );
- System.out.println( location );
- }
- /**
- * Prints out the monthly cost of the property
- *
- * Example:
- * The monthly rent is: 1200 Pound
- */
- public void printMonthlyRent()
- {
- System.out.println( "The monthly rent is: " );
- System.out.println( monthlyRent + " Pound" );
- }
- /**
- * Prints out the amount of bedrooms for this property
- *
- * Example:
- * The number of bedrooms is: 2
- */
- public void printNumberOfBedrooms()
- {
- System.out.println( "The number of bedrooms is: " );
- System.out.println( numberOfBedrooms );
- }
- /**
- * Prints out the occupied status of the property
- *
- * Example:
- * This property is currently occupied
- */
- public void printOccupiedStatus()
- {
- // initialise new variable occupiedStatus
- // occupiedStatus = Human readable form for the variable occupied
- String occupiedStatus;
- // check if the property is occupied or not
- // create answer sentence depening on the case
- if ( occupied )
- {
- occupiedStatus = "This property is currently occupied";
- }
- else
- {
- occupiedStatus = "This property is currently not occupied";
- }
- System.out.println( occupiedStatus );
- }
- /**
- * Prints out the tenant of the property
- *
- * Example:
- * The tenant is:
- * Cinderella
- */
- public void printTenant()
- {
- //check if the tenant name is not empty then print
- //otherwise tell the user that there is no tenant name available
- if ( !tenantName.isEmpty() )
- {
- System.out.println( "The tenant is: " );
- System.out.println( tenantName );
- }
- else
- {
- System.out.println( "Currnetly there is no tenant for this property." );
- }
- }
- /**
- * Prints information of the property in detail.
- * That includes: address, location in a proper way, monthlyrent, number of bedrooms, occupied status
- * and the tenants name
- */
- public void printDetailsOfProperty()
- {
- System.out.println( "++++ DETAILS OF PROPERTY ++++" );
- this.printAddress();
- System.out.println( "Area: " + this.getLocationName() );
- this.printMonthlyRent();
- this.printNumberOfBedrooms();
- this.printOccupiedStatus();
- this.printTenant();
- System.out.println( "++++ END OF PROPERTY DETAILS ++++" );
- System.out.println( "++++ \u00a9 by Richard Schwabe ++++" );
- }
- /**
- * This method retuns the location in a human readable and understandable string
- *
- * @return locationName "[North | South | West | East | Greater] London"
- */
- public String getLocationName()
- {
- //initialise the variables
- String locationName, locationDirection;
- //make sure that all the character is written in upperCase for an easier handling
- location = Character.toUpperCase( location );
- //depending on the character write full word
- switch ( location )
- {
- case 'S':
- locationDirection = "South";
- break;
- case 'N':
- locationDirection = "North";
- break;
- case 'W':
- locationDirection = "West";
- break;
- case 'E':
- locationDirection = "East";
- break;
- default:
- locationDirection = "Greater";
- }
- //add London to the locationDirection
- locationName = locationDirection + " London";
- //return the location name
- return locationName;
- }
- //##########################################################
- // Update Methods
- // The following Methods are going to update variables in the property class
- // See meethod Description for details
- //##########################################################
- /**
- * Updates the monthly cost of the property
- *
- * Example:
- * 1200
- *
- * @param newRent Must be an integer greather than 0
- */
- public void setMonthlyRent( int newRent )
- {
- if ( newRent > 0 )
- {
- monthlyRent = newRent;
- }
- else
- {
- System.out.println("This amount cannot be accepted as the new monthly rent.");
- }
- }
- /**
- * Updates the tenant name.
- *
- * Example:
- * The new tenant is: Thomas R. Smith
- *
- * @param newTenant Must be a string, will be the name of the tenant
- */
- public void setTenant( String newTenant )
- {
- //check if property is occupied, if so inform via console
- //print the current tenant
- //otherwise set the new tenant name
- if ( !occupied && !tenantName.isEmpty() )
- {
- //print that there is not tenant and the flat is empty
- System.out.println( "Cannot update tenant name, because this flat is already occupied by a tenant." );
- System.out.println( "The current tenant is: " );
- this.printTenant();
- }
- else
- {
- tenantName = newTenant;
- occupied = true;
- System.out.println( "The new tenant is: " + tenantName );
- }
- }
- /**
- * Remove the Tenant from the property.
- *
- * Example:
- * NO EXAMPLE GIVEN
- *
- * @param confirmation Must confirm the action to remove the tenant.
- */
- public void removeTenant( Boolean confirmation)
- {
- //initialise variable for outcome message
- String message;
- //the user has to confirm that the current tenant should be removed
- //if not the object will not change.
- if ( confirmation )
- {
- //remove tenant
- //set occupied to false
- tenantName = "";
- occupied = false;
- //set the confirmation message
- message = "The tenant was removed from the flat.";
- }
- else
- {
- //abort the tenant removal
- message = "Action: removeTenant aborted.";
- }
- //print the message
- System.out.println( message );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement