Design a class that stores information about products that are sold in a store.
ID: 3894024 • Letter: D
Question
Design a class that stores information about products that are sold in a store. Write a class named Item that has (at least) the following member variables: (Just do the part 1 and Part 2)
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, andsalePrice 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 taxablemember 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.
If your Item class is written properly, the following lines of code should be valid in your main method:
Item chair = new Item("Desk Chair", 30, 55);
//increase cost price by $3.
chair.increaseCost();
chair.increaseCost();
chair.increaseCost();
//display the profit
System.out.println("The chair’s profit is now $" + chair.profit());
//set the chair’s weight to 7 lb
chair.setWeight(7);
Item table = new Item("Picnic Table”, 70, 88);
System.out.println("The table’s profit is now $" + table.profit());
Write a Java Code:-
Part I:
This is a continuation of the Item class we designed in the last assignment.
Add a toString method to your Item class. This method does not accept any parameters and returns a String reflecting the current state of the object. Once you have written the toString method, the following statement will display the contents of all member variables in an Item object called chair:
System.out.println(chair);
Part II:
Create an array of Item objects. Then write a loop that calculates the total weight of the Items in the array. Since the weight is a private data member of the Item class, the loop will need to call an accessor method to query the weight of each Item in the array.
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Item.java
=========
public class Item {
private String name;
private String vendor;
private double salePrice;
private double costPrice;
private double weight;
private boolean taxable;
public Item(String name, double costPrc, double salePrc)
{
this.name = name;
this.costPrice = costPrc;
this.salePrice = salePrc;
vendor = "";
}
public String getName() {
return name;
}
public String getVendor() {
return vendor;
}
public double getSalePrice() {
return salePrice;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public boolean isTaxable() {
return taxable;
}
public void setTaxable(boolean taxable) {
this.taxable = taxable;
}
public void increaseCost()
{
costPrice = costPrice + 1;
}
public double profit()
{
return salePrice - costPrice;
}
public String toString()
{
return "Name: " + name + ", Cost Price: $" + costPrice + ", Sale Price: $" + salePrice
+ ", Weight: " + weight + ", Taxable: " + taxable + ", Vendor: " + vendor;
}
}
ItemDemo.java
=========
public class ItemDemo {
public static void main(String[] args) {
Item chair = new Item("Desk Chair", 30, 55);
//increase cost price by $3.
chair.increaseCost();
chair.increaseCost();
chair.increaseCost();
//display the profit
System.out.println("The chair’s profit is now $" + chair.profit());
//set the chair’s weight to 7 lb
chair.setWeight(7);
Item table = new Item("Picnic Table", 70, 88);
System.out.println("The table’s profit is now $" + table.profit());
//part 1 demo toString()
System.out.println(chair);
System.out.println(table);
//part 2
Item[] items = new Item[3];
items[0] = chair;
items[1] = table;
items[2] = new Item("Dining Table", 100, 130);
items[2].setWeight(25);
double totalWeight = 0;
System.out.println("The items in the array are ");
for(int i = 0; i < items.length; i++)
{
System.out.println(items[i]);
totalWeight += items[i].getWeight();
}
System.out.println("The total weight is " + totalWeight);
}
}
output
=====
The chair’s profit is now $22.0
The table’s profit is now $18.0
Name: Desk Chair, Cost Price: $33.0, Sale Price: $55.0, Weight: 7.0, Taxable: false, Vendor:
Name: Picnic Table, Cost Price: $70.0, Sale Price: $88.0, Weight: 0.0, Taxable: false, Vendor:
The items in the array are
Name: Desk Chair, Cost Price: $33.0, Sale Price: $55.0, Weight: 7.0, Taxable: false, Vendor:
Name: Picnic Table, Cost Price: $70.0, Sale Price: $88.0, Weight: 0.0, Taxable: false, Vendor:
Name: Dining Table, Cost Price: $100.0, Sale Price: $130.0, Weight: 25.0, Taxable: false, Vendor:
The total weight is 32.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.