Help interpreting what a class is supposed to do
I am supposed to determine what various sections of the code are supposed to do. My comments are in red. The first class is the one I am interpreting. The others are provided for reference. All constructive criticism is welcome. Thanks in advance for your help...
Code:
public class Inventory {
private Product[] list; [COLOR="Red"]// Here where the array is declared[/COLOR]
public Inventory(int size) { [COLOR="Red"]// Here is where the actual arrays is created[/COLOR]
list = new Product[size];
}
public int size() {
return list.length;
}
public double totalValue() { [COLOR="Red"]//This area computes the total value of the inventory[/COLOR]
double val = 0.0;
for (int i = 0; i < list.length; i++) {
val += list[i].value();
}
return val;
}
public void add(Product d, int p) { [COLOR="Red"]// adds an item to the array[/COLOR]
list[p] = d;
}
public Product get(int i) { [COLOR="Red"]// gets an item from product class to add to array[/COLOR]
return list[i];
}
[COLOR="Red"]// sort by name using a bubble sort[/COLOR]
public void sort() {
int n = list.length;
for (int search = 1; search < n; search++) {
for (int i = 0; i < n-search; i++) {
if (list[i].getdvdName().compareToIgnoreCase(list[i+1].getdvdName()) > 0) {
Product temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;
}
}
}
}
Code:
public class InventoryProgram {
public static void main(String args[]) {
//make products
Product product1 = new Product(1,"House", 11,16.99);
Product product2 = new Product(2,"Heroes", 25,21.99);
Product product3 = new Product(3,"24", 2,14.99);
// inventory object
Inventory obj = new Inventory(3);
obj.add(product1, 0);
obj.add(product2, 1);
obj.add(product3, 2);
obj.sort();
/// output
for (int i = 0; i < obj.size(); i++) {
System.out.println(obj.get(i));
System.out.println();
}
// total val
System.out.printf("Total value: $%.2f", obj.totalValue());
Code:
public class Product {
// variables
private int itemNumber;
private String dvdName;
private int units;
private double price;
// constructor for the product
public Product(int itemNumber, String dvdName, int units, double price) {
this.itemNumber = itemNumber;
this.dvdName = dvdName;
this.units = units;
this.price = price;
}
// total value
public double value() {
return units*price;
}
// getters and setters
public int getitemNumber() {
return itemNumber;
}
public void setitemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}
public String getdvdName() {
return dvdName;
}
public void setdvdName(String dvdName) {
this.dvdName = dvdName;
}
public int getUnits() {
return units;
}
public void setUnits(int units) {
this.units = units;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString () {
return "Item number: " + itemNumber
+ "\nName: " + dvdName+ "\nUnits: " + units
+ "\nPrice: " + String.format("$%.2f", price)
+ "\nValue: " + String.format("$%.2f", value());