Instructions
Requirements and Specifications
- Use methods
- Use classes
- Use constructors
- Use management system
Source Code
package GroceryMod4;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* @author Brian Reaves
*/
import java.lang.Math;
public class Grocery { // creating public class Grocery
String name; // declaring name variable as string
int quantity; // dclaring quanity variable as integer
double cost; // declaring cost variable as a decimal
double extendedCost; // declaring extendedCost variable as a decimal
public static int groceryObjectId; // declaring groceryObjectId variable as integer
public static int groceryObjectCounter; // declaring groceryObjectCounter variable as integer
public static int totalQuantity; // declaring totalQuantity variable as integer
public static double totalExtendedCost; // declaring totalExtendedCost variable as decimal
public Grocery() {
// initialize the varaiable to values
this.name = "";
this.cost = 0;
this.quantity = 0;
this.extendedCost = 0;
this.groceryObjectId = 0;
this.groceryObjectCounter = 0;
this.totalQuantity = 0;
this.totalExtendedCost = 0;
}
// set parameters to pass
public Grocery(String name, int quantity, double cost) {
this.name = name;
this.quantity = quantity;
this.cost = cost;
this.extendedCost = quantity * cost;
this.groceryObjectId = groceryObjectId +1;
this.groceryObjectCounter = groceryObjectCounter +1;
this.totalQuantity = totalQuantity +quantity;
this.totalExtendedCost = totalExtendedCost + extendedCost;
}
//create a get and a set for each variable private above (only gets for items calculate in main class
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getQuantity() {
return this.quantity;
}
public void setCost(double cost) {
this.cost = cost;
}
public double getExtendedCost() {
return this.extendedCost = cost * quantity;
}
public int getGroceryObjectID() {
return this.groceryObjectId;
}
public int getGroceryObjectCounter() {
return this.groceryObjectCounter;
}
// aggregate the item quantities
public int getTotalQuantity() {
return this.totalQuantity = totalQuantity;
}
// aggregate the costs of all the items
public double getTotalExtendedCost() {
return this.totalExtendedCost = totalExtendedCost;
}
public String getTotalsAverage() {
return " Average Cost = $" + (this.totalExtendedCost / this.totalQuantity );
}
//to string method for outputting the grocery superclass items
public String toString() {
return "\n Name: " + this.name.toUpperCase()
+ "\n Quantity: " + this.quantity
+ "\n Cost per item: $" + this.cost
+ "\n Total Cost for these items: $" + this.extendedCost
+ "\n";
}
}
Related Samples
Dive into our Java Assignment Samples for expertly crafted solutions to coding problems. Enhance your skills in object-oriented programming, Java frameworks, and algorithm implementation with clear, step-by-step examples. Designed to aid students in mastering Java concepts and achieving academic success.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java