mardi 22 octobre 2019

I am using Java, how should my code look?

I need help with multiple things in my code. They have comments next to them that say "This should". 1) How do I get the laptop name that the user has chosen to appear using format specifiers? i.e "%s". 2)How do I print the order summary when the if the statement is false?

The code is incomplete, I am not sure how to finish it. I will attach what the output is supposed to look like.

public static void main(String[] args) { //Begin main()

char cont='Y';
int qty=0;
int trigger=0;
int choice=0;
double total=0;
double subtotal=0;
double tax=0;
double price=0;
double lineItem=0;
String orderSummary=" ";
String laptop=" ";
Scanner input = new Scanner(System.in);
Calendar dateTime = Calendar.getInstance();

/**
 * Using a while loop, display a list of laptops & prices and 
 * prompt user for their choice.
 */
while (cont == 'Y') {  
System.out.printf("%nTOP LAPTOPS OF %tY"
                    + "%n%n1. %-23s %7s $%,9.2f"
                    + "%n2. %-23s %8s %,9.2f"
                    + "%n3. %-23s %8s %,9.2f"
                    + "%n4. %-23s %8s %,9.2f"
                    + "%n5. %-23s %8s %,9.2f"
                    + "%n%nEnter your choice: ",
                  dateTime,
                  "HP Envy 13", " ", 799.99, 
                  "Asus ZenBook 13 UX333FA", " ", 849.99,
                  "Dell XPS 13", " ", 989.99,
                  "Alienware Area 51-m", " ", 1999.99,
                  "Razer Blade Stealth", " ", 1299.00);
choice = input.nextInt();

if (choice > 0 && choice < 6) { //if-else to figure out the laptop name/price.
  System.out.printf("Enter the quantity for the "); //This should print the laptop type at the end.
  qty = input.nextInt();
}
else {
  System.out.printf("%nInvalid choice! Try again.%n%n");
  System.out.printf("Enter 'Y' to add a laptop to your purchase or 'N' to exit: ");
  cont = input.next().charAt(0);
  cont = Character.toUpperCase(cont);
}
if (cont == 'Y') { //if, nested if to figure out when the choice is valid to proceed.
  continue;   
}
if(cont != 'Y') {
  System.out.print(orderSummary); //This should print the orderSummary as shown in the example.
}





 //The following if-else prints a $ sign for the first line item
if(trigger == 1)
{
  orderSummary += String.format("%n%, -9d %-30s %8s $%,17.2f",
                                qty, laptop, " ", lineItem);
  trigger = 0;
} //End else for no $ sign



/**The following statement prints the order summary of the laptops purchased.
  * This should be done when there are no more laptops to process.
  */
if (choice > 0) {
  if (choice < 6) {
    tax = subtotal * .0825;
    total = subtotal + tax;

    orderSummary += String.format("%n%n%34s Subtotal %6s %,17.2f"
                                    + "%n%31s Tax @ 8.25%% %6s %,17.2f"
                                    + "%n%n%37s TOTAL %5s $%,17.2f%n",
                                  " ", " ", subtotal,
                                  " ", " ", tax,
                                  " ", " ", total);
    System.out.printf("%s", orderSummary);
    break;

  } //End if valid choice range ends print order summary  
} //End if valid choice range begins

 }
} 
}

Instructions/Output pg1

Instructions/Output pg2

Instructions/Output pg3

Instructions/Output pg4

Aucun commentaire:

Enregistrer un commentaire