Problem: I need to create a 2-arg constructor to do the following
-->
2-arg constructor that accepts a RetailItem object as an argument that is used to initialize the retailCost attribute.
The 2-arg constructor is created in the CashRegister class.
here are my classes so far:
-------------------------------------------------------

/*
 * Retailitem Class Modification
 */

public class RetailItem
{
     //attributes
     private String description;  // Item description
     private int itemNumber;      // Item number
     private CostData cost;       // Cost data

	//constructors
	
		
	
	  public RetailItem()
	  {
	  
	  }//end default constructor
	  
     public RetailItem(String desc, int itemNum,
                       double wholesale, double retail)
     {
          description = desc;
          itemNumber = itemNum;
          cost = new CostData(wholesale, retail);
     }//end 3-arg constructor

     public RetailItem(String desc, double retail)
	 {
	 	   description = desc;
	 	   cost = new CostData(retail);

	 }//end 2-arg constructo

	//Setters
     public void setWholesaleCost(double w)
     {
          cost.wholesaleCost = w;
     }

     public void setRetailCost(double r)
     {
          cost.retailCost = r;
     }

     //Getters

	 public String getDescription()
	 {
		return description;


	 }

	 public int getItemNumber()
	 {
		 return itemNumber;
 	 }


     public double getWholesaleCost()
     {
          return cost.wholesaleCost;
     }


     public double getRetailCost()
     {
          return cost.retailCost;
     }



     /**
      * CostData Inner Class
      */

     private class CostData
     {
		 //attributes
          public double wholesaleCost;  // Wholesale cost
          public double retailCost;     // Retail cost


         //constructors
          public CostData(double w, double r)
          {
               wholesaleCost = w;
               retailCost = r;

          }//end constructor

          public CostData(double retail)
		  {
		  	   retailCost = retail;

		  }//end 1-arg constructor

     }//end CostData class

}//end RetailItem class

------------------------------------------------------------------

public class CashRegister
{

	//Attributes
	public double retailCost;
	public int quantity;
	final double TAX_RATE = 0.06;
	double subtotal;	
	double Tax;
	double total;	
//	File salesReceipt;
		
		RetailItem myRetail = new RetailItem();
		

	
	//Constructors
	public CashRegister()
	{
		
	
	}//default constructor
	
	public CashRegister(int q)
	{
		
		//this.retailCost = r;
		quantity = q;
	
	}//end 2-arg constructor
	
	//Methods
	
	public double getSubTotal()
	{
	 subtotal = retailCost*quantity;
		return subtotal;
	}//end getSubTotal
	
	
	
	public double getTax()
	{
		Tax = retailCost*TAX_RATE;
		return Tax;
	}//end getTax
	
	public double getTotal()
	{
		total = subtotal+Tax;
		return total;
	}//end getTotal
	
//	public File createReceipt(double p,int q, double s, double st, double t )
//	{
//		salesReceipt = new File("salesReceipt.txt");
//	return salesReceipt;
	
//	}//end createReceipt
	
	

	
}//end CashRegister

-------------------------------------------------------------

public class Transaction
{

	
	
	
	
	//Constructors
	
	public Transaction()
	{
	
	}//end default constructor
	
	//Methods
	
	public static void main(String[] args)
	{
	String itemName = " ";
	double itemCost = 0.00;

	
	
	Scanner sc = new Scanner(System.in);
	NumberFormat nf = NumberFormat.getCurrencyInstance();

	RetailItem myRetailItem = new RetailItem();
	CashRegister myCashRegister = new CashRegister();
	
	System.out.print("What is the name of the item being purchased? : " );
	itemName = sc.nextLine();
	System.out.print("What is the cost of the item? : ");
	
	System.out.print("How many " + itemName + "(s) are you buying? ");
	myCashRegister.quantity = sc.nextInt();
	
	
	
	System.out.println();
	System.out.println();
	System.out.println();
	
	System.out.print("Subtotal:     " + nf.format(myCashRegister.getSubTotal()));
	System.out.println();
	System.out.print("Salestax:     ");
	System.out.println();
	System.out.print("Total:        ");
	
	System.out.println();
	System.out.print("Your receipt is printing.");
	
	
	
	}//end Main



}//end class

Mainly I just need help in creating that 2 arg constructor.

Edited 6 Years Ago by DRxACULA: n/a

A 2 arg constructor has 2 arguments. Like this:
public CashRegister(ArgType1 arg1, ARGType2 arg2) {..}

Examples:
public CashRegister(double d, int i) {..}
or

public CashRegister(RetailItem rItem, int q) {
  myRetail = rItem;
  quantity = q;
}

Call it like you call any other constructor with the appropriate arguments

This article has been dead for over six months. Start a new discussion instead.