Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleDel 7 - Objektstrukturer og java-teknikker (10 %)

Implementer metodene i Realtor

  • Realtor(double comission) oppretter et Realtor objekt med en gitt provisjon
  • setCommission(double comission) Oppdaterer provisjonen til megleren
  • addProperty(Property property) Legger til en eiendom i porteføljen til megleren
  • calculateTotalCommission() - Regner ut total provisjonslønn basert på alle solgte boliger megleren har
  • iterator() - Returnerer en iterator for å iterere gjennom alle eiendommene til megleren
Expand
titleKodeskjelett del 7


Code Block
languagejava
package del5_8;

import java.util.Iterator;

public class Realtor implements Iterable<Property> {

	/**
	 * Creates a Realtor object
	 * 
	 * @param name       the name of the realtor
	 * @param commission the commission the realtor takes for a sale
	 */
	public Realtor(String name, double commission) {
		// TODO
	}

	/**
	 * 
	 * @return the name of the realtor
	 */
	public String getName() {
		// TODO
		return null;
	}

	/**
	 * 
	 * @param commission the new commission of the realtor
	 * 
	 * @throws IllegalArgumentException if the commission not between (excluding) 0
	 *                                  and (including) 100.
	 */
	public void setCommission(double commission) {
		// TODO
	}

	/**
	 * Adds a property to the realtor's sale collection
	 * 
	 * @param property a property
	 */
	public void addProperty(Property property) {
		// TODO
	}

	/**
	 * The total commission is calculated as the sum of the highest bid of each sold
	 * property times the commission rate. The commission rate is calculated based
	 * on the realtor's current commission rate and does not need to consider
	 * historical commission rates
	 * 
	 * A realtor with commission of 10 %, and two sold properties sold at 1000 each,
	 * would have a total commission value of 200
	 * 
	 * @return the calculated commission of the realtor
	 */
	public double calculateTotalCommission() {
		// TODO
		return 0;
	}

	@Override
	public Iterator<Property> iterator() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 
	 * @return an iterator to be able to iterate through all the properties of this
	 *         realtor
	 */
	public Iterator<Property> iterable() {
		return null;
	}

	public static void main(String[] args) {
		Realtor realtor = new Realtor("test", 10);
		// The following will only work if BusinessProperty and Property has the correct
		// implementation
		Property p = new Property("name", 1500);
		p.bidReceived("BIDDER", 2000);
		p.setIsSold();
		realtor.addProperty(p);
		// Should be 200
		System.out.println(realtor.calculateTotalCommission());
	}

}



Expand
titleLF


Code Block
languagejava
package del5_8;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Realtor implements Iterable<Property>{

	private List<Property> properties = new ArrayList<>();
	private double commission;
	private String name;
	/**
	 * Creates a Realtor object
	 * 
	 * @param name      the name of the realtor
	 * @param comission the commission the realtor takes for a sale
	 */
	public Realtor(String name, double commission) {
		this.setCommission(commission);
		this.name = name;
	}
	
	public String getName() {
		return this.name;
	}

	/**
	 * 
	 * @param comission the new comission of the realtor
	 * 
	 * @throws IllegalArgumentException if the commission not between (excluding) 0
	 *                                  and (including) 100.
	 */
	public void setCommission(double commission) {
		if (commission <= 0 || commission > 100) {
			throw new IllegalArgumentException("Comission must be between 0 and 100");
		}
		this.commission = commission;
	}

	/**
	 * Adds a property to the realtor's sale collection
	 * 
	 * @param property a property
	 */
	public void addProperty(Property property) {
		this.properties.add(property);
	}

	/**
	 * The total comission is calculated as the sum of the highest bid of each sold
	 * property times the commission rate The comission rate is calculated based on
	 * the realtor's current commission rate and does not need to consider
	 * historical commission rates
	 * 
	 * @return the calculated commission of the realtor
	 */
	public double calculateTotalCommission() {
		return this.properties.stream().filter(p -> p.isSold()).mapToDouble(Property::getHighestBid)
				.map(price -> price * (commission) / 100.0).sum();
	}

	@Override
	public Iterator<Property> iterator() {
		return this.properties.iterator();
	}
	
	public String toString() {
		return this.name;
	}	
	public static void main(String[] args) {
		Realtor realtor = new Realtor("test", 10);
		// Will only work if BusinessProperty and Property has the correct
		// implementation
		Property p = new Property("name", 1500);
		p.bidReceived("BIDDER", 2000);
		p.setIsSold();
		realtor.addProperty(p);
		// Should be 200
		System.out.println(realtor.calculateTotalCommission());
	}

}



...

Expand
titleDel 8 - Interfaces og Comparator (5 %)

Implementer metoden i RealtorComparator

  • sortRealtorsByHighestBidReceived - Returnerer en Comparator som sorterer en liste med Realtor-objekter. Disse skal sorteres etter hvilken megler som har oppnådd det høyeste budet basert på alle eiendommene fra høyest til lavest

    Expand
    titleKodeskjelett del 8


    Code Block
    languagejava
    package del5_8;
    
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class RealtorComparator {
    
    	/**
    	 * 
    	 * @return a comparator that sorts Realtor objects based on the highest bid they
    	 *         have received on any of their properties from highest to lowest
    	 * 
    	 *         Example output with a list of one "realtor1" with two properties,
    	 *         property1 with highest bid 1000, and property2 with highest bid 2000,
    	 *         and a second "realtor2" with one property with the highest bid of
    	 *         500, and a third "realtor3" with one property with the highest bid of
    	 *         3000 would yield the following result
    	 * 
    	 *         realtor3
    	 *         realtor1 
    	 *         realtor2
    	 * 
    	 *         I.e: This sorts by the highest, single sale, and not the total of
    	 *         sales for each realtor.
    	 * 
    	 * 
    	 */
    	public static Comparator<Realtor> sortRealtorsByHighestBidReceived() {
    		return null;
    	}
    
    	public static void main(String[] args) {
    		// This may not yield the correct results if Property/BusinessProperty/Realtor
    		// has not been correctly implemented
    		Realtor realtor = new Realtor("test1", 10);
    		Realtor realtor2 = new Realtor("test2", 10);
    		Property p = new BusinessProperty("name", 1500);
    		p.bidReceived("BIDDER", 2000);
    		Property p2 = new BusinessProperty("name2", 1000);
    		p2.bidReceived("BIDDER", 1500);
    		realtor.addProperty(p);
    		realtor2.addProperty(p2);
    		List<Realtor> realtors = Arrays.asList(realtor2, realtor);
    		System.out.println(realtors);
    		Collections.sort(realtors, sortRealtorsByHighestBidReceived());
    		// A more useful to string method or using the debugger might be helpful here
    		// Should be in the opposite direction with realtor, and then realtor2
    		System.out.println(realtors);
    
    	}
    
    }
    



    Expand
    titleLF


    Code Block
    languagejava
    package del5_8;
    
    
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class RealtorComparator {
    
    	/**
    	 * 
    	 * @return a comparator that sort Realtor objects based on the highest bid they
    	 *         have received on any of their properties from highest to lowest
    	 * 
    	 *         Example output with a list of one "realtor1" with two properties,
    	 *         property1 with highest bid 1000, and property2 with highest bid 2000,
    	 *         and a second "realtor2" with one property with the highest bid of
    	 *         500, and a third "realtor3" with one property with the highest bid of
    	 *         3000 would yield the following result
    	 * 
    	 *         realtor3 
    	 *         realtor1 
    	 *         realtor2
    	 * 
    	 * 
    	 */
    	public static Comparator<Realtor> sortRealtorsByHighestBidReceived() {
    		return (Realtor r1, Realtor r2) -> {
    			int highestBidR1 = 0;
    			for (Property property: r1) {
    				highestBidR1 = Math.max(highestBidR1, property.getHighestBid());
    			}
    			int highestBidR2 = 0;
    			for (Property property: r2) {
    				highestBidR1 = Math.max(highestBidR2, property.getHighestBid());
    			}
    			return highestBidR2-highestBidR1;
    		};
    	}
    	
    	public static void main(String[] args) {
    		// This may not yield the correct results if Property/BusinessProperty/Realtor has not been correctly implemented
    		Realtor realtor = new Realtor("test1", 10);
    		Realtor realtor2 = new Realtor("test2", 10);
    		Property p = new BusinessProperty("name", 1500);
    		p.bidReceived("BIDDER", 2000);
    		Property p2 = new BusinessProperty("name2", 1000);
    		p2.bidReceived("BIDDER", 1500);
    		realtor.addProperty(p);
    		realtor2.addProperty(p2);
    		List<Realtor> realtors=  Arrays.asList(realtor2, realtor);
    		System.out.println(realtors);
    		Collections.sort(realtors, sortRealtorsByHighestBidReceived());
    		// A more useful to string method or using the debugger might be helpful here
    		System.out.println(realtors);
    		
    	}
    
    }
    



...