Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through the days of the week

Tags:

java

I was wondering if someone could tell me if it's possible to loop through the days of the week after 6 inputs of product or/and 6 inputs of quantity are input. So basically the program displays Monday and then loops 6 times and then goes to Tuesday loops 6 times and then Wednesday. If it is possible could I get a few ideas??? I would really appreciate some help. I have googled with no luck and that's because I know i'm not asking google the right question.

 import java.util.Scanner;

      public class Mailorder {

         public static void main(String[] args) {

         //create a scanner
         Scanner input = new Scanner(System.in);

         //declare variables

         double product1 = 3.75;
         double product2 = 5.95;
         double product3 = 8.75;
         double product4 = 6.92;
         double product5 = 8.75;
         double product6 = 7.87;
         double sum2 = 0;
         int sum1 = 0;
         double total = 0.00;
         int product;
         int quantity;

         //Monday
         System.out.print("Monday");
         System.out.println();

         //read in product # 
         System.out.print("Enter a product number: ");
         product = input.nextInt();

         //read in quantity sold
         System.out.print("Enter quantity sold: ");
         quantity = input.nextInt();


         //keep reading data until the input is 0
         while (quantity != -1) {
                 sum1 += quantity;

         //switch case
         switch (product) 
        {
         case 1: total = product1 * quantity; break;
         case 2: total = product2 * quantity; break;
         case 3: total = product3 * quantity; break;
         case 4: total = product4 * quantity; break;
         case 5: total = product5 * quantity; break;
         case 6: total = product6 * quantity; break;
        }

       sum2 +=total;

       //read the next data 
       System.out.print("Enter a product number: ");
             product = input.nextInt();

       System.out.print("Enter quantity sold: ");
             quantity = input.nextInt();

        }   
      //print results
      System.out.println("The total retail value of all products sold last week $" + sum2);

    }
  }
like image 912
jake Avatar asked Mar 14 '23 06:03

jake


1 Answers

DayOfWeek enum

The DayOfWeek enum defines seven objects, one for each day of the week, numbered 1-7 for Monday-Sunday.

An EnumSet is a highly optimized implementation of Set for collecting enum objects.

EnumSet<DayOfWeek> dows = EnumSet.allOf( DayOfWeek.class );  // Collect all seven day-of-week objects, in order Monday-Sunday.

Loop that set.

for( DayOfWeek dow : dows ) {
    String dowName = dow.getDisplayName( TextStyle.FULL , Locale.US ) ;  // Generate localized string for the name of the day-of-week.
    System.out.println( dowName ) ;  
    … // Ask for product code and do your math
}
like image 71
Basil Bourque Avatar answered Mar 24 '23 21:03

Basil Bourque