Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class YearlyVisitorsWMH
- {
- public static int numVis; //This variable holds the number of visitors. This number will be assigned to whichever month the user has chosen to assign it to.
- public static int numMonths; //This variable will serve as our counter. It will be incremented every time the user adds data to a month.
- public static int[] monthlyVisitors; //This is the array we will use hold the visitors for each month. Jan=0, Feb=1, etc.
- public YearlyVisitorsWMH() //This instantiates the array and both the visitor count and the counter for months.
- {
- monthlyVisitors = new int[12];
- numVis=0;
- numMonths=0;
- }
- public void setMonthCount(int month,int visitorCount) //This variable gets the number of visitors for a particular month and holds it in the assigned month.
- {
- monthlyVisitors[month]=visitorCount;
- numMonths++;
- }
- public int getMonthCount(int month) //This method returns the current values for the number of visitors in each month.
- {
- return monthlyVisitors[month];
- }
- public void printMonthlyTotals() //This method returns the total number of visitors for each month.
- {
- for(int i=0;i<=numMonths;i++)
- {
- System.out.println(monthlyVisitors[i]);
- }
- }
- public int average() //This method calculates and returns the average
- {
- int total=0;
- int average=0.0;
- for(int i=0;i<=numMonths;i++)
- {
- total+=monthlyVisitors[i];
- }
- average=total/(numMonths);
- return average;
- }
- public int min() //This method determines the minimum number of visitors in a single month that has had data submitted and returns that value.
- {
- int c=-1;
- for(int i=0;i<=numMonths;i++)
- {
- if(monthlyVisitors[i]<c || c==-1)
- {
- if(monthlyVisitors[i]==0)
- {
- boolean t =true; //Boolean variable prevents min from returning a zero in a non-submitted month as a minimum.
- }
- else
- {
- c=monthlyVisitors[i];
- }
- }
- }
- return c;
- }
- public int max() //The following method determines the maximum number of visitors in a single month and returns that value.
- {
- int c=-1;
- for(int i=0;i<=numMonths;i++)
- {
- if(monthlyVisitors[i]>c || c==-1)
- {
- if(monthlyVisitors[i]==0)
- {
- boolean t=true;
- }
- else
- {
- c=monthlyVisitors[i];
- }
- }
- }
- return c;
- }
- }
- import java.util.*; //importing utils
- import java.io.*;
- public class ParkManagerWMH
- {
- public static YearlyVisitorsWMH a = new YearlyVisitorsWMH();
- public static void addMonth() //this method adds visitors to the chosen month
- {
- Scanner input = new Scanner(System.in);
- System.out.println("Please enter the month number. Enter 0 for January, 1 for February, and so on.");
- int month = input.nextInt();
- System.out.println("Please enter the visitor count for the month");
- int visitors = input.nextInt();
- a.setMonthCount(month,visitors);
- }
- public static void getMonth() //This method returns the value of the visitor count for the chosen month
- {
- Scanner input = new Scanner(System.in);
- System.out.println("Please enter the month number.");
- int month = input.nextInt();
- int visitors=a.getMonthCount(month);
- System.out.println("The visitor count for month "+month+" is "+visitors);
- }
- public static void printTotals() //This calls the printTotals method, which displays the total number of visitors for each month
- {
- a.printMonthlyTotals();
- }
- public static void printAvg() //This calls the average method, which calculates and return the average number of visitors per month submitted.
- {
- int average = a.average();
- System.out.println("The yearly visitor average is "+average);
- }
- public static void printMin() //This calls the Min method, which determines the lowest number of visitors in a month submitted and returns that value.
- {
- int min = a.min();
- System.out.println("The minimum number of visitors in a single month is "+min);
- }
- public static void printMax() //This calls the Max method, which determines the highest number of visitors in a month submitted and returns that value.
- {
- int max = a.max();
- System.out.println("The maximum number of visitors in a single month is "+max);
- }
- public static void main (String[] args)
- {
- Scanner input = new Scanner(System.in);
- int choice=0;
- while(choice!=7) //This while loop contains the switch, which has the user's options. Through this, they select what they want to do or end the program.
- {
- System.out.println("1. Add a Monthly Count");
- System.out.println("2. Get a Monthly Count");
- System.out.println("3. Print the monthly totals");
- System.out.println("4. Print the Yearly Average");
- System.out.println("5. Print the Yearly Minimum");
- System.out.println("6. Print the Yearly Maximum");
- System.out.println("7. Exit the menu");
- choice = input.nextInt();
- switch(choice)
- {
- case 1: addMonth();
- break;
- case 2: getMonth();
- break;
- case 3: printTotals();
- break;
- case 4: printAvg();
- break;
- case 5: printMin();
- break;
- case 6: printMax();
- break;
- case 7: choice=7;
- }
- }
- System.exit(0); //This ends the program
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement