/*********************************************** The school cafeteria sells food to about 1000 students each day. They have a program to record the amount paid for each transaction. The result is a text file on the disk containing all the numbers paid each day. ************************************************/
import java.io.*;
public class PricesSaver
{
double[] nums = new double[10000];
int count = 0; // number of items in NUMS
public PricesSaver()
{
double price = 0;
while(price >= 0) // type -1 at the end of the day
{
price = Double.parseDouble( input("Price") );
if(price >= 0)
{
nums[count] = price;
count = count+1;
}
}
saveNumbers();
stats();
}
void saveNumbers()
{
try
{
BufferedWriter file = new BufferedWriter(
new PrintWriter("prices.dat")
);
for(int c=0; c < count; c = c+1)
{
file.write(nums[c]+"\n");
}
file.close();
}
catch(IOException ex)
{ System.out.println(ex.toString()); }
}
void stats()
{
double sum = 0;
for(int c=0; c < count; c = c+1)
{
sum = sum + nums[c];
}
System.out.println("Total = " + sum);
System.out.println("Average = " + sum / count);
}
public static void main(String[] args)
{ new PricesSaver(); }
public String input(String prompt)
{ return javax.swing.JOptionPane.showInputDialog(null,prompt); }
public void output(String message)
{ javax.swing.JOptionPane.showMessageDialog(null,message); }
}
|