Товарный чек, несколько вариантов выбора (если / еще), а l oop, возможность клиента изменить выбор - PullRequest
0 голосов
/ 16 марта 2020

Ни один из других постов, казалось, не подходил соответственно. Я действительно борюсь с созданием этой программы без ошибок. Ниже я привел это, однако, если вы запустите программу, она не будет должным образом зациклена, и если вы выберете одну и ту же конфету, дважды математика не добавится к предыдущему выбору.

Для второй части Мне нужно перекодировать, чтобы настроить так, чтобы клиент мог изменить текущие выборы, если они хотели бы. Итак, мне нужно, чтобы первая часть работала идеально.

import java.util.Scanner; //Program uses Scanner
import java.util.Calendar; //Program uses calendar

public class ClairAndreaG005PA2
{//Begin class ClairAndreaG005PA2

 public static void main(String[] args)
  {
   /**Begin method. The customer is asked whether to proceed with a candy purchase.
   * As long as the candy choice is in the proper range, prompt for the quantity,
   * calculate the item total and subtotal; otherwise, print an error message. If it’s 
   * the first item in the purchase, ass it to the sales, a receipt with a $ sign for the
   * item total. If not format the item without a $ sign for the item total 
   * and add it to the sales receipt. Start the process again when the customer wants
   * to add another candy purchase. When the customer is done, the sales tax and the
   * total calculated, the sales receipt is finalized and printed.
   */

    int quantity = 0,
       formatFirstItem = 1,
       choice = 0;


    final double TAX_RATE = .0825; //tax rate declared

    double price = 0,
      itemTotal = 0,
      subtotal = 0,
      taxAmount = 0,
      total = 0;

    char proceed = ' '; //proceed variable for loop

    String candy = new String();

    Calendar dateTime = Calendar.getInstance(); //situational date and time

    Scanner input = new Scanner(System.in);

     String salesReceipt = String.format("%n%nFAIRYTALE SWEETS%n"
                                      + "North Star Mall%n"
                                      + "San Antonio, TX%n"
                                      + "Date: %n", dateTime
                                      + "Time: %n", dateTime);

    //Initiating variables and calculations

    System.out.printf("%nDo you want to proceed with your candy purhase? \'Y\' or \'N\': ");
    proceed = input.nextLine().charAt(0);
    //End Prompt 1

    while(Character.toUpperCase(proceed) == 'Y')
    {
      System.out.printf("%nFAIRYTALE SWEETS"
                    + "%n%n1. Arabian Nights Chocolate Coins - 1 lb. Bag %5s%,7.2f"
                    +"%n2. Beauty and the Beast Lollipops - 1 lb. Bag %,12.2f"
                    +"%n3. Mad Hatter Jelly Beans - 1 lb. Bag %,20.2f"
                    +"%n4. Pinocchio's Candy Cones - Each  %,23.2f"
                    +"%n5. Sleeping Beauty Caramel Apples - Each %,17.2f"
                    +"%n%nEnter your choice: ", "$", 2.25, 2.50, 1.75, 0.75, 1.25);
              choice = input.nextInt();
     //Prompt 2 Candy Menu

     if(choice > 0)
     {if(choice < 6)
        {if(choice == 1)
          {candy = "Arabian Nights Chocolate Coins";
              price = 2.25;
          }
          else
          {if(choice == 2)
            { candy = "Beauty and the Beast Lollipops";
                price = 2.50;
            }
            else
            {if(choice == 3)
              {candy = "Mad Hatter Jelly Beans";
                  price = 1.75;
              }
              else
              {if(choice == 4 )
                {candy = "Pinocchio's Candy Cones";
                    price = 0.75;
                }
                else
                {candy = "Sleeping Beauty Caramel Apples";
                    price = 0.75;
                 }
               }
             }
          }
          System.out.printf("%nQuantity for %s: ", candy);
    quantity = input.nextInt();

    //quantity of selections
     }
     else
      { System.out.println("Invalid candy choice! Try again.");
     }

     //Choices and selections

    itemTotal = quantity * price; //calculation 1
    subtotal += itemTotal; //calculation 2

    System.out.printf("%nWould you like to make another candy purchase? \'Y\' or \'N\': ");
         proceed = input.next().charAt(0);

         if(formatFirstItem == 1 )
         {
           salesReceipt += String.format("%n%s"
                                    + "%n      %d @ $%.2f ex. %-24s $%,10.2f%n", candy,
                                  quantity, price, " ", itemTotal);
           formatFirstItem = 0;
         }
         else
         {
           salesReceipt += String.format("%s"
                                    + "%n      %d @ $%.2f ea. %-25s %,10.2f%n", candy,
                                  quantity, price, " ", itemTotal);

         }
  //End if FormatFIrst Item is 1 or else formatFirstItem NOT 1
    }
    }//End while loop selection


    taxAmount = TAX_RATE * subtotal;// calculation 3
    total = taxAmount + subtotal; // calculation 4

    salesReceipt += String.format("%n%36s %-6s $%,10.2f"
                                + "%n%36s %-7s %,10.2f"
                                + "%n%n%36s %-6s $%,10.2f%n", "SUBTOTAL:   ", " ",
                              subtotal, "TAX @ 8.250%:  ", " ", taxAmount,
                              "TOTAL:  ", " ", total);
    System.out.printf("%s", salesReceipt); 
      }
}

Дайте мне знать!

...