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
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();
}
}
}
Comments
Leave a comment