Java Help....

MillerMan

Newcomer
Alright,

I view the forums alot but dont post much. Well since you a bord full of devlopers I just wondered if I can pick your brains?

I got UNI test comign up soon and im doing a bit of programing for revision. But im stuck on a bit of a program that im doing for revsion. What I have to do is:

Add code that will find the valid investment with the highest balance after 5 years and an initial deposit of £50
and display details of this investment. You should also display the names of investments that are not available
for this term or deposit. If your program is working correctly, you should expect to see output like the following:
Cannot invest in Club Saver
Cannot invest in LT Saver

Best Investment is Super Sprinter
balance after 5 years would be £59.67

Well I just cannot figure it out!

This is my code below... I would be greatful if you help me out. BTW its JAVA

Code:
/**
 * A investment calculator
 * @author Christopher James Cartlidge
 * @version 0.9 (15/4/2006)
 */
public class Investment
{
  private String name;
  private  double interestRate;
  private int minYears;
  private double minDeposit;
  /**
   * Constructor For Investment
   * @param theName is the name of the investment
   * @param rate is the interest rate of the investment
   * @param years is the time period of the investment
   * @param depoist is the initial capital of the investment
   */
  public Investment(String theName, double rate, int years, double depoist) throws IllegalArgumentException
  {
    if(depoist <1)
    {
       throw new IllegalArgumentException("The Minimum Deposit Has To Be £1 Or Greater!");
    }
    
    if(years <0)
    {
       throw new IllegalArgumentException("The Inerest Rate Cannot Be Negative!");
    }
    
    name = theName;
    interestRate = rate;
    minYears = years;
    minDeposit = depoist;
  }
  
  /**
   * This method gets the name veriable
   * @return name of the person
   */
  public String getName()
  {
    return (name);
  }
  /**
   * This method gets the fixed intrest rate
   * @return fixed intrest rate
   */
  public double getRate()
  {
    return (interestRate);
  }
  
  /**
   * This method gets the minimum amount of years allowed
   *@return the minimum amount of years is returned
   */
  public int getMinYears()
  {
    return (minYears);
  } 
  /**
   * This method gets the minimum depoist allowed
   *@return the minimum depoist that is returned
   */
  public double getMinDeposit()
  {
    return (minDeposit);
  }
  
  /**
   * This method creates a string representaion of investerment
   * @return investerment as a string representation is returned
   */
  public String toString()
  {
    String investment = "Ivestement Name: "+getName()+"\nInvestment Period: "+getMinYears()+", Interest Rate: "+getRate()+"%"+"\nMinimum Deposit: "+getMinDeposit()+"\n";
    
    return (investment);
  }
  
  /**
   * This method calculates the value of an investment after a number of years
   * @param initialInvestment the start off investment that is allocated
   * @param numOfYears is the number of years the investment will take palce
   * @return is the value of the investment after a number of years
   */
  public double calCrowth(double initialInvestment, int numOfYears) throws IllegalArgumentException
  {
    if((initialInvestment < 0) ^ (numOfYears <0) )
    {
       throw new IllegalArgumentException("The Initial Ivestement And/Or Number Of Years Is Less Negative!");
    }
    
    else if((initialInvestment == 0) ^ (numOfYears == 0))
    {
       throw new IllegalArgumentException("The Initial Investement And/Or Number Of Years Is Equal To Zero!");
    }
    
    else if((initialInvestment < minDeposit) ^ (numOfYears < minYears)) 
    {
       throw new IllegalArgumentException("The Initial Ivestement Is Less Than The Requirment Amount And/Or\n The Number Of Years Is Less Than The Required Amount!");
    }
    
    double interest = initialInvestment * interestRate / 100; 
    
    return (interest);
  }
  
  /**
   * This method set the new interest rate
   * @param newRate is the new interest rate
   */
  public void setRate(double newRate) throws IllegalArgumentException
  {
    if(newRate <0)
    {
      throw new IllegalArgumentException("The Interest Rate Is Negative!");
    }
    
    interestRate = newRate;
  }
}

Main method below...

Code:
public class Investor
{
  public static void main(String[] args)
  {
    Investment clubSaver = new Investment("Club Saver",3.9,5,100); 
    Investment lTSaver = new Investment("LT Saver",5.0,10,1000);
    Investment sprintSaver = new Investment("Sprint Saver",3.5,1,10);
    Investment superSprinter = new Investment("Super Sprinter",3.6,2,20);
    
    System.out.println(clubSaver.toString());
    System.out.println(lTSaver.toString());
    System.out.println(sprintSaver.toString());
    System.out.println(superSprinter.toString());
    
    
    clubSaver.calCrowth(50.0,5);
    lTSaver.calCrowth(50.0,5);
    sprintSaver.calCrowth(50.0,5);
    superSprinter.calCrowth(50.0,2);
    
    
    
    if((clubSaver.calCrowth(50.0,5)  > lTSaver.calCrowth(50.0,5)))
    {
      clubSaver.calCrowth(50.0,5)
    }
    else
    {
      
    }
    
    
    
  }
}
 
if((clubSaver.calCrowth(50.0,5) > lTSaver.calCrowth(50.0,5)))
{
clubSaver.calCrowth(50.0,5)
}
else
{

}
You are missing a semi colon after clubSaver.calCrowth(50.0,5) (might have been a typo)

For starters the numbers you are using will always throw an exception because you have it set up that way. Your method Investment assigns your deposit to minDeposit.

You then set up your exceptions so that you will throw an exception

else if((initialInvestment < minDeposit) ^ (numOfYears < minYears))
{
throw new IllegalArgumentException("The Initial Ivestement Is Less Than The Requirment Amount And/Or\n The Number Of Years Is Less Than The Required Amount!");
}

if initialInvestment is less than minDeposit. In your Main you then go ahead and use numbers that are guaranteed to make that else if a true statement.
ie clubSaver.calCrowth(50.0,5)

I am not sure if this is what is holding you back or not, but just looking at your program, it will compile (once u fix semi colon) and will always throw an exception until you use different numbers in your main.
 
Yeah it will throw an exception for the first 2 is there anyway of it carring on the program after the expection had been caught ?

Thanks BTW
 
right ive got this so far....

Code:
public class Investor
{
  public static void main(String[] args)
  {
    Investment clubSaver = new Investment("Club Saver",3.9,5,100); 
    Investment lTSaver = new Investment("LT Saver",5.0,10,1000);
    Investment sprintSaver = new Investment("Sprint Saver",3.5,1,10);
    Investment superSprinter = new Investment("Super Sprinter",3.6,2,20);
    
    System.out.println(clubSaver.toString());
    System.out.println(lTSaver.toString());
    System.out.println(sprintSaver.toString());
    System.out.println(superSprinter.toString());
    
    try
    {
    clubSaver.calCrowth(50.0,5);
    lTSaver.calCrowth(50.0,5);
    sprintSaver.calCrowth(50.0,5);
    superSprinter.calCrowth(50.0,2);
    }
    
    
    catch(IllegalArgumentException errorStatment)
    {
      System.out.println("You Cannot Invest In: ");
    }
  
  }
}

Doen a try and catch block but i still cant figure out how to find the best investment!
 
Add a method to investment to calculate the interest it generates given a number of years. Something like public double getInterest(int years){ calculate interest in here}. Then just pick up the best one.
 
Back
Top