Question 4 Retail Item Class Using this parameters Default a

Question 4: Retail Item Class

Using this parameters:

Default and parameterized constructors

Sets and Gets for all class variables

A toString()

Whatever functionality the assignment requires

The class instantiated in a main, proving that the methods work

Write a class named RetailItemClass that holds data about an item in a retail store. The class should have the following fields:

Description: the description field references a string object that holds a brief description of item.

units on Hand: The unit on hands field is an int variable that holds the number of unit currently in inventory

price:The price field is a double that holds the item\'s retail price

Write a constructor that accepts the arguments for each field,appropriate mutator methods that stores values in these fields,and accessor methods that returns the values in these fields. Once you have written the class,write a separate progarm that creates three Retail Item objects and stores the following data in them:

#1 12 units jacket cost 59.95

#2 40 units Designer Jeans cost 34.95

#3 20 units of shirts cost 24.95

Solution

RetailItem.java

public class RetailItem {
   //Declaring instance variables
private String description;
private int units_On_Hand;
private double price;

//Declaring static variable
static int count=0;

//Parameterized constructor
public RetailItem(String description, int units_On_Hand, double price) {
   this.description = description;
   this.units_On_Hand = units_On_Hand;
   this.price = price;
}

//Setters and getters
public String getDescription() {
   return description;
}
public void setDescription(String description) {
   this.description = description;
}
public int getUnits_On_Hand() {
   return units_On_Hand;
}
public void setUnits_On_Hand(int units_On_Hand) {
   this.units_On_Hand = units_On_Hand;
}
public double getPrice() {
   return price;
}
public void setPrice(double price) {
   this.price = price;
}

//toString() method which displays the contents of an object inside it
@Override
public String toString() {
  
   System.out.printf(\"Item#%d\\t%10s\\t\\t%5d\\t\\t\\t%.2f\ \",(++count),description,units_On_Hand,price);
   return \"\";
}

}

______________________

TestClass.java

import java.util.ArrayList;

public class TestClass {

   public static void main(String[] args) {
      
       //Creating an ArrayList object
       ArrayList<RetailItem> al=new ArrayList<RetailItem>();
      
      
       //Creating the RetailItem class object by passing the inputs as arguments
       RetailItem ri1=new RetailItem(\"Jacket\",12,59.95);
      
       //Adding the RetailItem Object to the ArrayList
       al.add(ri1);
      
       //Creating the RetailItem class object by passing the inputs as arguments
       RetailItem ri2=new RetailItem(\"Designer Jeans\",40,34.95);
      
       //Adding the RetailItem Object to the ArrayList
       al.add(ri2);
      
       //Creating the RetailItem class object by passing the inputs as arguments
       RetailItem ri3=new RetailItem(\"Shirt\",20,24.95);
      
       //Adding the RetailItem Object to the ArrayList
       al.add(ri3);
      
      
System.out.println(\"\\t\\tDescription\\t\\tUnits on Hand\\t\\tPrice\");
  
//This for loop will iterate over the ArrayList
       for(RetailItem ri:al)
       {
           ri.toString();
       }
      

   }

}

______________________

Output:

Description       Units on Hand       Price
Item#1   Jacket 12           59.95
Item#2   Designer Jeans 40           34.95
Item#3   Shirt 20           24.95

___________Thank You

Question 4: Retail Item Class Using this parameters: Default and parameterized constructors Sets and Gets for all class variables A toString() Whatever function
Question 4: Retail Item Class Using this parameters: Default and parameterized constructors Sets and Gets for all class variables A toString() Whatever function
Question 4: Retail Item Class Using this parameters: Default and parameterized constructors Sets and Gets for all class variables A toString() Whatever function

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site