Display bill total, Java public void display() error -
i'm having trouble executing total bill display. asked professor gave me small block of code, modified meet needs, reason not executing , has error:
illegal start expression line --> public void display().
the compiler suggests end semi colon don't believe accurate.
what missing public void display()
not being executed , erroneous?
import java.util.scanner; public class coffeeshop { /* author: date: program: create coffee shop application uses while loop build customer order. coffee shops sells coffee ($3.25), espresso ($4.25), , tea ($2.75). coffee selection presents customer choices of iced (no charge), cream (50 cents), , sugar (50 cents). espresso selection presents customer choice of caramel (no charge) , chocolate (no charge) 1 shot (no charge) or 2 shots ($1.25) of espresso. once customer done, or receive bill of total price. after each selection, customer have choice of returning main menu additional purchases. use nested loops handle customers' submenu selections. */ public static void main(string[] args) { //declarations double coff = 3.25; double esp = 4.25; double tea = 2.75; double cream = .50; double sugar = .50; double dblshot = 1.25; int dblshotqty = 0; int userinput = 0; int userinput2 = 0; int coffqty = 0; int espqty = 0; int teaqty = 0; int creamqty = 0; int sugarqty = 0; double runtotal = 0; double totalcoff = 0; double totalesp = 0; double totaltea = 0; double totalbill = 0; scanner scan = new scanner(system.in); system.out.print("would place order? press 1 yes or 2 no :"); //start loop control variable asking if cup of coffee yes or no userinput = scan.nextint(); while(userinput == 1) { system.out.print("enter 1 coffee, 2 espresso, or 3 tea: "); userinput2 = scan.nextint(); switch(userinput2) //if 1 pressed coffee ordered { // open switch case '1': { coffqty = coffqty + 1; system.out.print("press 1 if coffee iced or 2 no: "); userinput = scan.nextint(); } { system.out.print("press 1 if cream $.50 or 2 no: "); userinput = scan.nextint(); if ( userinput == 1 ) { creamqty = creamqty + 1; } } { system.out.print("press 1 if sugar $.50 or 2 no: "); userinput = scan.nextint(); if ( userinput == 1 ) { sugarqty = sugarqty + 1; } }//end case 1 break; // espresso ordered ask double shot case '2': { espqty = espqty +1; system.out.println("press 1 double shot $1.25 or 2 no: "); userinput = scan.nextint(); if(userinput == 1) { dblshotqty = dblshotqty +1; } }//end case 2 break; //tea ordered case '3': { teaqty = teaqty + 1; system.out.println("you have selected tea! great choice."); }//end case 3 }//end switch // create output display total bill adding totals public void display() { double totalcoff = coffqty * coff + cream * creamqty + sugar * sugarqty; double totalesp = espqty * esp + dblshot * dblshotqty; double totaltea = teaqty * tea; system.out.println("order: \n "+coffqty + " coffee" + "\n "+creamqty +" cream" + "\n "+sugarqty + " sugar" + "\ntotal coffee: "+ totalcoff); system.out.println(" "+teaqty + " tea" + "\ntotal tea: "+ totaltea); system.out.println(" "+espqty + " espresso" + "\n "+dblshotqty +" double shot" + "\ntotal espresso: "+ totalesp); double totalbill = totalcoff+totalesp+totaltea; system.out.println("\ntotal drink order: "+totalbill); } break; } // end while } } // end of class
your code structure making things difficult you. mentioned @carcigenicate, (nice name btw), have declared method inside main method. once change that, you'll notice display
method shows compiler errors. because variables referenced in display
no longer visible. have few options:
- rethink design of class i.e methods should exist , how called.
- take display method outside main method , make variables member variables visible methods need. read effects of using member variables first.
- remove display method , move logic main method.
Comments
Post a Comment