Question
Your supervisor is very pleased with your progress and has provided you an updated set of program specifications to include additional functionality. As an incremental
Your supervisor is very pleased with your progress and has provided you 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 objects attributes to the output .txt file using a polymorphic call to the toString() method.
- The final totals and average cost.
- Headings:
Grocery.java:
package grocery; public class Grocery { /* attributes that will be used */ String name; int quantity; double cost; double extendedCost;
public static int groceryObjectID; public static int groceryObjectCounter; public static int totalQuantity; public static double totalExtendedCost;
public Grocery() {
this.name = null; this.quantity = 0; this.cost = 0.0; this.extendedCost = 0.0; this.groceryObjectID = 0; this.groceryObjectCounter = 0; this.totalQuantity = 0; this.totalExtendedCost = 0.0; }
public Grocery(String name, int quantity, double cost) {
this.name = name; this.cost = cost; this.groceryObjectID = groceryObjectID + 1; this.groceryObjectCounter = groceryObjectCounter + 1; this.quantity = quantity; this.totalQuantity = totalQuantity + quantity; this.totalExtendedCost = totalExtendedCost + cost; } 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 int getTotalQuantity() { return this.totalQuantity; }
public double getExtendedCost() { return this.extendedCost; }
public double getTotalExtendedCost() { return this.totalExtendedCost; }
public String getTotalsAverage() { return "Average Cost = $" + (totalExtendedCost / totalQuantity); }
public String toString() { return " " + this.name.toUpperCase() + " | Qty: " + this.quantity + " | Cost: $" + this.cost; }
}
Meat.java
package grocery; public class Meat extends Grocery { private int cookingTemp; private String cookingTime; private boolean cooked;
//Constructor() public Meat() { //calls default Constructor of Grocery Class using super keyword super(); this.cookingTemp=0; this.cookingTime=""; this.cooked= false; } public Meat(String name,int quantity,double cost,int cookingTemp,String cookingTime,boolean cooked) { //Calls overload constructor of Grocery Class super(name,quantity,cost); this.cooked= cooked; this.cookingTime= cookingTime; this.cookingTemp= cookingTemp; } //getters and setters public int getCookingTemp() { return cookingTemp; }
public void setCookingTemp(int cookingTemp) { this.cookingTemp = cookingTemp; }
public String getCookingTime() { return cookingTime; }
public void setCookingTime(String cookingTime) { this.cookingTime = cookingTime; }
public boolean isCooked() { return cooked; }
public void setCooked(boolean cooked) { this.cooked = cooked; } //Overriding the toString() method of super class Grocery @Override public String toString() { return super.toString()+" Cooking Temperature: "+cookingTemp+" Cooking Time: "+cookingTime+ " Cooked: "+cooked; } }
Produce.java
package grocery; public class Produce extends Grocery { private boolean needsPeeling; boolean peeled; public Produce() { //calls default Constructor of Grocery Class using super keyword super(); needsPeeling= false; peeled= false; } public Produce(String name,int quantity,double cost,boolean needsPeeling, boolean peeled) { //Calls overload constructor of Grocery Class super(name,quantity,cost); this.needsPeeling= needsPeeling; this.peeled= peeled; } //getters and setters public boolean isNeedsPeeling() { return needsPeeling; }
public void setNeedsPeeling(boolean needsPeeling) { this.needsPeeling = needsPeeling; }
public boolean isPeeled() { return peeled; }
public void setPeeled(boolean peeled) { this.peeled = peeled; } //Overriding the toString() method of super class Grocery @Override public String toString() { return super.toString()+" Needs peeling: "+needsPeeling+" Peeled: "+peeled; } }
import java.util.ArrayList; import java.util.Scanner; import java.io.IOException; public class GroceryDriver { //function to display menu public static void displayMenu() { System.out.println("Select an option: (1) Input a Grocery item, " + " " + "(2) Input Meat item, " + " " + "(3) Input Produce item, " + " " + "(-1) End user input. Enter: "); } public static void main(String[] args) { //array list to hold Groceries ArrayList
Scanner in = new Scanner(System.in); int userChoice=-1; do { //display menu
displayMenu(); userChoice = in.nextInt(); String name=""; int quantity=0; double cost=0.0; int cookingTemp=0; String cookingTime=""; boolean cooked=false; boolean needsPeeled=false; boolean peeled = false;
switch (userChoice) {
case 1: in.nextLine(); System.out.println("Enter Item Name"); name = in.nextLine(); System.out.println("Enter the quanity"); quantity = Integer.parseInt(in.nextLine()); if(quantity<1) { System.out.println("Invalid quantity!!"); break; }
System.out.println("Enter the cost"); cost = Double.parseDouble(in.nextLine()); if(cost<=0) { System.out.println("Invalid Cost!!!!"); break; } groceries.add(new Grocery(name, quantity, cost)); break; case 2: in.nextLine(); System.out.println("Enter Item Name"); name = in.nextLine(); System.out.println("Enter the quanity"); quantity = Integer.parseInt(in.nextLine()); if(quantity<1) { System.out.println("Invalid quantity!!"); break; }
System.out.println("Enter the cost"); cost = Double.parseDouble(in.nextLine()); if(cost<=0) { System.out.println("Invalid Cost!!!!"); break; } System.out.println("Cooking Temperature"); cookingTemp = Integer.parseInt(in.nextLine()); System.out.println("Enter cooking Time: "); cookingTime = in.nextLine(); System.out.println("Is item Cooked? "); cooked = in.nextBoolean(); //Create object of meat class Meat meat = new Meat(name,quantity,cost,cookingTemp, cookingTime, cooked);
//add to the arraylist groceries.add(meat);
break; case 3: in.nextLine(); System.out.println("Enter Item Name"); name = in.nextLine(); System.out.println("Enter the quanity"); quantity = Integer.parseInt(in.nextLine()); if(quantity<1) { System.out.println("Invalid quantity!!"); break; }
System.out.println("Enter the cost"); cost = Double.parseDouble(in.nextLine()); if(cost<=0) { System.out.println("Invalid Cost!!!!"); break; } System.out.println("needs to peeled"); needsPeeled = in.nextBoolean(); System.out.println("Has been peeled"); peeled = in.nextBoolean(); //Create object ob Produce Produce produce = new Produce(name,quantity,cost,needsPeeled, peeled); produce.setName(name); produce.setQuantity(quantity); produce.setCost(cost); //Add to array list groceries.add(produce); break;
} }
GroceryDriver.java:
while (userChoice!=-1);
//print the information
for(int i =0;i public SourceVersion getSupportedSourceVersion() { return SourceVersion.latest(); } } Output: Select an option: (1) Input a Grocery item, (2) Input Meat item, (3) Input Produce item, (-1) End user input. Enter: 3 Enter Item Name corn Enter the quanity 2 Enter the cost 1.99 needs to peeled true Has been peeled false Select an option: (1) Input a Grocery item, (2) Input Meat item, (3) Input Produce item, (-1) End user input. Enter: -1 Grocery Item 1: CORN | Qty: 2 | Cost: $1.99 Needs peeling: true Peeled: false Total1 Cost: $1.99 | Total Quantity: 2 | Average Cost = $0.995
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started