303.31.. Driver to exercise the use of multiple Account objects.
Opis programa: Ključna riječ new se koristi da alocira (dodijeli) memoriju u vremenu izvršavanja.
Listing programa:
// file: Account.java
package BankAccount;
// Represents a bank account with basic services such as deposit and withdraw
public class Account
{
private final double RATE = 0.035; // interest rate of 3.5%
private int acctNumber;
private double balance;
private String name;
// Sets up the account by defining its owner, account number, and initial balance
public Account (String owner, int account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
// Validates the transaction, then deposits the specified amount into the account.
// Returns the new balance.
public double deposit (double amount)
{
if (amount < 0) //deposit value is negative
{
System.out.println();
System.out.println("Error: Deposit amount is invalid.");
System.out.println(acctNumber + " " + amount);
}
else
balance = balance + amount;
return balance;
}
// Validates the transaction, then withdraws the specified amount from the account.
// Returns the new balance.
public double withdraw (double amount, double fee)
{
amount = amount + fee;
if (amount < 0) // withdraw value is negative
{
System.out.println();
System.out.println("Error: Withdraw amount is invalid.");
System.out.println("Account: " + acctNumber);
System.out.println("Requested: " + amount);
}
else
if (amount > balance) // withdraw value exceeds balance
{
System.out.println();
System.out.println("Error: Insuficient funds.");
System.out.println("Account: " + acctNumber);
System.out.println("Requested: " + amount);
System.out.println("Available: " + balance);
}
else
balance = balance - amount;
return balance;
}
// Adds interest to the account and returns the new balance.
public double addInterest()
{
balance = balance + (balance * RATE);
return balance;
}
// Returns the current balance of the account.
public double getBalance()
{
return balance;
}
// Returns the account number.
public int getAccountNumber()
{
return acctNumber;
}
// Returns a one-line description of the account as a string.
public String toString()
{
return (acctNumber + "\t" + name + "\t" + balance);
}
}
/////////////////////////////////////////////////////////////////
// file: SvePlanete2_4.java
package BankAccount;
// Driver to exercise the use of multiple Account objects.
public class Banking
{
// Creates some bank accounts and requests various services.
public static void main(String[] args)
{
Account acct1 = new Account ("Peter A.", 72354, 102.56);
Account acct2 = new Account ("Jane B.", 69713, 40.00);
Account acct3 = new Account ("Mark C.", 93757, 759.32);
acct1.deposit(25.85);
double janeBalance = acct2.deposit(500.00);
System.out.println("Jane B. balance after deposit: " + janeBalance);
janeBalance = acct2.withdraw(430.75, 1.50);
System.out.println("Jane B. balance after withdrawal: " + janeBalance);
acct3.withdraw(800.00, 0.0); // exceeds balance
acct1.addInterest();
acct2.addInterest();
acct3.addInterest();
System.out.println();
System.out.println(acct1);
System.out.println(acct2);
System.out.println(acct3);
}
}
Ispis na ekranu:
Jane B. balance after deposit: 540.0
Jane B. balance after withdrawal: 107.75
Error: Insuficient funds.
Account: 93757
Requested: 800.0
Available: 759.32
72354 Peter A. 132.90435
69713 Jane B. 111.52125
93757 Mark C. 785.8962
Index
|
 |