Question: Java Create a new public class named PopMachine that utilizes the following code and follows the comments to add code where needed. // Insert the
Java
Create a new public class named PopMachine that utilizes the following code and follows the comments to add code where needed.
// Insert the needed import statements here
/**
* @author Insert Your name here
* Class PopMachine simulates simple functionality of a PopMachine
*/
public class PopMachine
{
public static void main(String[] args) throws IOException
{
final int MAX_SODAS = 10;
// Create a new array named popMachine that will be able
// to hold MAX_SODAS Soda objects
int numSodas = fillMachine(popMachine, "Sodas.txt");
// Loop to reduce the quantity of each soda in the popMachine
// by one. Note that the machine may not be filled. Use
// the reduce method.
saveMachine(popMachine, numSodas, "SodasChanged.txt");
System.out.println("Machine closed.");
}
/**
* fillMachine loads the machine with sodas from the specified file.
* @param popMachine The array to fill with sodas
* @param fileName The name of the file containing soda information
* @return The number of sodas read into the machine
* @throws IOException
*/
public static int fillMachine(Soda[] popMachine,
String fileName) throws IOException
{
// Check if the file exists. If not, inform the user that the
// file does not exist and exit with an error code of 1.
// Open the file for reading and read each Soda into the array
// Stop reading when there are no more lines to process or the
// array is filled with sodas.
// Note: In each loop iteration, you will need to add a new Soda
// object to the array, get the 4 lines of data associated with
// that soda, and set the soda object with that data. You will need
// to keep track of the number of sodas read, and that can be used
// as an index into the array.
// Close the file
// Return the number of sodas read
}
/**
* saveMachine writes the popMachine data back to file in the same format
* as originally read.
* @param popMachine The array that contains the sodas
* @param numSodas The number of sodas in the array
* @param fileName The name of the file to write the soda information
* @throws IOException
*/
public static void saveMachine(Soda[] popMachine, int numSodas,
String fileName) throws IOException
{
// Open the fileName for writing. No need to check for an error.
// Create a DecimalFormat class that will output the price with
// always 1 digit before the decimal point and 2 digits after.
// Loop to write the soda data back to file in the same original format.
// Close the output file.
}
}
Here is my soda class
/**
*
* @author
* The Item class tests new items and different values.
*
*/
public class Soda {
private String name;
private double price;
private int quantity;
private int ounces;
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
public int getQuantity()
{
return quantity;
}
public int getOunces()
{
return ounces;
}
public void setName(String name)
{
this.name = name;
}
public void setPrice(double price)
{
this.price = price;
if (price < 0)
this.price = 0;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
if (quantity < 0)
this.quantity = 0;
}
public void setOunces(int ounces)
{
this.ounces = ounces;
if (ounces < 0)
this.ounces = 0;
}
public Soda() {
setName("");
setPrice(0);
setQuantity(0);
setOunces(0);
}
public Soda(String name, double price, int quantity, int ounces) {
setName(name);
setPrice(price);
setQuantity(quantity);
setOunces(ounces);
}
public void reduce(int amount) {
if (amount > 0)
quantity = quantity - amount;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
