Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class using JAVA that stores information about products that are sold i

ID: 3601168 • Letter: D

Question

Design a class using JAVA that stores information about products that are sold in a store. Write a class named Item that has (at least) the following member variables:

name: a String that holds the name of the inventory item.

vendor: a String that stores the name of the vendor for the product.

salePrice: a double that stores the current selling price of the item (how much the customer pays to buy it).

costPrice: a double that stores the current cost price of the item (how much the store pays to acquire it).

weight: a double that stores the weight of a single item of this type.

taxable: a boolean field that indicates whether or not tax is charged on the item.

Make sure to choose appropriate access specifiers for the data members and methods in your Item class.

In addition, the class should have the following member methods:

Constructor. The constructor should accept the item’s name, cost price, and selling price as arguments and assign these values to the object's name, costPrice, and salePrice member variables. The constructor should initialize the weight to 1 and taxable to true.

Accessors. Appropriate accessor methods should be created to allow values to be retrieved from an object's name, vendor, salePrice, weight, and taxable member variables.

Mutators. Appropriate mutator methods should be created to allow values to be changed in an object’s weight and taxable fields.

increaseCost. This method should increase the cost price by 1 dollar. This is a void method since it modifies the current state of the object.

profit. This method should accept no parameters and return the profit on the item, which is calculated as the cost price subtracted from the selling price.

Demonstrate the class in a program that creates an Item object. Then increase the cost 3 times by a dollar, calculate the profit and display it on the screen. Call the mutator method that sets the weight to a number you specify. Create several other Item objects and see that the fields have different values.

Explanation / Answer

Item.java

public class Item {
//Declaring instance variables
private String name;
private String vendor;
private double sellPrice;
private double costPrice;
private double weight;
private boolean taxable;

//Parameterized constructor
public Item(String name, double sellPrice, double costPrice) {
super();
this.name = name;
this.sellPrice = sellPrice;
this.costPrice = costPrice;
this.weight = 1;
this.taxable = true;
}

//getters and setters
public String getName() {
return name;
}

public String getVendor() {
return vendor;
}

public double getSellPrice() {
return sellPrice;
}

public double getWeight() {
return weight;
}

public boolean isTaxable() {
return taxable;
}

public void setWeight(double weight) {
this.weight = weight;
}

public void setTaxable(boolean taxable) {
this.taxable = taxable;
}

//This method will increase the cost of the product by 1 dollar
public void increasCost() {
costPrice += 1;
}

//This method will calculate the profit
public double profit() {
return sellPrice - costPrice;
}

@Override
public String toString() {
return "Item [name=" + name + ", vendor=" + vendor + ", sellPrice=" + sellPrice + ", costPrice=" + costPrice + ", weight=" + weight + ", taxable=" + taxable + "]";
}

}

__________________

Test.java

public class Test {

public static void main(String[] args) {
//Creating an Instance of Item#1 class object
Item item1 = new Item("Book", 8.5, 3.0);
item1.increasCost();
item1.increasCost();
item1.increasCost();

System.out.println("Profit on Item#1:$" + (item1.profit()));
item1.setWeight(3.5);
//Displaying the Item#1 info
System.out.println(item1);

//Creating an Instance of Item#2 class object
Item item2 = new Item("Umbrella", 10.5, 6.5);
item2.setWeight(1);
item2.increasCost();
System.out.println("Profit on Item#2:$" + (item2.profit()));
//Displaying the Item#2 info
System.out.println(item2);


//Creating an Instance of Item#3 class object
Item item3 = new Item("Head Phones", 20, 15);
item3.setWeight(0.5);
item3.increasCost();
item3.increasCost();
System.out.println("Profit on Item#3:$" + (item3.profit()));
//Displaying the Item#3 info
System.out.println(item3);
}

}

____________________

Output:

Profit on Item#1:$2.5
Item [name=Book, vendor=null, sellPrice=8.5, costPrice=6.0, weight=3.5, taxable=true]
Profit on Item#2:$3.0
Item [name=Umbrella, vendor=null, sellPrice=10.5, costPrice=7.5, weight=1.0, taxable=true]
Profit on Item#3:$3.0
Item [name=Head Phones, vendor=null, sellPrice=20.0, costPrice=17.0, weight=0.5, taxable=true]


_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote