I am supposed to Add the following method to allow BankAccounts to transfer money to another BankAccount. Where would I add this method to make sure it works for all BankAccounts, including SavingsAccount and CheckingAccount?
The method im supposed to use is transferTo(BankAccount destinationAccount, int transferAmount)
public class BankAccount {
private String accountHolderName;
private String accountNumber;
private int balance;
public BankAccount(String accountHolder, String accountNumber) {
this.accountHolderName = accountHolder;
this.accountNumber = accountNumber;
this.balance = 0;
}
public BankAccount(String accountHolder, String accountNumber, int balance) {
this.accountHolderName = accountHolder;
this.accountNumber = accountNumber;
this.balance = balance;
}
public String getAccountHolderName() {
return accountHolderName;
}
public String getAccountNumber() {
return accountNumber;
}
public int getBalance() {
return balance;
}
// Update the balance by using the DollarAmount.Plus method
public int deposit(int amountToDeposit) {
balance = balance + amountToDeposit;
return balance;
}
// Update the balance by using the DollarAmount.Minus method
public int withdraw(int amountToWithdraw) {
balance = balance - amountToWithdraw;
return balance;
}
public int transferTo(BankAccount destinationAccount, int transferAmount) {
return balance;
}
}
Assumptions:
This is only an exercise and not a real application for a bank.
SavingsAccount and CheckingAccount are subclasses of BankAccount
The method transferTo can be implemented like the following:
public int transferTo(BankAccount destinationAccount, int transferAmount) {
this.balance -= transferAmount;
destinationAccount.deposit(transferAmount);
return balance;
}
In a real world application you need to ensure that this operation will be always atomic and thread-safe. Additionally, using int for the balance is strongly not recommended.
Related
I am working on an inheritance bank account which has 5 classes.In one of the classes(InterestFreeDeposit) ,the account should be created with at least 10 $. How should I write the code of this part?
here is what I have done until now:
superClass:
import java.util.Scanner;
import java.io.FileReader;
public class Account {
private String owner;
private double balance;
private int accountNumber;
private double interestRate;
public Account( String owner,double balance, int accountNumber , double interestRate){
this.balance=balance;
this.owner=owner;
this.accountNumber=accountNumber;
this.interestRate=interestRate;
}
public void deposit(double amount) {
if(amount>0) {
this.balance+=amount;
}
}
public void withdraw(double amount) {
if(amount>0 && balance>=amount) {
this.balance-=amount;
}
}
public double getBalance() {
return balance;
}
public void setBalance(double amount) {
this.balance = amount;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public double getInterestRate() {
return interestRate;
}
public double setInterestRate(double interestRate) {
//System.out.println("Enter the period(month):");
return this.balance +=balance * interestRate/100;
}
subClass(InterestFrreClass):
public class InterestFreeDeposit extends Account {
public InterestFreeDeposit(String owner, double balance, int accountNumber, double interestRate) {
super(owner, balance, accountNumber, interestRate);
// TODO Auto-generated constructor stub
}
public void interest() {
super.setInterestRate(0.0) ;
}
}
You need to add the check when the value is set, which in this case happens in the constructor of InterestFreeDeposit. The question is how you want to react when the value is below 10. You could throw an IllegalArgumentException:
public InterestFreeDeposit(String owner, double balance, int accountNumber, double interestRate) {
super(owner, balance, accountNumber, interestRate);
if(balance < 10){
throw new IllegalArgumentException("balance must be at least 10");
}
}
The problem with this approach is that IllegalArgumentException is an unchecked exception, meaning that the caller of the constructor is not forced to deal with it if it is thrown.
You can use a ternary operator in your subclass constuctor to default to a 10.0 minimum value:
public class InterestFreeDeposit extends Account {
public InterestFreeDeposit(String owner, double balance, int accountNumber, double interestRate) {
super(owner, balance < 10.0 ? 10.0 : balance, accountNumber, interestRate);
}
}
A ternary operator can be seen as an inline if. So you have following structure:
condition ? condition is true : condition is false
In the above snippet it just checks if the provided balance is below the 10.0. if it is it just sets it to 10, else it is passed along:
balance < 10.0 ? 10.0 : balance
In this example, how do I initiate the object state on as soon as it's created?
public class Account {
private double balance;
public void credit(double amount){
balance = balance + amount;
}
public double getBalance(){
return balance;
}
}
Add a simple constructor, as follows:
public class Account {
private double balance;
public Account(double balance) {
this.balance = balance;
}
public void credit(double amount){
balance = balance + amount;
}
public double getBalance(){
return balance;
}
}
From the method that creates an Account instance (e.g., from your main method), you can simply call this constructor as follows:
Account account = new Account(100.5);
to initialize an Account instance with a balance value of 100.5.
This is one of the first things that you should know when learning to program in Java, so perhaps an introductory book to Java would be highly recommended. It is the fastest way to learn Java, IMHO.
I am new to java and I am facing a problem like this.
public class Account {
private String name;
private double balance;
private static int nextID;
int acctID;
Account(String n, double bal) {
init();
this.name = n;
this.balance = bal;
print();
}
void init() {
this.acctID = nextID++;
}
}
public class SavingsAccount extends Account {
private double interest;
public SavingsAccount(String n, double bal, double interest) {
super(n, bal);
this.interest = interest;
}
void init() {
}
}
The class SavingsAccount will use its init() and this is wrong. I know I can do this to solve the problem.
public class Account {
private String name;
private double balance;
private static int nextID;
int acctID;
Account(String n, double bal) {
init();
this.name = n;
this.balance = bal;
print();
}
{
this.acctID = nextID++;
}
}
I would like to ask if there any other way to solve this problem, for example force other class to use the Account.init() or don't allow others to make their only init()?
If you make the init() method final, then nobody can override it.
final void init() {
this.acctID = nextID++;
}
http://docs.oracle.com/javase/tutorial/java/IandI/final.html
Okay guys, im having trouble with these constructors for this code and some logic. I did most of it, just confused on how to finish this off.
Here is the main code i have. (it might be a little sloppy but its "good enough")
public class Accountdrv {
public static void main (String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated());
}
}
class Account {
private int id;
private double balance;
private double annualInterestRate;
private java.util.Date dateCreated;
public Account() {
dateCreated = new java.util.Date();
}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
dateCreated = new java.util.Date();
}
public int getId() {
return this.id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int id) {
this.id =id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}
Now, i wanted to create a Savings, and Checking account. I need help with what i need to add for constructors, i put in the comments the parts i know im missing but confused on how to do them.
Savings:
class Savings extends Account{
//need to create a constructor
public Savings(int id, double balance, double annualInterestRate) {
//need to invoke the constructor for Account
super(id, balance, annualInterestRate);
}
// need to override the withdraw method in Account
public void withdraw(double amount) {
// place logic to prevent the account from being overdrawn
// that is do not allow a negative balance
}
}
You just need to check id the amount < balance Or whatever logic you have (eg: you want
some threshold amount to be present in the account).
Also I would recommend to look into synchronized methods as you should synchronize access to account of a given user to ensure that when a withdraw function is invoked...another withdraw on the same user account has to wait...or else it will lead to problems...I will leave that to you to figure out.
public void withdraw(double amount) {
// place logic to prevent the account from being overdrawn
// that is do not allow a negative balance
if(balance < amount)
{
// print error message or do something
}
else
{
// withdraw the money
balance -= amount;
// print message or do something
}
}
super() invokes the constructor for the super-class, in the Savings case, Account.
So I'm working on this program and I've created two classes, one class called CreditCard, the other called CreditCardTester.I'm using Eclipse IDE and I keep getting compiling errors such as "The method getBalance(double) in the type CreditCard is not applicable for the arguments ()". I'm not really sure on what I have to fix.
This is the first class called CreditCard:
public class CreditCard
{
private String accountNumber;
private double creditLimit;
private double balance;
public void CreditCard(String number, double limit)
{
accountNumber = number;
limit = creditLimit;
balance = 0;
}
public String getAccountNumber()
{
return accountNumber;
}
public double getCreditLimit()
{
return creditLimit;
}
public double getBalance(double theBalance)
{
return balance;
}
public double charge(double amount)
{
balance = balance + amount;
return balance;
}
This is the second class CreditCardTester:
public class CreditCardTester
{
public static void main(String[] args)
{
CreditCard card = CreditCard("1234-5678-9012-3456", 1000.00);
String formatString = "Credit Card [number = %s, bal = %.2f, limit = %.2f]\n";
System.out.printf(formatString, card.getAccountNumber(), card.getBalance(), card.getCreditLimit());
System.out.println("\nCharging $50.00 to credit card...\n");
card.charge(50.00);
System.out.printf(formatString, card.getAccountNumber(), card.getBalance(), card.getCreditLimit());
}
This is not a constructor, because you added void, making it a normal method:
public void CreditCard(String number, double limit)
Remove void:
public CreditCard(String number, double limit)
Also, one of the assignments in the method/constructor is backwards. You assigned the instance variable to the parameter.
limit = creditLimit;
Change it around:
creditLimit = limit;
You're missing "new" when creating a CreditCard:
CreditCard card = CreditCard("1234-5678-9012-3456", 1000.00);
Try
CreditCard card = new CreditCard("1234-5678-9012-3456", 1000.00);
You have an unused parameter on the getBalance method, and you call it without a parameter.
public double getBalance(double theBalance)
Remove it:
public double getBalance()
I think getBalance should not accept an argument in your class definition.
Use this in your CreditCard class:
public double getBalance()
{
return balance;
}