Instructions
Objective
Write a java homework to create grocery management system.
Requirements and Specifications
Using the files attached ( along with these instructions) add code to the file to: OPEN a file, READ a file, CLOSE the file, OUTPUT the same data that is printed on the screen to the OUTPUT file: Your Grocery Calculator application is growing, and the features and functionality demonstrate several types of programming skills you have developed. Your supervisor is very pleased with your progress and has provided you with an updated set of program specifications to include additional functionality. As an incremental update to your program, you are asked to generate an output file that is saved to disk. Your task this module is to do the following: Class including the Main() Method: ○ Include code to create a .txt file that contains the output from calling the toString() methods in your program. ○ Provide an appropriate file name that includes your first initial and last name suffixed by GroceryList.txt. § For example, if Pat Smith coded the application, the output file is named: psmithGroceryList.txt. ○ For this assignment, § code a specific path to where the .txt file is saved. § If the path has not been created, □ then create the path and output file. § Code methods to □ open and close the output file ® using appropriate try and catch blocks. § Include appropriate package import declarations as needed. ○ Output: to the console while executing the program: § Use System.out.printf to echo back to the user the data they input once validated. § Other console output as needed. § Output: Save to the .txt file: □ Headings: □ A greeting. □ Your Name. □ Your Class, Module/Week, and Assignment. □ Date/Time stamps. ○ Output generated by polymorphic calls to the toString() method during array processing. § After the user has signaled the termination of input and prior to exiting the program, □ process the array by writing ® each object’s attributes to ◊ the output .txt file using } a polymorphic call to the toString() method. – The final totals and average cost. ○ Other: § Run your program with at least □ two Grocery, □ two Meat, and □ two Produce □ object instances varying the attributes of each.
Source Code
package GroceryCalculator;
/*
* 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";
}
}
Similar Samples
Discover a wealth of programming homework samples at ProgrammingHomeworkHelp.com. These examples demonstrate our proficiency across various programming languages and problem-solving techniques. Whether you need assistance with data structures, algorithms, or software development projects, our samples provide clear insights and solutions tailored to academic requirements. Explore our portfolio to see how we can assist you in achieving your programming goals.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java