1 Replies - 162 Views - Last Post: 13 September 2012 - 01:46 PM Rate Topic: -----

#1 spacelady  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-April 12

problem using collections an for each and iterator construct

Posted 13 September 2012 - 01:41 PM

HI I have a class the insurancepolicy class which I have to complete using the specofications below. I already started working on the methods which I have to fill in on the template but I am not sure if I am on the right track. I have to make use of a set oh which I have chosen the hashset implementation an I have to make use of a for each loop an iterator.

Complete the InsurancePolicy class to represent an insurance policy. This class needs to have a
Set which will contain the particular CoverType objects that represent the insurance cover
desired by the customer. Methods you need to write/complete are:
· includeCover ( CoverType ) – which should note the specified type of insurance cover as
being included in this insurance policy.
· getPolicyCost – to return the total cost of the policy which is $100 plus the sum of all the
included types of insurance cover.
· lodgeAnotherClaim (CoverType) – which should increase a counter that keeps track of how
many times the person makes a claim against this policy. The parameter specifies which
type of cover the claim is being lodged for. If the policy does not include that type of cover,
then the counter is not increased.
· toString – which should display the policy number, and list the types of cover included in
the policy, as well as the total number of claims made so far. You should make use of an
Iterator to show all the types of cover.

Here are the two classes first the covertype class which I already completed an the insurancepolicy class which will make use of the covertype class

import java.util.*;

// Class:   CoverType
// Purpose: Represents a type of insurance cover that can be included on a policy.

// YOU SHOULD NOT NEED TO MODIFY THIS CLASS.

public class CoverType
{
	private String name;		// The name of this type of insurance cover
	private int price;			// The price of this type of insurance cover
	
	// a universal (i.e. not instance specific) attribute for keeping all known CoverTypes.
	private static ArrayList<CoverType> allTypes = null;
	
	// Constructor - deliberately not public.
	protected CoverType(String name, int price)
	{
		this.name = name;
		this.price = price;
	}
	
	public String getName()
	{
		return name;
	}
	
	public int getPrice()
	{
		return price;
	}
	
	
	// static Method:  getCoverTypesList
	// Purpose:  It returns a List containing all the CoverTypes that are available in this program.
	public static List<CoverType> getCoverTypesList()
	{
		if (allTypes == null)		// Means that this method has not previously been called
		{
			// So we need to set up the system to contain the following hard-coded cover types
			allTypes = new ArrayList<CoverType>();
			
			allTypes.add( new CoverType("Fire",250) );		// Protection against fire
			allTypes.add( new CoverType("Flood",175) );		// Protection against flood
			allTypes.add( new CoverType("Storm Damage",160) );	// etc.
			allTypes.add( new CoverType("Theft",420) );	
		}
		
		// give back an ArrayList that contains the above cover types...
		return (List<CoverType>) allTypes.clone();
	}
}



an here is the insurance policy class the methods in which I have done work according to the task specifications are marked with "student must complete".




// Class:   InsurancePolicy
// Purpose: To represent a particular customer's insurance agreement
import java.util.Set;
import java.util.HashSet;
import java.util.*;
public class InsurancePolicy {
    
    // Each instance will have a unique policy number.
    private int policyNumber;
    
    private static int nextPolicy = 1;
    
    Set<CoverType> myCovers;
    
    
    public InsurancePolicy()
    {
        this.policyNumber = nextPolicy;
        nextPolicy++;  
        myCovers = new HashSet<CoverType>();
    }
    
    // Determine this policy's number
    public int getPolicyNumber()
    {
        return policyNumber;
    }
    
    // Method:  getPolicyCost
    // Purpose: to report to the caller, the total cost of this policy, based on the covers
    //          that are selected
    public int getPolicyCost()
    {
        // Student must replace the following...
        //myCovers = new HashSet<CoverType>;
        //ArrayList<CoverType> allTypes = new ArrayList<CoverType>();
        int totalCost = 0;
        for (CoverType cover : myCovers) {
        totalCost += cover.getPrice()+100; 
         }
        return totalCost;
        
   }
    // Method:  includeCover
    // Purpose: To allow the caller to specify a type of insurance cover to be included for this policy.
    // Returns: True if the cover is now included; if it was already included or was unable to
    //          be included this time, false is returned.
    public boolean includeCover(CoverType which)
    {
        // Student must replace this following:
        boolean included = true;
        for (CoverType which1 :myCovers )
            if (!myCovers.add(which)){
                included = true;
                System.out.println("Cover added: " + which1);
            }
            else {
                System.out.println("Cover already included");
                included = false;
            }
        return included;
    }
    
    // Method:  lodgeAnotherClaim
    // Purpose: To increase the counter of how many claims have been made against this policy.
    // Parameter: lodgedType - specifies the type of cover being claimed. But only the types which
    //            have been included so far, will actually be countable.
    public void lodgeAnotherClaim(CoverType lodgedType)
    {
        // Student must complete
        //myCovers.contains(CoverType lodgeType);
        int count = 0;
        for (CoverType lodgeType1: myCovers){
            if (lodgeType1.getName().eqaulsIgnoreCase(lodgedType))
                System.out.println("Lodgetype does not exist");
            else {
                System.out.println("Claim counted");
             count++;
            }  
        } 
    }
    
    // Method:  toString
    // Purpose: Display a textual summary of this object's state
    @Override public String toString()
    {
        // Student must complete
        String retVal = "";
        retVal = "Policy number is :" + policyNumber;
        Iterator<CoverType> myCoversIterator = myCovers.iterator();   
        while (myCoversIterator.hasNext()) {
         String temp = myCoversIterator.next();
         System.out.println(temp);
       }            
        return "Something";
    }
}



in the toString method how do get the iterator to display all the covers
in the lodgeAnotherClaim method how do i check the set if the cover already exist in order to increase the counter.
in the includedCover method i coded to check whether the cover is included or not an I just want to make sure that it will work.


Is This A Good Question/Topic? 0
  • +

Replies To: problem using collections an for each and iterator construct

#2 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: problem using collections an for each and iterator construct

Posted 13 September 2012 - 01:46 PM

is more efficient with a StringBuffer but as you have just a few elements you can afford to recreate a new String at each iteration

String retval = "";
while (myCoversIterator.hasNext())   
          retval = retval + " " +  myCoversIterator.next();  

return retval;  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1