Answer to Question #292726 in Java | JSP | JSF for User12345

Question #292726

Build app for icecream parlor using file handling to record daily sales. Icecream parlor offers 10 flavours icecreams. Each has different price per scoop. Different flavours rates, description and rate/scoop are stored in file named flavors.txt. Also for each flavor sugarfree version also exists. It has same flavor and description but rate is 20% more than original per scoop price . There is no separate file or record for sugar free types. Build an interface by yourself.

A sale is done by selecting flavor from the list. Customer specifies the number of scoops & bill is calculated. If sugar free checkbox is selected then price is recalculated as mentioned above and bill is adjusted accordingly. Upon finishing a sale, sale record is saved in file named sales.txt. This file will contain record of all the sales of the day. Also create a separate file named total.txt. This file will contain just one number which is grand total of all the sales done


1
Expert's answer
2022-02-01T16:25:55-0500
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
    public static String[] flavors;
    public static double[] prices;

    public static void readFlavors() {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("flavors.txt")))) {
            flavors = new String[10];
            prices = new double[10];
            int i = 0;
            String line;
            while ((line = reader.readLine()) != null) {
                String[] data = line.split(";");
                flavors[i] = data[0];
                prices[i++] = Double.parseDouble(data[1]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        readFlavors();
        StringBuilder sales = new StringBuilder();
        double total = 0;
        while (true) {
            System.out.println();
            for (int i = 0; i < flavors.length; i++) {
                System.out.println(i + 1 + ". " + flavors[i] + " $" + prices[i] + "/scoop");
            }
            System.out.println("0. Exit");
            System.out.println("Flavor:");
            int index = Integer.parseInt(in.nextLine());
            if (index == 0) {
                break;
            }
            System.out.println("How many scoops:");
            int scoops = Integer.parseInt(in.nextLine());
            System.out.println("Sugar-free(+20%) Y|N:");
            String sugarFree = in.nextLine();
            double cost = scoops * prices[index - 1];
            if (sugarFree.equalsIgnoreCase("Y")) {
                cost *= 1.2;
            }
            String bill = flavors[index - 1] + " $" + prices[index - 1] + " " + scoops + " scoops Total: $" + cost;
            System.out.println(bill);
            sales.append(bill).append('\n');
            total += cost;
        }
        try (PrintWriter writer = new PrintWriter("sales.txt")) {
            writer.println(sales);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (PrintWriter writer = new PrintWriter("total.txt")) {
            writer.println(total);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS