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

The price for a pizza depends on the size of the pizza and the number of topping

ID: 3879656 • Letter: T

Question

The price for a pizza depends on the size of the pizza and the number of toppings added to the pizza crust. The store charges $9.50 for a small size pizza, $12.25 for a medium size pizza, and $14.50 for a large pizza. Each topping costs $1.50.

The store just received an order of pizzas from one her customers. The order is stored in a textfile. Name of the file will be passed to the program when the program is executed. The number of pizzas ordered is unknown. However, two lines are used to hold the information of each pizza ordered. For each pizza ordered, the first line consists of two strings separated by one comma. The first string is about the crust type (“hand-tossed”, “thin”, or “thick”). The second string denotes the size of the pizza (“small”, “medium”, or “large”). The second line holds the toppings ordered for the pizza. Write a program to assist the manager at Pizza Delight in processing the order stored in the textfile. An order may consist of more than one pizza. The program displays the detail information about each pizza in the order followed by the amount due. The output of the pizza is arranged in descending order on cost of each pizza. The program also displays the total cost of the order with a 10% tax rate.

How to manuplate the string, so that calculations can be made from the information of the text file in java. Also how to sort it from descending order.

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.

Pizza.java
============
public class Pizza {
private String type;
private String size;
private String[] toppings;
//define constants
private final double COST_PER_TOPPING = 1.5; //constant per topping
private final double SMALL_COST = 9.5; //cost for small sized pizza
private final double MEDIUM_COST = 12.25; //cost for medium sized pizza
private final double LARGE_COST = 14.5; //cost for large sized pizza
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String[] getToppings() {
return toppings;
}
public void setToppings(String[] toppings) {
this.toppings = toppings;
}
private double getBaseCost()
{
if(size.equalsIgnoreCase("small"))
return SMALL_COST;
else if(size.equalsIgnoreCase("medium"))
return MEDIUM_COST;
else if(size.equalsIgnoreCase("large"))
return LARGE_COST;
else
return 0;
}
private double getToppingsCost()
{
return toppings.length * COST_PER_TOPPING;
}
public double getPizzaCost()
{
return getBaseCost() + getToppingsCost();
}
public String toString()
{

double sub = getBaseCost() + getToppingsCost();
String s = "Type: " + type +", Size: " + size + " ";
s += "Toppings: " ;
for(int i = 0; i < toppings.length; i++)
s += toppings[i] +" ";
s += " ";
s += "Cost of Base: $" + String.format("%.2f", getBaseCost()) + " ";
s += "Cost of toppings = $" + String.format("%.2f", getToppingsCost()) + " ";
s += "Pizza cost = $" + String.format("%.2f", sub) + " ";
return s;
}
}

PizzaOrder.java
==============
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PizzaOrder {
private static void sort(Pizza[] pizza, int n)
{
//using selection sort, sort in descending order
for(int i = 0; i < n; i++)
{
int maxIdx = i;
for(int j = i+1; j < n; j++)
{
if(pizza[j].getPizzaCost() > pizza[maxIdx].getPizzaCost())
maxIdx = j;
}
if(i != maxIdx)
{
Pizza temp = pizza[i];
pizza[i] = pizza[maxIdx];
pizza[maxIdx] = temp;
}
}
}
private static int loadFile(Scanner s, Pizza[] pizzas)
{
String line;
int n = 0;
while(s.hasNextLine())
{
line = s.nextLine();
String[] tokens = line.split(",");
Pizza p = new Pizza();
p.setType(tokens[0].trim());
p.setSize(tokens[1].trim());

line = s.nextLine();
String[] toppings = line.split(",");
p.setToppings(toppings);
pizzas[n++] = p;
}

return n;
}
public static void main(String[] args) {
String filename;
Scanner keyboard = new Scanner(System.in);
Pizza[] pizzas = new Pizza[50]; //max 50 pizzas
int n = 0;
if(args.length == 1) //file name passed as command line?
{
filename = args[0];
}
else
{
System.out.println("Enter input filename: ");
filename = keyboard.next();
}

Scanner file = null;
try {
file = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
System.out.println("Error: " + e.getMessage());
System.exit(1);
}
n = loadFile(file, pizzas);
file.close();
sort(pizzas, n);
double subTotal = 0;
double TAX_RATE = 0.10; // 10 %
System.out.println(" Pizzas sorted in descending order of cost");
for(int i = 0; i < n; i++)
{
subTotal += pizzas[i].getPizzaCost();
System.out.println(pizzas[i] + " ");
}
double tax = subTotal * TAX_RATE;
double total = subTotal + tax;
System.out.printf("Sub Total: $%.2f ",subTotal);
System.out.printf("Tax: $%.2f ", tax);
System.out.printf("Total: $%.2f ", total);
}
}

Sample Input file: pizza.txt
==========
hand-tossed,small
onion,mushroom,tomato
thick,large
chicken, onion
output
======
Enter input filename:
pizza.txt

Pizzas sorted in descending order of cost
Type: thick, Size: large
Toppings: chicken onion
Cost of Base: $14.50
Cost of toppings = $3.00
Pizza cost = $17.50

Type: hand-tossed, Size: small
Toppings: onion mushroom tomato
Cost of Base: $9.50
Cost of toppings = $4.50
Pizza cost = $14.00

Sub Total: $31.50
Tax: $3.15
Total: 34.65

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote