Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package week_6.q3_coffee; /** * * // Refactor the following program to use Drink

ID: 3722318 • Letter: P

Question

 package week_6.q3_coffee;  /**  *  *  // Refactor the following program to use Drink Objects.  This is the same question as week 5, but you'll refactor your program to  use Drink objects.    Write a program that creates a sales report for a coffee shop.  The coffee shop will use this at the end of every day to calculate sales, expenses, and profit.    The coffee shop sells 12 different drinks. The name of each drink, the price the shop  charges the customer, and how much it costs to make each drink, are saved in the file  coffee_price_data.txt. It's in the root directory of this project.    The data is in the format    name;cost to make;price charged    As in this example,    Cappuccino;1.56;3.50    So the cappuccino drink costs the coffee shop $1.56 to make, and they charge the customer $3.50.    The file coffee_sales_data.txt contains the sales data for one day. This file is in the format    name;number sold    As in this example,    Cappuccino;100    The coffee shop sold 100 cappuccino drinks.    Your program should read this data from coffee_price_data.txt, and coffee_sales_data.txt, and  store it all of the data for each drink in one Drink object. One Drink object  should store that drink's name, price, number sold, cost to make one drink, price one drink sold for.    A Drink object should be able to calculate the total expenses to make all the drinks of that type sold;  the total price all of that drink of that type sold for, and profit for that type of drink.    You should deal with any file-related exceptions properly.    Once you have gathered all the data, generate a report that will be written out to a new file called  daily_sales_report.txt. For each drink, record the number of drinks sold, the total that it cost to  make the total quantity of those drinks (expenses), and the total amount (revenue) spent by  customers on that drink.    So, for example, if the coffee shop sold 100 cappuccinos today, you'll write a line that says    Cappuccino: Sold 100, Expenses $150.60, Revenue $350.00, Profit $190.40    perhaps using this String formatting template...    "%s: Sold %d, Expenses $%.2f, Revenue $%.2f, Profit $%.2f"      And a similar line for each of the drinks. The autograder is looking for this exact format.    Write the drink data in the *same order* as found in the data files.    At the bottom of the file, write the total expenses, total revenue, and total profit for all drinks,  for example, like this,    All Drinks: Total Sold 1000, Expenses $1000, Revenue $2500, Profit $1500    You should use try-with-resources exception handling for both file reading, and file writing.    Use methods to organize your code. The autograder will call the salesReport() method, and will examine  the output file your program creates.   The instructor will assess the quality of your code and solution.    You should probably write some extra helper methods for the sub-tasks of this problem.    Optional: You can write your own unit tests for your Drink class methods.    Test and comment your code.    */  public class Question_3_Coffee_Shop {          public static void main(String[] args) {         Question_3_Coffee_Shop q7 = new Question_3_Coffee_Shop();         q7.salesReport();     }                    public String price_data_file = "coffee_price_data.txt";     public String sales_data_file = "coffee_sales_data.txt";          public String output_report_file = "daily_sales_report.txt";          public void salesReport() {                  // Suggested outline of program.                  // You may (and probably should) add more methods if necessary.                  Object allDrinkData = readCoffeeDataFiles(price_data_file, sales_data_file);  // TODO replace Object with the type of your data structure         writeReportFile(allDrinkData, output_report_file);              }               public Object readCoffeeDataFiles(String dataFile, String salesFile) {                  // TODO read in the data from the files         // TODO create a Drink object for each type of drink, containing all info about one drink         // TODO put all the Drink objects into some type data structure, and return it.         // TODO change the return type of this method to the type of your data structure.                  return null;              }          public void writeReportFile(Object drinkData, String filename) {                  // TODO finish this method.                  // You may find this format String helpful         String reportLineTemplate = "%s: Sold %d, Expenses $%.2f, Revenue $%.2f, Profit $%.2f";     }           } 

Explanation / Answer

Here is your solution:

Drink.java

/**

*

*/

package week_6.q3_coffee;

/**

* @author Lokesh Kumar

*

*/

public class Drink {

private String drinkName;

private double costToMake;

private double price;

private int noOfSold;

public String getDrinkName() {

return drinkName;

}

public void setDrinkName(String drinkName) {

this.drinkName = drinkName;

}

public double getCostToMake() {

return costToMake;

}

public void setCostToMake(double costToMake) {

this.costToMake = costToMake;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public int getNoOfSold() {

return noOfSold;

}

public void setNoOfSold(int noOfSold) {

this.noOfSold = noOfSold;

}

}

Question_3_Coffee_Shop .java

/**

*

*/

package week_6.q3_coffee;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

* @author Lokesh Kumar

*

*/

public class Question_3_Coffee_Shop {

public static void main(String[] args) {

Question_3_Coffee_Shop q7 = new Question_3_Coffee_Shop();

q7.salesReport();

}

public String price_data_file = "E://coffee_price_data.txt";

public String sales_data_file = "E://coffee_sales_data.txt";

public String output_report_file = "E://daily_sales_report.txt";

public void salesReport() {

List<Drink> allDrinkData = readCoffeeDataFiles(price_data_file, sales_data_file);

writeReportFile(allDrinkData, output_report_file);

}

public List<Drink> readCoffeeDataFiles(String dataFile, String salesFile) {

List<Drink> ArrayList<Drink>();

Map<String, Integer> HashMap<String, Integer>();

List<Drink> totalDrinkList = new ArrayList<Drink>();

BufferedReader in = null;

BufferedReader in1 = null;

try {

in = new BufferedReader(new FileReader(dataFile));

String priceLine;

while ((priceLine = in.readLine()) != null) {

Drink drink = new Drink();

String[] arg = priceLine.split(";");

drink.setDrinkName(arg[0]);

drink.setCostToMake(Double.parseDouble(arg[1]));

drink.setPrice(Double.parseDouble(arg[2]));

onlyDrinkList.add(drink);

}

in1 = new BufferedReader(new FileReader(salesFile));

String salesLine;

while ((salesLine = in1.readLine()) != null) {

String[] args = salesLine.split(";");

onlySalesMap.put(args[0], Integer.parseInt(args[1]));

}

for (Drink drink : onlyDrinkList) {

Drink drinks = new Drink();

drinks.setDrinkName(drink.getDrinkName());

drinks.setCostToMake(drink.getCostToMake());

drinks.setPrice(drink.getPrice());

drinks.setNoOfSold(onlySalesMap.get(drink.getDrinkName()));

totalDrinkList.add(drinks);

}

} catch (Exception exception) {

try {

in.close();

in1.close();

} catch (IOException e) {

e.printStackTrace();

}

exception.printStackTrace();

}

return totalDrinkList;

}

public void writeReportFile(List<Drink> drinkData, String filename) {

try {

File file = new File(filename);

if (!file.exists()) {

file.createNewFile();

}

BufferedWriter writer = new BufferedWriter(new FileWriter(filename));

for (Drink drink : drinkData) {

int noOfSold = drink.getNoOfSold();

double Expenses = noOfSold * drink.getCostToMake();

double Revenue = noOfSold * drink.getPrice();

double Profit = Revenue - Expenses;

writer.write(drink.getDrinkName() + ": Sold " + noOfSold + ", Expenses " + Expenses + ", Revenue "+ Revenue + ", Profit " + Profit+" ");

}

writer.close();

} catch (Exception exception) {

exception.printStackTrace();

}

}

}

Note :- In this code i have used E Drive to store and retrieve files.

coffee_price_data.txt

Cappuccino;1.56;3.50
Espresso;2.75;6.50
Ristretto;4.20;8.00

coffee_sales_data.txt

Cappuccino;100
Espresso;50
Ristretto;75

My output file daily_sales_report.txt

Cappuccino: Sold 100, Expenses 156.0, Revenue 350.0, Profit 194.0
Espresso: Sold 50, Expenses 137.5, Revenue 325.0, Profit 187.5
Ristretto: Sold 75, Expenses 315.0, Revenue 600.0, Profit 285.0