Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.74 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. /**
  4.  * Coursework 1 Introduction to Programming
  5.  * Write a class for properties. It must be able to create new
  6.  * properties, set and remove tenants, change the occupied status, number of bedrooms, and
  7.  * update the monthly rent. In addition there must be the basic information
  8.  * available about the flat: address and the location in London
  9.  *
  10.  * On top of that there is a report please check the pdf file
  11.  *
  12.  * @author Richard Schwabe
  13.  * @version 1.0.1
  14.  */
  15. public class Property
  16. {
  17.     // instance variables
  18.     private String address;
  19.     private char location;
  20.     private Integer monthlyRent;
  21.     private Integer numberOfBedrooms;
  22.     private Boolean occupied;
  23.     private String tenantName;
  24.  
  25.     /**
  26.      * Constructor for objects of class Property
  27.      */
  28.     public Property(String addressInput, char locationInput, Integer monthlyRentInput, Integer numberOfBedroomsInput)
  29.     {
  30.         // initialise instance variables
  31.         address             =   addressInput;
  32.         location            =   locationInput;
  33.         monthlyRent         =   monthlyRentInput;
  34.         numberOfBedrooms    =   numberOfBedroomsInput;
  35.        
  36.         //Default Values:
  37.         tenantName  =   "";
  38.         occupied    =   false;        
  39.     }
  40.    
  41.     //##########################################################
  42.     // Print Details Methods
  43.     // The following Methods are going to print out the set Attributes
  44.     // of this Class. See meethod Description for details
  45.     //##########################################################
  46.     /**
  47.      * Prints out the address of the property
  48.      *  
  49.      * Example:
  50.      * The address is:
  51.      * 123 Kings Road, SW3 3TT London
  52.      */
  53.     public void printAddress()
  54.     {
  55.         System.out.println( "The address is: " );
  56.         System.out.println( address );
  57.     }
  58.    
  59.     /**
  60.      * Prints out the location of the property
  61.      *
  62.      * Example:
  63.      * The location is: S
  64.      */
  65.     public void printLocation()
  66.     {
  67.         System.out.println( "The location is: " );
  68.         System.out.println( location );        
  69.     }    
  70.    
  71.     /**
  72.      * Prints out the monthly cost of the property
  73.      *
  74.      * Example:
  75.      * The monthly rent is: 1200 Pound
  76.      */
  77.     public void printMonthlyRent()
  78.     {
  79.         System.out.println( "The monthly rent is: " );
  80.         System.out.println( monthlyRent + " Pound" );          
  81.     }
  82.    
  83.     /**
  84.      * Prints out the amount of bedrooms for this property
  85.      *
  86.      * Example:
  87.      * The number of bedrooms is: 2
  88.      */    
  89.     public void printNumberOfBedrooms()
  90.     {
  91.         System.out.println( "The number of bedrooms is: " );
  92.         System.out.println( numberOfBedrooms );    
  93.     }
  94.    
  95.     /**
  96.      * Prints out the occupied status of the property
  97.      *
  98.      * Example:
  99.      * This property is currently occupied
  100.      */
  101.     public void printOccupiedStatus()
  102.     {
  103.         // initialise new variable occupiedStatus
  104.         // occupiedStatus = Human readable form for the variable occupied
  105.         String occupiedStatus;
  106.        
  107.         // check if the property is occupied or not
  108.         // create answer sentence depening on the case
  109.         if ( occupied )
  110.         {
  111.             occupiedStatus = "This property is currently occupied";
  112.         }
  113.         else
  114.         {
  115.             occupiedStatus = "This property is currently not occupied";                
  116.         }
  117.        
  118.         System.out.println( occupiedStatus );      
  119.     }
  120.    
  121.     /**
  122.      * Prints out the tenant of the property
  123.      *
  124.      * Example:
  125.      * The tenant is:
  126.      * Cinderella
  127.      */
  128.     public void printTenant()
  129.     {
  130.         //check if the tenant name is not empty then print
  131.         //otherwise tell the user that there is no tenant name available
  132.         if ( !tenantName.isEmpty() )
  133.         {
  134.             System.out.println( "The tenant is: " );
  135.             System.out.println( tenantName );    
  136.         }
  137.         else
  138.         {
  139.             System.out.println( "Currnetly there is no tenant for this property." );  
  140.         }
  141.     }
  142.    
  143.     /**
  144.      * Prints information of the property in detail.
  145.      * That includes: address, location in a proper way, monthlyrent, number of bedrooms, occupied status
  146.      * and the tenants name
  147.      */
  148.     public void printDetailsOfProperty()
  149.     {
  150.         System.out.println( "++++ DETAILS OF PROPERTY ++++" );
  151.         this.printAddress();
  152.         System.out.println( "Area: " + this.getLocationName() );
  153.         this.printMonthlyRent();
  154.         this.printNumberOfBedrooms();
  155.         this.printOccupiedStatus();
  156.         this.printTenant();
  157.         System.out.println( "++++ END OF PROPERTY DETAILS ++++" );
  158.         System.out.println( "++++ \u00a9 by Richard Schwabe ++++" );
  159.     }
  160.    
  161.     /**
  162.      * This method retuns the location in a human readable and understandable string
  163.      *
  164.      * @return locationName     "[North | South | West | East | Greater] London"
  165.      */
  166.     public String getLocationName()
  167.     {
  168.         //initialise the variables
  169.         String locationName, locationDirection;
  170.        
  171.         //make sure that all the character is written in upperCase for an easier handling
  172.         location    =   Character.toUpperCase( location );
  173.        
  174.         //depending on the character write full word
  175.         switch ( location )
  176.         {
  177.             case 'S':
  178.                 locationDirection    =   "South";
  179.                 break;
  180.                
  181.             case 'N':
  182.                 locationDirection    =   "North";
  183.                 break;
  184.                
  185.             case 'W':
  186.                 locationDirection    =   "West";
  187.                 break;
  188.                
  189.             case 'E':
  190.                 locationDirection    =   "East";
  191.                 break;
  192.                
  193.            default:
  194.                 locationDirection    =   "Greater";
  195.         }
  196.        
  197.         //add London to the locationDirection
  198.         locationName    =   locationDirection + " London";
  199.        
  200.         //return the location name
  201.         return locationName;
  202.     }
  203.    
  204.     //##########################################################
  205.     // Update Methods
  206.     // The following Methods are going to update variables in the property class
  207.     // See meethod Description for details
  208.     //##########################################################
  209.     /**
  210.      * Updates the monthly cost of the property
  211.      *
  212.      * Example:
  213.      * 1200
  214.      *
  215.      * @param newRent   Must be an integer greather than 0
  216.      */
  217.     public void setMonthlyRent( int newRent )
  218.     {
  219.         if ( newRent > 0 )
  220.         {
  221.             monthlyRent     =   newRent;
  222.         }
  223.         else
  224.         {
  225.             System.out.println("This amount cannot be accepted as the new monthly rent.");
  226.         }
  227.     }
  228.    
  229.     /**
  230.      * Updates the tenant name.
  231.      *
  232.      * Example:
  233.      * The new tenant is: Thomas R. Smith
  234.      *
  235.      * @param newTenant   Must be a string, will be the name of the tenant
  236.      */
  237.     public void setTenant( String newTenant )
  238.     {
  239.         //check if property is occupied, if so inform via console
  240.         //print the current tenant
  241.         //otherwise set the new tenant name
  242.         if ( !occupied && !tenantName.isEmpty() )
  243.         {
  244.             //print that there is not tenant and the flat is empty
  245.             System.out.println( "Cannot update tenant name, because this flat is already occupied by a tenant." );
  246.             System.out.println( "The current tenant is: " );
  247.             this.printTenant();
  248.         }
  249.         else
  250.         {
  251.             tenantName  =   newTenant;
  252.             occupied    =   true;
  253.             System.out.println( "The new tenant is: " + tenantName );
  254.         }
  255.     }
  256.    
  257.     /**
  258.      * Remove the Tenant from the property.
  259.      *
  260.      * Example:
  261.      * NO EXAMPLE GIVEN
  262.      *
  263.      * @param confirmation  Must confirm the action to remove the tenant.
  264.      */
  265.     public void removeTenant( Boolean confirmation)
  266.     {
  267.         //initialise variable for outcome message
  268.         String message;
  269.        
  270.         //the user has to confirm that the current tenant should be removed
  271.         //if not the object will not change.
  272.         if ( confirmation )
  273.         {
  274.             //remove tenant
  275.             //set occupied to false
  276.             tenantName  =   "";
  277.             occupied    =   false;
  278.            
  279.             //set the confirmation message
  280.             message     =   "The tenant was removed from the flat.";
  281.         }
  282.         else
  283.         {
  284.             //abort the tenant removal
  285.             message     =   "Action: removeTenant aborted.";
  286.         }
  287.         //print the message
  288.         System.out.println( message );
  289.        
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement