Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

Lo6 Identify several management development methods.

Answered: 1 week ago

Question

LO4 List options for development needs analyses.

Answered: 1 week ago