Click here to Skip to main content
15,885,143 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It is for fix withdrawl but my test cases are failing.

What I have tried:

Java
import java.util.*;
	public class save
	{
	public static void main(String args[]) 
		{
		double Money = 0, principle_amount, Interest_rate = 0, Amount, n;
		int Time = 0, Actual_amount_need;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter desired cashflow: ");
		System.out.println("Enter time (in months): ");
		System.out.println("Enter rate: ");
		Money = sc.nextDouble();
		Time = sc.nextInt();
		Interest_rate = sc.nextDouble();
		try 
			{
			if (Money > 0 && Time > 0 && Interest_rate >= 0)
				{
				if (Interest_rate != 0) 
					{
					Interest_rate = Interest_rate / 100;
					principle_amount = Money * ((1 - Math.pow((1+(Interest_rate/12)),(-Time)))/(Interest_rate / 12));
					} 
				else 
					{
					Amount = Money * Time;
					principle_amount = Amount;
					}
				Actual_amount_need = (int)(principle_amount + 0.5);
				System.out.println("Money to be deposited now: " + Actual_amount_need);
				return;
				} 
				else 
				{
				System.out.println("Error");
				}
			}
			catch (Exception e) 
			{
			System.out.println(e);
			System.out.println("not Amount valid input");
			}
		}
	}
Posted
Updated 23-Aug-16 10:59am
v4
Comments
Richard MacCutchan 23-Jul-16 3:21am    
What is failing? What results are you getting and why are they wrong? It would help if you used meaningful names for your variables rather than single letters. You could also add some comments to indicate exactly what each part of the code is supposed to do.
[no name] 23-Jul-16 4:43am    
I have tried commenting too , I used M for money needed per month,R for rate of interest, T for time, p for principal amount , P integer round of p , A for total amount without interest.
All this for a normal continuous withdrawal of exact equal amount of money M from bank.....
Happy that someone helps.
Patrice T 23-Jul-16 3:33am    
Show test cases with expected results and actual ones.
[no name] 23-Jul-16 4:45am    
I have tried commenting too , I used M for money needed per month,R for rate of interest, T for time, p for principal amount , P integer round of p , A for total amount without interest.
All this for a normal continuous withdrawal of exact equal amount of money M from bank.....
Happy that someone helps.

Server is expecting the results , I myself failed to understand that what is wrong, and which test cases are failing.
Richard MacCutchan 23-Jul-16 4:51am    
"I myself failed to understand that what is wrong, and which test cases are failing."

Well you cannot expect us to know which test cases are failing.

And, as I suggested above, please Please use proper names for variables and add some comments to your code. Use the Improve question link above to edit your question. As it stands it is not easy to understand what the code is supposed to do.

Assume you want an income from the interest on your deposit of 1000.00 per month.

"Enter desired cashflow" (this will be 1000)
"Time" (this will be 12)
"Interest rate" (this will be 6)
Your answer will be 200,000.00 deposit required.

You will notice the changes made which simplified the code in your calculation.
Also I placed the try before your data entry point in order to catch the input errors.

This is a nice little program and can form the basis for a lot of experimentation.

I hope I have interpreted your intentions correctly.
 
Share this answer
 
Here is the code. I had a problem putting it here so I hope it is still ok.

Java


Quote:

import java.util.*;
public class Save {

public static void main(String[] args) {
// TODO Auto-generated method stub
double Money = 0.00, principle_amount=0.00, Interest_rate = 0.00;
int Time = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter desired cashflow: ");
System.out.println("Enter time (in months): ");
System.out.println("Enter rate: ");
try
{
Money = sc.nextDouble();
Time = sc.nextInt();
Interest_rate = sc.nextDouble();
sc.close();
if (Money > 0 && Time > 0 && Interest_rate >= 0)
{
if (Interest_rate != 0)
{
Interest_rate = Interest_rate / 100;
principle_amount = (Money * Time)/Interest_rate;
}
else
{
principle_amount = Money * Time;
}

System.out.println("Money to be deposited now: " + principle_amount);
return;
}
else
{
System.out.println("Error");
}
}
catch (Exception e)
{
System.out.println(e);
System.out.println("not Amount valid input");
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900