Commit 3f9372a7 authored by ElaFHNW's avatar ElaFHNW
Browse files

Testing exercise

parent 966c601d
package Testing;
import java.text.NumberFormat;
/**
* Account is a bank account with basic services for deposit,
* withdrawal, and interest.
*/
public class Account
{
private NumberFormat fmt = NumberFormat.getCurrencyInstance();
private final float kInterestRate = 0.045f; // interest rate of 4.5%
private long acctNumber;
private float balance;
public final String name;
/**
* Sets up the account by defining its owner, account number,
* and initial balance.
* @param owner : name of account holder
* @param account : the account number
* @param initial : the initial amount of money in the account.
*/
public Account(String owner, long account, float initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
/**
* Deposit the amount into the account.
* @param amount value to be added to the balance
* @return true if amount is non-negative, false if amount
* is negative; false indicates balance was not changed.
*/
public boolean deposit(float amount)
{
boolean result = true;
// is amount valid?
if (amount < 0)
{
result = false;
}
else
{
balance = balance + amount;
}
return result;
}
/**
* Withdraw the amount from the account.
* If amount is negative or fee is negative, or
* there is too little money to pay out amount and fee, abort.
* @param amount value to be deducted from the balance
* @param fee transaction fee debited from the account
* @return true if transaction was successful, false otherwise;
*/
public boolean withdraw(float amount, float fee)
{
// validate parameters
if (isValidWithdrawal(amount, fee))
{
balance = balance - (amount + fee);
return true;
}
else {
return false;
}
}
/* Determine if withdrawal parameters are valid */
public boolean isValidWithdrawal(float amount, float fee)
{
return amount >= 0 && fee >= 0 && (amount + fee) <= balance;
}
/**
* Adds interest to the account.
*/
public void addInterest()
{
balance += (balance * kInterestRate);
}
/**
* Accessor to the current balance of the account.
* @return the current balance of the account.
*/
public float getBalance()
{
return balance;
}
/**
* Accessor to the account number.
* @return the account number.
*/
public long getAccountNumber()
{
return acctNumber;
}
/**
* Returns a one-line description of the account as a string.
* @return formatted account information
*/
public String toString()
{
return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}
}
\ No newline at end of file
package Testing;
import java.text.*;
/******************************************************************
* BadMoney program
* This program demonstrates the inaccuracies that can occur from
* using a floating point variable to represent money.
* NO user input of data is required.
* @author J. Dalbey 2/7/2000
* Assignment: Examine the code and write down your prediction of the output.
* Then compile and execute the program to obtain the actual results.
* If your prediction was incorrect, research the cause and write up a
* detailed technical explanation for the observed results.
******************************************************************/
public class BadMoney
{
public static void main(String[] args)
{
// Example 1
float originalPrice = 400000.00f;
// to make the price more attractive, we'll reduce it to $399999.95
float reducedPrice = originalPrice - 0.05f;
float discount = originalPrice - reducedPrice;
// Display discount amount
System.out.println("Discount is: " + discount);
// Example 2
// The amount you have in the bank: $12,345,678.12
float principal = 12345678.12f;
int count; // count number of iterations
NumberFormat fmt = NumberFormat.getCurrencyInstance(); // for output formatting.
// Display the original amount
System.out.println( "The original principal is "
+ fmt.format(principal) + ".");
// Add a dime to the principal a thousand times
for (count = 1; count <= 1000; count = count + 1)
{
principal = principal + .10f;
}
// The result should be 100 dollars larger.
System.out.println( "The new principal is "
+ fmt.format(principal) + ".");
// Example 3
double FloatNumber = 4.35;
int IntegerNumber = (int) (100 * FloatNumber);
System.out.println("Computation result is " + IntegerNumber);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment