Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

  //*******************************************************
// FlexibleAccount.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************
import java.util.Random;
public class FlexibleAccount
{
  private double balance;
  private String name;
  private long acctNum;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public FlexibleAccount(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }

  public FlexibleAccount(double initBal, String owner, double number)
  {
    Random generator = new Random();
     balance = initBal;
    name = owner;
     number=generator.nextDouble();
     this.acctNum= number;
    }

    public FlexibleAccount(String owner)
    {
     balance = 0;
     name=owner;
     Random generator = new Random();
     number=generator.nextDouble();
     this.acctNum= number;
     }
  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");
  }

  public void withdraw(double amount,int fee)

  {
   if(balance>=amount)
    balance-= amount+fee;
    else
        System.out.println("Insufficient funds");
    }   

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }


  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
    return "Name: " + name + 
"
Account Number: " + acctNum +
"
Balance: " + balance; 
  }
}

This is what i have and I'm trying to overload the FlexibleAccount 3 times as follow

  1. public FlexibleAccount (double initBal, String owner, long number) – initializes the balance, owner, and account number as specified
  2. public FlexibleAccount (double initBal, String owner) – initializes the balance and owner as specified; randomly generates the account number.
  3. public FlexibleAccount (String owner) – initializes the owner as specified; sets the initial balance to 0 and randomly generates the account number.

When I compiled i get these error

 FlexibleAccount.java:31: possible loss of precision
    found   : double
    required: long
         this.acctNum= number;
                       ^
    FlexibleAccount.java:39: cannot find symbol
    symbol  : variable number
    location: class FlexibleAccount
         number=generator.nextDouble();
         ^
    FlexibleAccount.java:40: cannot find symbol
    symbol  : variable number
    location: class FlexibleAccount
         this.acctNum= number;
                   ^

How do I fix this and is this the right way to overload?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
230 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just like previous question - there is no such variable number when you write acctNum = number; in public FlexibleAccount(String owner):

public FlexibleAccount(String owner)
    {
     balance = 0;
     name=owner;
     Random generator = new Random();
     number=generator.nextDouble();        << number is not declared
     this.acctNum= number;
     }

EDIT: This is your 2nd constructor:

 public FlexibleAccount(double initBal, String owner, double number)
  {
    Random generator = new Random();
     balance = initBal;
    name = owner;
     number=generator.nextDouble();
     this.acctNum= number;
    }

For some reason you declare it with 3 arguments although you wrote you want it to be receive only 2 arguments -

public FlexibleAccount (double initBal, String owner) – initializes the balance and owner as specified; randomly generates the account number.

I guess you wanted to have the variable number... it should be more something like that:

 public FlexibleAccount(double initBal, String owner)
      {
        Random generator = new Random();
         balance = initBal;
        name = owner;
         this.acctNum= generator.nextLong();
        }

Now as you said in the prev. question this is a homework, so I won't add to that. Read the answers and comments you got until now and work it up.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...