×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Create A Program to Create a Grocery System in Java Assignment Solution

July 02, 2024
Donna J. Seymour
Donna J.
🇸🇬 Singapore
Java
Donna J. Seymour, PhD in Computer Science from an esteemed Austrian university, with 8 years of experience in Java assignments. Specializing in advanced Java programming and academic mentoring, ensuring robust solutions and student success.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Start by understanding the problem, then choose relevant algorithms, and test incrementally. This approach helps in debugging and enhances clarity, especially in assignments involving machine learning models or neural networks.
News
In 2024, Python updates introduced improvements in TensorFlow and FastAPI, enhancing AI and web development for programming students. New cybersecurity standards were also implemented to protect open-source applications​

Instructions

Objective
Write a java assignment program to create a grocery system in.

Requirements and Specifications

Create a management system related to a grocery system.
  •   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.