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

For this program you must used the attached file to do this assignment. The prog

ID: 3732533 • Letter: F

Question

For this program you must used the attached file to do this assignment. The program will read in the information from the GroceryList.txt file.

If the price of the item is below $10, the program will print "name Of Item' is cheap.

If the price is greater than $10 but less than $25, the program will print "Name Of Item is affordable."

If the price of the item is above $25, then the program will print "name Of Item" is expensive!

Note all program outputs must be to both the screen and an output file called affordability.txt.

Finally, the program must print the average price of all items.

bread 2.99

eggs 1.99

milk 4.00

cheese 2.48

chicken 5.54

tomatoes 8.99

salmon 12.00

fish 20.00

lamb 50.00

steaks 32.44

apples 6.00

carrots 8.99

broccoli 21.34

beef 25.22

oranges 30.00

Explanation / Answer

GroceryListTest.java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class GroceryListTest {

public static void main(String[] args) throws FileNotFoundException {

File input = new File("D:\GroceryList.txt");

File output = new File("D:\affordability.txt");

PrintWriter pw = new PrintWriter(output);

if(input.exists()) {

double total = 0;

int count = 0;

Scanner inputFile = new Scanner(input);

while(inputFile.hasNextLine()) {

String name = inputFile.next();

double price = inputFile.nextDouble();

total += price;

count++;

if(price<10) {

System.out.println(name+" is cheap");

pw.write(name+" is cheap ");

} else if(price >=10 && price < 25) {

System.out.println(name+" is affordable");

pw.write(name+" is affordable ");

} else {

System.out.println(name+" is expensive!");

pw.write(name+" is expensive! ");

}

}

System.out.println("Average price: "+(total/count));

pw.write("Average price: "+(total/count)+" ");

pw.flush();

pw.close();

inputFile.close();

System.out.println("File has been generated");

} else {

System.out.println("Input file does not exist");

}

}

}

Output:

bread is cheap
eggs is cheap
milk is cheap
cheese is cheap
chicken is cheap
tomatoes is cheap
salmon is affordable
fish is affordable
lamb is expensive!
steaks is expensive!
apples is cheap
carrots is cheap
broccoli is affordable
beef is expensive!
oranges is expensive!
Average price: 15.465333333333335
File has been generated

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