-3

Alright so I need help here I created a cash register java file and a retailItem file in my cash register file it cant read a section i put in retailItem because its private. So my question is how do i access that private variable that i wrote in my retailItem class into the cash register file

So here's my code for retailItem

import java.text.DecimalFormat;
public class RetailItem
{
 private String description;
 private double price;
 private double unitsOnHand;
 private CostData cost;


public void setDescription(String userDescription)
{ 
  description = userDescription;

}

public void setPrice(double p)
{
  price = p;
}


public void setUnitsOnHand(double userUnitsOnHand)
{ 
  unitsOnHand = userUnitsOnHand;

}

public String getDescription()
{
  return description;
}
public double getPrice()
{
  return price;
}

public double getUnitsOnHand()
{
  return unitsOnHand;
}



 public RetailItem(RetailItem object2)
 {
  description = object2.description;
  price = object2.price;
  unitsOnHand = object2.unitsOnHand;
  cost = new CostData(object2.cost.wholeSale,object2.cost.Retail);
 }



public RetailItem( String descriptionGet,double pri, double 
unitsOnHandGet,double wholeSale,double retail)
{  
  description = descriptionGet;
  price = pri;
  unitsOnHand = unitsOnHandGet;
  cost = new CostData(wholeSale,retail);
}


public String toString()
{
  String str;

  DecimalFormat dollar = new DecimalFormat("#,##0.00");

  str = "Description: " + description +
        "Item Price: " + price +
        "\nItem Number: " + unitsOnHand +
        "\nWhole Cost: $" + dollar.format(cost.wholeSale) +
        "\nRetail Price: $" + dollar.format(cost.Retail);

        return str;
}

public class CostData
{
 public double wholeSale;
 public double Retail;

 public CostData(double whole,double re)
 {
  wholeSale = whole;
  Retail = re;

 }

 public void setRetail(double re)
 {
  Retail = re;
 }

public void setWholeSale(double whole)
{
  wholeSale = whole;
}

public double getRetail()
{
  return Retail;
}
public double getWholeSale()
{
  return wholeSale;
}


}    
} 

Here is the Cash Register class:

public class CashRegister
{
 private RetailItem retail;
 private int quantityItem;
 private final double SALES_TAX = 0.06;
 private int subTotal;


 public CashRegister()
{
  quantityItem = 0;
  subTotal = 0;
}


 public CashRegister(RetailItem retailObject,int quantity)
{
  retail = new RetailItem(retailObject);
  quantityItem = quantity;
}

public RetailItem getRetailItem()
{
  return new RetailItem(retail);
}

public double getSubTotal()
{
 return quantityItem * retail.cost.getRetail();//Here is where the problem is 

}

public double getTax()
{
  return SALES_TAX;
}
public double getTotal()
{
  return subTotal + SALES_TAX;
}

}

1 Answers1

0

You have a number of options:

  1. If they are in the same package, you can set the access of cost to
    default (remove the word private from in front of it.)
  2. Change cost from private to public.
  3. Create a default access (neither private nor public) getCost() method to return cost.
  4. Create a public getCost() method to return cost.

The first option gives read/write access to any class in the same package as RetailItem.

The second option gives read/write access to all classes.

The fourth option gives read-only access to all classes.

protected access could also be an option. Its like package access, but also gives read/write access to subclasses.

See also: In Java, difference between default, public, protected, and private

Jaguar
  • 116
  • 1
  • 12