lundi 15 février 2021

Put associated Classes Separately In Java

I am very new to Java. I have two classes User and Wallet. If I put them in a single file, everything works fine. But I want both of them to be in separate-separate files like Wallet.java & User.java

And obviously, if I put them in different files then the User.java file will throw an error because it does not know what Wallet is.

What is the best way to do this?? Please let me know if my question is not clear.

public class Wallet{
    float winningAmount;
    float bonusAmount;
    float depositAmount;
    public Wallet(float winningAmount, float bonusAmount, float depositAmount){
      this.winningAmount = winningAmount;
      this.bonusAmount = bonusAmount;
      this.depositAmount = depositAmount;
    }
    public void debitBonus(float amountToDebit){
      this.bonusAmount = this.bonusAmount - amountToDebit;
    }
    public void debitDeposit(float amountToDebit){
      this.depositAmount = this.depositAmount - amountToDebit;
    }
    public void debitWinnings(float amountToDebit){
      this.winningAmount = this.winningAmount - amountToDebit;
    }
   }
  
public class User{
    int id;
    String name;
    String email;
    String mobileNumber;
    boolean isVerified;
    Wallet wallet;
    User(int id, String name, String email, String mobileNumber,Wallet wallet){
      this.id = id;
      this.name = name;
      this.email = email;
      this.mobileNumber = mobileNumber;
      this.isVerified = false;
      this.wallet = wallet;
    }
  }

Aucun commentaire:

Enregistrer un commentaire