Question
I'm trying to make all the output numbers into two decimal places, since it's referencing money, how can I do this? I posted this question
I'm trying to make all the output numbers into two decimal places, since it's referencing money, how can I do this?
I posted this question earlier, but I left out info and realized my mistake, sorry for the confusion.
//first class
public class BankAccount
{
//fields
private int ID;
private String name;
private double Saving;
private static int IDCOUNTER;
public BankAccount()
{
IDCOUNTER += 1;
ID = IDCOUNTER;
}
public BankAccount(String name)
{
IDCOUNTER += 1;
ID = IDCOUNTER;
this.name = name;
}
public BankAccount(String name, double Saving)
{
IDCOUNTER += 1;
ID = IDCOUNTER;
this.name = name;
this.Saving = Saving;
}
//Accessors
public String getName()
{
return name;
}
public int getID()
{
return ID;
}
public double getSaving()
{
return Saving;
}
//Modifiers
public void ChangeName()
{
this.name = name;
}
//toString
public String toString()
{
return "The name of this account is: " +this.name + " The ID of this account is: " + this.ID + " The Saving of this account is: " + this.Saving;
}
//Functions
public void Deposit(double M)
{
this.Saving += M;
}
public void withdraw(double M)
{
if(this.Saving >= M)
{
this.Saving -= M;
}
else System.out.println("This account " +ID+ " does not have enough money");
}
public void TransferTo(BankAccount B, double M)
{
if(this.Saving >= M)
{
B.Deposit(M);
withdraw(M);
}
else System.out.println("This Account " +ID+ " does not have enough money");
}
}
//This is the next separate class
import java.util.Random;
public class BankAccount_Client
{
public static void main(String[] args)
{
//Random
Random rd = new Random();
//Set up of bank ACCOUNTS
BankAccount A = new BankAccount("A", rd.nextDouble()*500);
BankAccount B = new BankAccount("B", rd.nextDouble()*500);
BankAccount C = new BankAccount("C", rd.nextDouble()*500);
System.out.println(A);
System.out.println(B);
System.out.println(C);
System.out.println();
//deposit 100 to A
A.Deposit(100);
System.out.println(A);
System.out.println();
//withdraw 1000 from B
B.withdraw(1000);
System.out.println(B);
System.out.println();
//Transfer 100 from A to C
A.TransferTo(C,100);
System.out.println(A);
System.out.println(C);
System.out.println();
//Transfer 1000 from A to B
A.TransferTo(B,1000);
System.out.println(A);
System.out.println(B);
}
}
Output:
The name of this account is: A
The ID of this account is: 1
The Saving of this account is: 376.7775344970733
The name of this account is: B
The ID of this account is: 2
The Saving of this account is: 77.58661904378833
The name of this account is: C
The ID of this account is: 3
The Saving of this account is: 316.29872741052395
The name of this account is: A
The ID of this account is: 1
The Saving of this account is: 476.7775344970733
This account 2 does not have enough money
The name of this account is: B
The ID of this account is: 2
The Saving of this account is: 77.58661904378833
The name of this account is: A
The ID of this account is: 1
The Saving of this account is: 376.7775344970733
The name of this account is: C
The ID of this account is: 3
The Saving of this account is: 416.29872741052395
This Account 1 does not have enough money
The name of this account is: A
The ID of this account is: 1
The Saving of this account is: 376.7775344970733
The name of this account is: B
The ID of this account is: 2
The Saving of this account is: 77.58661904378833
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started