Home > database >  How can I update the eggs at cash while I create class in java?
How can I update the eggs at cash while I create class in java?

Time:04-09

I am a beginner at Java and we were given an activity to create an egg trader where we start with 0 eggs and 500 cash, we should be able to update the eggs and cash after buying and after selling. However, the eggs and cash do not update when I buy eggs so the same goes for when I buy eggs.

I have 2 classes, 1 is named EggTrader and 1 is the EggTraderDriver

Here are the conditions1

EggTrader code

public class EggTrader{
    double cash;
    int eggs;
    public EggTrader(){
        cash = 500;
        eggs = 0;
    }
    public void buyOneDozen(double pricePerDozen){
        cash = cash - pricePerDozen;
    }
    
    public void sellEggs(int numEggs, double unitPrice){
        eggs = eggs - numEggs;
        cash = cash   unitPrice;
    }
    
    public int getEggCount(){
        return eggs;
    }
    public double getCashOnHand() {
        return cash;
    }
}

EggTraderDriver

public class EggTraderDriver{
    public static void main (String [] args){
        
        EggTrader eggs = new EggTrader();
        EggTrader cash = new EggTrader();
        
        //cash.deposit(500);
        //current balance
        System.out.println("Current eggs: "   eggs.getEggCount());
        System.out.println("Current cash: "   cash.getCashOnHand());
        
        //buy eggs
        eggs.buyOneDozen(24);
        System.out.println("\nEggs bought: "   eggs.getEggCount());
        System.out.println("Cash left: "   cash.getCashOnHand());
      
        //sells eggs
        eggs.sellEggs(12,30);
        System.out.println("\nEggs sold: "   eggs.getEggCount());
        System.out.println("Cash earned: "   cash.getCashOnHand());
    }
    
}

and this is the output

this is the output2

CodePudding user response:

You are creating two EggTrader objects for cash and eggs respectively. In EggTraderDriver you are performing buyOneDozen() and sellEggs() on the 'eggs' object, as a result while trying to access cash value from the 'cash' object the variables have not been updated for that object. Create only 1 object as you have rightly initialized the member variables in the constructor of EggTrader.

EggTrader trader = new EggTrader();
        
//cash.deposit(500);
//current balance
System.out.println("Current eggs: "   trader.getEggCount());
System.out.println("Current cash: "   trader.getCashOnHand());
        
//buy eggs
trader.buyOneDozen(24);
System.out.println("\nEggs bought: "   trader.getEggCount());
System.out.println("Cash left: "   trader.getCashOnHand());
      
//sells eggs
trader.sellEggs(12,30);
System.out.println("\nEggs sold: "   trader.getEggCount());
System.out.println("Cash earned: "   trader.getCashOnHand()); 

Output

Current eggs: 0
Current cash: 500.0

Eggs bought: 0
Cash left: 476.0

Eggs sold: -12
Cash earned: 506.0

CodePudding user response:

In the below code, I added method main to class EggTrader instead of using class EggTraderDriver. (Notes after the code.)

public class EggTrader {
    public static final int DOZEN = 12;

    private double cash;
    private int eggs;

    public EggTrader() {
        cash = 500;
        eggs = 0;
    }

    public void buyOneDozen(double pricePerDozen) {
        cash -= pricePerDozen;
        eggs  = DOZEN;
    }

    public void sellEggs(int numEggs, double unitPrice) {
        eggs = eggs - numEggs;
        cash  = numEggs * unitPrice;
    }

    public int getEggCount() {
        return eggs;
    }

    public double getCashOnHand() {
        return cash;
    }

    public static void main(String[] args) {
        EggTrader eggTrader = new EggTrader();

        // cash.deposit(500);
        // current balance
        System.out.println("Current eggs: "   eggTrader.getEggCount());
        System.out.println("Current cash: "   eggTrader.getCashOnHand());

        // buy eggs
        eggTrader.buyOneDozen(24);
        System.out.println("\nTotal eggs: "   eggTrader.getEggCount());
        System.out.println("Cash left: "   eggTrader.getCashOnHand());

        // sells eggs
        eggTrader.sellEggs(12, 30);
        System.out.println("\nEggs left: "   eggTrader.getEggCount());
        System.out.println("Total cash: "   eggTrader.getCashOnHand());
    }
}

You only need to create one instance of class EggTrader since a single instance contains both cash and eggs. In your code you create two instances but they are not related. Changing one instance does not affect the other instance. So in this line of your code (in method main of class EggTraderDriver):

eggs.buyOneDozen(24);

you are only changing the eggs instance. The cash instance is unchanged.

There are also some problems in class EggTrader. In method buyOneDozen you don't update the eggs. If you buy one dozen eggs then you have 12 more eggs. And in method sellEggs you correctly update eggs but incorrectly update cash. The amount of money you get for the eggs you sell is the number of eggs sold multiplied by the unit price. You need to add that amount to cash.

Here is the output when I run the above code:

Current eggs: 0
Current cash: 500.0

Total eggs: 12
Cash left: 476.0

Eggs left: 0
Total cash: 836.0

CodePudding user response:

pls follow this

 public class EggTrader{
        double cash;
        int eggs;
        public EggTrader(){
            cash = 500;
            eggs = 0;
        }
        public void buyOneDozen(double pricePerDozen){
            cash = cash - pricePerDozen; 
            eggs =12  //if one dozen=12 or number of eggs he purchased
        }
        
        public void sellEggs(int numEggs, double unitPrice){
            eggs = eggs - numEggs;
            cash = cash   unitPrice; //
        }
        
        public int getEggCount(){
            return eggs;
        }
        public double getCashOnHand() {
            return cash;
        }
    }

// on the second class

public class EggTraderDriver{
    public static void main (String [] args){
        
        EggTrader eggTrader = new EggTrader();
       
        
        //cash.deposit(500);
        //current balance
        System.out.println("Current eggs: "   eggTrader.getEggCount());
        System.out.println("Current cash: "   eggTrader.getCashOnHand());
        
        //buy eggs
        eggTrader.buyOneDozen(24);
        System.out.println("\nEggs bought: "   eggTrader.getEggCount());
        System.out.println("Cash left: "   eggTrader.getCashOnHand());
      
        //sells eggs
        eggTrader.sellEggs(12,30);
        System.out.println("\nEggs sold: "   eggTrader.getEggCount());
        System.out.println("Cash earned: "   eggTrader.getCashOnHand());
    }
    
}
  • Related