In: Computer Science
to be done in java
Your task is to finish the StockItem class so that it meets the following criteria
• The StockItem class will have 4 attributes:
a stock number;
a name;
the price of the item;
the total number of items currently in stock
• The first three of the above characteristics will need to be set at the time a StockItem object is created, with the total number of items set to 0 at this time. The stock number and name will not need to be changed after an item is created.
• The following methods are also required:
o A method that allows the price to be re-set during the object's lifetime
o a method that takes an integer argument and adds this to the total number of items in stock
o a method that returns the total value of items of this stock type;
calculated by multiplying the price of the item by the number of items in stock (price * units)
Required Methods: StockItem(String, String, double) (class constructor) setPrice(double) increaseTotalStock(int) getStockNumber(): String getName(): String getTotalStock(): int getPrice(): double calculateTotalPrice(): double
Requested files StockItem.java
/** * A class representing an item in stock in some sort * of inventory system */
public class StockItem {
//TODO: add fields for a: name, stockNumber, price, units.
//TODO: create a constructor that takes name, stockNumber and price as arguments
// and sets units to 0.
//TODO: create accessor ("getter") methods for price, stock number and name.
//TODO: create a method increaseTotalStock that increases units by a given quantity.
//TODO:create a mutator ("setter") method setPrice that sets price to a given amount.
//TODO: create a method calculateTotalPrice that returns the total price of the current inventory
// (Calculated as current price * number of units) }
Code is Given Below:
-----------------------------------
public class StockItem {
private String stockNumber;
private String name;
private double price;//the price of the item;
private int totalNumber;//the total number of items
currently in stock
//constructor that takes name, stockNumber and price
as arguments
public StockItem(String stockNumber, String name,
double price) {
this.stockNumber =
stockNumber;
this.name = name;
this.price = price;
this.totalNumber=0;
}
public void setPrice(double price) {
this.price=price;
}
// method increaseTotalStock that increases units by a
given quantity.
public void increaseTotalStock(int n) {
totalNumber=totalNumber+n;
}
//accessor ("getter") methods for price, stock number
and name.
public String getStockNumber() {
return stockNumber;
}
public String getName() {
return name;
}
public int getTotalStock() {
return totalNumber;
}
public double getPrice() {
return price;
}
//method calculateTotalPrice that returns the total
price of the current inventory
public double calculateTotalPrice() {
return price*totalNumber;
}
}
Code Snapshot:
-----------------------------