/*********************************************** 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.*;
float[] nums = new float[10000]; int count = 0; // number of items in NUMS
void setup() { float price = 0; while(price >= 0) // type -1 at the end of the day { price = float( input("Price") ); if(price >= 0) { nums[count] = price; count = count+1; } } saveNumbers(); stats(); }
void saveNumbers() { PrintWriter file = createWriter("prices.dat"); for(int c=0; c < count; c = c+1) { file.println(nums[c]); } file.close(); }
void stats() { float sum = 0; for(int c=0; c < count; c = c+1) { sum = sum + nums[c]; } println("Total = " + sum); println("Average = " + sum / count); }
public String input(String prompt) { return javax.swing.JOptionPane.showInputDialog(null,prompt); } public void output(String message) { javax.swing.JOptionPane.showMessageDialog(null,message); }
|