Updating variable

Hi!!
I am unable to update one of the variable called “wallet” in the following program.
Kindly help me with it.

public class Myshop {

String toyType;
double cost;
boolean iscurrent;
int minAge;
double wallet = 4500;
boolean buy = wallet >= cost;


public static void greetCustomer(String name){
	System.out.println("Welcome to my Toy shop " + name + "!");
}

public double tax(){
	return cost + cost * 0.5;
}

public void description(){
	System.out.println("Toy Type: " + toyType + "\nPrice : Rs " + tax() + " (tax inc.) " + "\nElectric: " + iscurrent + "\nMinimal Age: " + minAge + "\n");
}

public Myshop(String toy, double price, boolean iselectric, int age ){
    toyType = toy;
    cost = price;
    iscurrent = iselectric;
    minAge = age;
    
}

public void bought(){
	System.out.println("You bought " + toyType + " of Price Rs " + tax() + " (tax inc.)"); 
}

public void walletStatus(){
	System.out.println("\nMoney in wallet: Rs " + wallet + "\nCost of Toy bought: Rs " + tax());
	double updatedWallet = wallet - cost;
	System.out.println("Money left after purchase: Rs " + updatedWallet);
        wallet = updatedWallet;
    
}

public void canBuy(){
    System.out.println("\nDo you have enough money to buy " + toyType + ": " + buy);
}


public static void main(String[] args){
	
	Myshop doll = new Myshop("Animal Doll", 2000, false, 5);
	Myshop car = new Myshop("Toy Car", 1500, true, 8);
	Myshop ball = new Myshop("Football", 2000, false, 5);
	
	greetCustomer("Aniket");

	ball.walletStatus();

	car.walletStatus();
	
    
}

}

Hey there!

public double tax(){
  return cost + cost * 0.5;
}

public void walletStatus(){
  System.out.println("\nMoney in wallet: Rs " + wallet + "\nCost of Toy bought: Rs " + tax());
  double updatedWallet = wallet - cost;
  System.out.println("Money left after purchase: Rs " + updatedWallet);
  wallet = updatedWallet;
    
}
  • You are printing the value of wallet and the price derived from tax()

System.out.println("\nMoney in wallet: Rs " + wallet + "\nCost of Toy bought: Rs " + tax());

  • Afterwards, you declare a new variable for your updated value of the wallet but you use cost to subtract money away from wallet, which doesn’t seem to reflect the value of tax().

I think to clarify, it should print the same values that are being changed (so if you’re going to subtract cost, print cost, if you’re going to subtract tax(), print tax()).


The other thing I would say is that your tax seems unrealistically high (50% tax). I think you might be missing a 0 (to make it 5% tax).

Thanks for your guidance.

1 Like