Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- /**
- * @author Andrew Moe
- * 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
- Soda[] popMachine = new Soda[MAX_SODAS];
- 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
- {
- int numSodas = 0;
- File inFile = new File("Sodas.txt");
- Scanner input = new Scanner(inFile);
- if (!inFile.exists())
- {
- System.out.println(inFile.getName() + " does not exist.");
- System.exit(1);
- }
- else
- {
- while(input.hasNextLine())
- {
- String name = input.nextLine();
- double price = Double.parseDouble(input.nextLine());
- int quantity = Integer.parseInt(input.nextLine());
- int ounces = Integer.parseInt(input.nextLine());
- popMachine[numSodas] = new Soda(name, price, quantity, ounces);
- ++numSodas;
- }
- }
- input.close();
- return numSodas;
- // 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
- {
- try(
- PrintWriter output = new PrintWriter(new File("SodasChanged.txt")))
- {
- for (int i = 0; i < numSodas; ++i)
- {
- output.println(popMachine[i]);
- }
- }
- // 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.
- }
- }
- /**
- * @author Andrew Moe
- * Class Soda sets the name, price, quantity and number of ounces of a soda
- * object. It also contains a method to reduce the quantity of sodas.
- */
- public class Soda
- {
- private String name;
- private double price;
- private static int quantity;
- private int ounces;
- public Soda()
- {
- setName("");
- setPrice(0);
- setQuantity(0);
- setOunces(0);
- }
- public Soda(String newName, double newPrice, int newQuantity, int newOunces)
- {
- setName(newName);
- setPrice(newPrice);
- setQuantity(newQuantity);
- setOunces(newOunces);
- }
- public void setName(String newName)
- {
- name = newName;
- }
- public String getName()
- {
- return name;
- }
- public void setPrice(double newPrice)
- {
- if(newPrice > 0)
- price = newPrice;
- }
- public double getPrice()
- {
- return price;
- }
- public void setQuantity(int newQuantity)
- {
- if (newQuantity > 0)
- quantity = newQuantity;
- }
- public int getQuantity()
- {
- return quantity;
- }
- public void setOunces(int newOunces)
- {
- if (newOunces > 0)
- ounces = newOunces;
- }
- public int getOunces()
- {
- return ounces;
- }
- /**
- * Method reduce takes a reduction parameter and reduces the
- * quantity of sodas.
- * @param reduction reduces quantity.
- */
- public static void reduce(int reduction)
- {
- if (reduction > 0 && quantity >= reduction)
- {
- quantity = quantity - reduction;
- }
- }
- @Override
- public String toString()
- {
- return String.format("%s%n%1.2f%n%d%n%d", getName(), getPrice(), getQuantity(), getOunces());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement