Assignment Details:

The context for this assignment (Part 1 and Part 2) is a 'Milk Consumption App' for calculating and displaying milk consumption of a given household. This assignment will test a students knowledge of and skills in writing application software for a particular task, understanding the business rules of a particular problem, coding these in a computer program, developing a graphical user interface.

For this assignment, students will use the Java programming language and development will be on the NetBeans IDE platform.

This assignment consists of 2 stages,

  • Part 1: A simple console program (no GUI)
  • Part 2: The same but wrapped in a GUI

Milk Consumption App

Preamble: A milk provider business owner has approached you with a request to write a software application to track the milk usage of their clients.

Following data are gathered for each filling;

  • Amount of milk purchased (x litres) [float]
  • Price of milk (y cents/litre) [integer]
  • Number of days the household could utilize the milk (z days) [float]

The client requires the application to track the last five such household fillings of milk and alert if the average cost goes above the threshold as per the following table. (Important: The value of threshold is as per the last two digits of student ID from the following table.)

Threshold Above Last 2 Digits of your Student ID
1) 114.05 cents 00 to 09
2) 114.10 cents 10 to 19
3) 114.15 cents 20 to 29
4) 114.20 cents 30 to 39
5) 114.25 cents 40 to 49
6) 114.30 cents 50 to 59
7) 114.35 cents 60 to 69
8) 114.40 cents 70 to 79
9) 114.45 80 to 90
10) 114.50 91 to 99

A Worked example: Let's calculate the average milk consumption for a single filling. Using the notation above;

The average milk consumption (afc1) = x/z (liters per day)
The average cost (ac1) = (x/z)*y (cents per day)

Then for five households;

The overall Average Milk Consumption (afc) = {afc1+ afc2+...+ afc5}/5
The overall Average Cost (ac) = {ac1+ ac2+...+ ac5}/5

You'll also need to check if ac exceeds client set threshold warning.

Test Cases

When designing and implementing software applications, it is always important to first work out how to test the program(s) and what data to use, then to code. You need to identify at least 5 test cases. You need to design and systematically test the two different conditions of average cost warning.

Part 1:

Write a simple Java program to calculate the average milk consumption and the average cost for a single household filling. Then extend this to include five households and calculate the overall average milk consumption (afc) and average cost (ac) for the five households. You'll also need to warn the user if the average cost is equal to higher than the threshold.

In Part 1, you will be developing a Java program without a GUI. Input and output are via the console.

When the program is executed, the user will be asked to input for five consecutive households:

  • Amount of milk purchased (in litres)
  • Price of ONE litre of milk (in cents)
  • The number of days after the filling (in days)

Then the program should display afc and ac for the data and indicate if the ac is below the warning level.

Hint/Steps for Part 1:

1. Think about your strategy for how you will calculate the afcn and ac_n for each filling. And then how you work out the overall afc and ac using these calculations. Think about writing simple functions/methods for the repetitive calculations.

2. Add a simple Java class named Part1. Do not add a GUI.

3. Declare and instantiate the variables that you will need for Part 1. You will also need several ArrayLists to hold the milk amount, price, and number of days, at the start of the main method, for example (you'll need to add additional ArraysLists as well): Make use of Generics to define your Collection Framework classes. i.e. ArrayList

4. Add code to read in the data from the user for the five households. A recommended way is to use the Scanner class.

5. Now add the code that implements your strategy of calculating the afc and ac.

6. Finally, add three System.out.println() statements that state the overall afc, ac and the warning message if ac exceeds the threshold.

7. Test your implementation with the test cases mentioned above (and additionally your own).

Part 2:

Design and implement a Java Swing GUI application (Java Swing components) that provides an easy to use interface.

This GUI application must allow a user to input the various data elements. To this end, the user can

  • input the data using Java Swing GUI elements, e.g. text fields, radio buttons, drop down lists, etc.
  • Click on a Calculate button that starts the calculation of the final results, and
  • An Exit or Quit button to properly close the program.

Use the same code for the calculations from Part 1. Reusing code is an important part of software development.

You have a great degree of freedom in what GUI elements you choose and how you would like to design the layout of your GUI.

Notes:

  • For this assignment, you do not have to do any checking for invalid user input (you may of course, if you want to), for example you do not have to check whether the user has typed in a negative number or a zero as discussed earlier.
  • Your GUI should update the output labels "Average milk Consumption , Average Cost" and another label to show a warning if the average cost is higher when the Calculate button has been clicked. If Average Cost is greater than threshold then display the warning message in Red colour.

Hint/Steps for Part 2:

1. Draw a sketch of your graphical user interface on paper. What Java Swing components will you use? Where will you place them? The above GUI is just an example. You are free to create your own GUI.

2. Add all the components for your GUI. Use Java Swing components like JFrame, JLabel, JTextField, JButton.

3. Add event handlers (ActionListeners) for your buttons, radio buttons, check boxes, etc.

4. Add the code that does the actions, e.g. that does the calculation for afc and ac. Reuse your code for the calculation Part 1 by copying and pasting it from the Part1main method into the Calculate button's event handler.

5. Test your application. Run it as a Java Application. Enter the test cases listed above and make sure your code adheres to the Java style guide. You need to manually add more comments than the automatically generated comments.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.