b
Tuesday, February 23, 2016
Account.java & ManageAccount.java
Account.java & ManageAccount.java
// *******************************************************
// Account.java
// *******************************************************
import java.text.NumberFormat;
public class Account
{
private double balance;
private String name;
private long acctNum;
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
}
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
public void deposit(double amount)
{
if (amount > 0)
balance += amount;
else
System.out.println("Amount should be positive.");
}
public double getBalance()
{
return balance;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (name + "\t" + acctNum + "\t" + fmt.format(balance));
}
public double chargeFee()
{
balance -= 10;
return balance;
}
public void changeName(String newName){
name = newName;
}
}
// ************************************************************
// ManageAccounts.java
// ************************************************************
public class ManageAccounts{
public static void main(String[] args)
{
Account acct1, acct2;
acct1 = new Account(1000, "Sally", 1111);
acct2 = new Account(500, "Joe", 2222);
acct2.deposit(100);
System.out.println("Balance: $" + acct2.getBalance() );
acct1.withdraw(50);
System.out.println("Sally's balance is " + acct1.getBalance() );
acct1.chargeFee();
acct2.chargeFee();
acct2.changeName("Joseph");
System.out.println(acct1.toString() );
System.out.println(acct2.toString() );
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment