2017-10-19 3 views
0

내 프로그램이 거의 완료되었습니다. 하나의 드라이버 클래스, 하나의 피자 클래스 및 주문 가격을 계산하는 주문 클래스입니다. 내 유일한 문제는 피자 클래스의 inputToppings 메서드에서 while 루프를 통해 변수가 설정되고 주문 클래스의 피자 클래스에서 toString 메서드가 호출 될 때 올바른 값이 인쇄되지만 내 getToppingQuantity 메서드는 내 코드를 실행하면 문제가됩니다. 인스턴스 변수에 대한 기본값을 전달합니다. 내가 여기서 어디로 잘못 가고 있니? 여기 변수가 기본값으로 되돌아갑니다.

package edu.ilstu; 
import java.util.Scanner; 

public class Pizza 
    { 

//default pizza is cheese and tomato sauce only 

private int alfredo = 0; 
private int pesto = 0; 
private int tomato = 1; 
private int sausage = 0; 
private int pepperoni = 0; 
private int onion = 0; 
private int mushroom = 0; 
private int greenPepper = 0; 
private int cheese=1; 
private char choice; 

public int getCheese() { 
    return cheese; 
} 
public int getAlfredo() 
{ 
    return alfredo; 
} 
public void setAlfredo() 
{ 
    alfredo=1; 
    pesto=0; 
    tomato=0; 
} 
public int getPesto() 
{ 
    return pesto; 
} 
public void setSausage(int sausage) 
{ 
    this.sausage = sausage; 
} 
public void setPepperoni(int pepperoni) 
{ 
    this.pepperoni = pepperoni; 
} 
public void setOnion(int onion) 
{ 
    this.onion = onion; 
} 
public void setMushroom(int mushroom) 
{ 
    this.mushroom = mushroom; 
} 
public void setGreenPepper(int greenPepper) 
{ 
    this.greenPepper = greenPepper; 
} 
public void setCheese(int cheese) 
{ 
    this.cheese = cheese; 
} 
public void setPesto() 
{ 
    pesto=1; 
    tomato=0; 
    alfredo=0; 
} 
public int getTomato() 
{ 
    return tomato; 
} 
public void setTomato() 
{ 
    tomato=1; 
    pesto=0; 
    alfredo=0; 
} 
public int getSausage() 
{ 
    return sausage; 
} 
public int getPepperoni() 
{ 
    return pepperoni; 
} 
public int getOnion() 
{ 
    return onion; 
} 
public int getMushroom() 
{ 
    return mushroom; 
} 

public int getGreenPepper() 
{ 
    return greenPepper; 
} 
public void inputToppings() { 

    Scanner sc = new Scanner (System.in); 

    while (true) { 

     System.out.println("Input toppings. Enter Q to quit"); 
     System.out.println("1. Sausage"); 
     System.out.println("2. Pepperoni"); 
     System.out.println("3. Onion"); 
     System.out.println("4. Mushroom "); 
     System.out.println("5. Green Pepper"); 
     System.out.println("6. Cheese"); 
     System.out.println("7. Alfredo"); 
     System.out.println("8. Pesto"); 
     System.out.println("9. Tomato"); 

     if (choice == 'q' || choice == 'Q') { 
      break; 
     } 
     if (choice == '1') { 
      addSausage(); 
     } 
     if (choice == '2') { 
      addPepperoni(); 
     } 
     if (choice == '3') { 
      addOnion(); 
     } 
     if (choice == '4') { 
      addMushroom(); 
     } 
     if (choice == '5') { 
      addGreenPepper(); 
     } 
     if (choice == '6') { 
      addCheese(); 
     } 
     if (choice == '7') { 

      if(alfredo != 1) { 
      setAlfredo(); 
      } 
      else 
       System.out.println("No double sauce allowed"); 
     } 
     if (choice == '8'){ 
      if (pesto != 1) { 
       setPesto(); 
      } 
      else 
       System.out.println("No double sauce allowed"); 
     } 
     if (choice == '9') { 
      if(tomato != 1) { 
       setTomato(); 
      } 
      else 
       System.out.println("No double sauce allowed"); 
     } 

     choice = sc.next().charAt(0); 
    }//end while loop 
    sc.close(); 
}//end of inputToppings method 
public void addCheese() { 
    if(cheese<2) { 
     setCheese(cheese+1); 
    } 
    else 
     System.out.println("Invalid input"); 
} 
public void addPepperoni() { 
    if (pepperoni<2) { 
     setPepperoni(pepperoni+1); 
    } 
    else 
     System.out.println("Maximum ammount of topping exceeded"); 
} 
public void addSausage() { 
    if(sausage<2) { 
     setSausage(sausage+1); 
    } 
    else 
     System.out.println("Maximum ammount of topping exceeded"); 
} 
public void addOnion() { 
    if(onion<2) { 
     setOnion(onion+1); 
    } 
    else 
     System.out.println("Maximum ammount of topping exceeded"); 
} 
public void addGreenPepper() { 
    if(greenPepper<2) { 
     setGreenPepper(greenPepper+1); 
    } 
    else 
     System.out.println("Maximum ammount of topping exceeded"); 
} 
public void addMushroom() { 
    if(mushroom<2) { 
     setMushroom(mushroom+1); 
    } 
    else 
     System.out.println("Maximum ammount of topping exceeded"); 
} 
public String toString() { 
    String str ="sausage = " + Integer.toString(sausage) + " Pepperoni = " + Integer.toString(pepperoni) + "\n" + "Onion = " + Integer.toString(onion) + " Mushroom = " + Integer.toString(mushroom) +"\n" + "cheese = " + Integer.toString(cheese) +"\n" + "Tomato = " + Integer.toString(tomato) + " Pesto = " + Integer.toString(pesto) + " Alfredo = " + Integer.toString(alfredo); 
    return str; 
} 
public int getToppingQuantity() { 
    return sausage+pepperoni+onion+mushroom+cheese; 
} 
}//end of class 

은 주문 클래스

public class Order 
{ 
final double TAX_RATE = 0.075; 
final double BASE_PRICE = 5.00; 
final double TOPPING_CHARGE = 0.75; 
final double DELIVERY_CHARGE = 0.10; 
public char typeOfOrder; 
public String storeLocation = ""; 
Pizza pizza1 = new Pizza(); 
Customer cust1 = new Customer(); 

public double calculateSubtotal() { 
    double toppingPrice = (pizza1.getToppingQuantity()*TOPPING_CHARGE); 
    return BASE_PRICE+toppingPrice; 
} 
public double calculateSalesTax() { 
    return calculateSubtotal()*TAX_RATE; 
} 
public double calculateDeliveryCharge() { 
    return (calculateSubtotal() + calculateSalesTax()) * DELIVERY_CHARGE; 
} 
public void displaySummary() { 

    if (typeOfOrder=='d' || typeOfOrder=='D') { 
     System.out.printf("Subtotal $%.2f\n", calculateSubtotal()); 
     System.out.printf("Sales Tax: $%.2f\n", calculateSalesTax()); 
     System.out.printf("Delivery Charge: $%.2f\n", calculateDeliveryCharge()); 
     System.out.printf("Total: $%.2f\n", calculateSubtotal() + calculateSalesTax() + calculateDeliveryCharge()); 
     System.out.println(pizza1.getToppingQuantity()); 
     System.out.println("Thank you, come again!"); 
    } 
    else if (typeOfOrder == 'p' || typeOfOrder == 'P') { 
     System.out.println(storeLocation); 
     System.out.printf("Subtotal $%.2f\n", calculateSubtotal()); 
     System.out.printf("Sales Tax: $%.2f\n", calculateSalesTax()); 
     System.out.printf("Total: $%.2f\n", calculateSubtotal() + calculateSalesTax()); 
     System.out.println("Thank you, come again!"); 
    } 

} 
public char getTypeOfOrder() 
{ 
    return typeOfOrder; 
} 
public void setTypeOfOrder(char typeOfOrder) 
{ 
    this.typeOfOrder = typeOfOrder; 
} 
public String getStoreLocation() 
{ 
    return storeLocation; 
} 
public void setStoreLocation(String storeLocation) 
{ 
    this.storeLocation = storeLocation; 
} 
} 

나는 내 프로그램이 제대로 단계를 통과하지만, 토핑 변수에 대한 잘못된 값을 반환했다

public class PizzaDriver 
{ 

public static void main(String[] args) { 

    Customer cust01= new Customer(); 
    Order order01 = new Order(); 
    Pizza pizza01 = new Pizza(); 

    Scanner sc = new Scanner(System.in); 
    System.out.println("Welcome to ILSTU Pizza"); 
    System.out.println("Step 1: Is this for a pickup or delivery?"); 
    System.out.println("1 Pickup"); 
    System.out.println("2 Delivery"); 
    int choice = sc.nextInt(); 
    System.out.println("Choice: " + choice); 

    if (choice==1) { 

     order01.setTypeOfOrder('p'); 
     System.out.println("1. 207 W. North St, Normal, IL"); 
     System.out.println("2. 208 Landmark Dr, Bloomington, IL"); 
     System.out.println("3. 1600 W. Market St, Bloomington, IL"); 
     System.out.println("Which location would you like to pickup from?"); 
     choice=sc.next().charAt(0); 

     if (choice=='1') { 
      order01.setStoreLocation("207 W. North St, Normal, IL"); 
     } 
     else if (choice=='2') { 
      order01.setStoreLocation("208 Landmark Dr, Bloomington, IL"); 
     } 
     else if (choice=='3') { 
      order01.setStoreLocation("1600 W. Market St, Bloomington, IL"); 
     } 
     else 
      System.out.println("invalid choice"); 

     System.out.println("At this stage would you like to quit this order entry process? \n Enter 'Y' to quit or 'N' to continue on to build your pizza."); 
     choice=sc.next().charAt(0); 
     if (choice=='N'|| choice == 'n') { 
      pizza01.inputToppings(); 
      System.out.println("Your pizza contains the following toppings: \n" + pizza01.toString()); 
      order01.displaySummary(); 
     } 
     else if(choice == 'Y' || choice == 'y') { 
      System.out.println("Thank you, come again!"); 
     } 
    } 

    else if (choice == '2') { 
     //System.out.println("Step 2: Customer Information"); 
     order01.setTypeOfOrder('d'); 
     sc.nextLine(); 
     System.out.println("Please enter your first and last name: \n"); 
     cust01.setFullName(sc.nextLine()); 

     System.out.println("Please enter your street address"); 
     cust01.setAddress(sc.nextLine()); 

     System.out.println("Please enter your city, state and zip code"); 
     cust01.setCityStateZip(sc.nextLine()); 

     System.out.println("Please enter your phone number (10-dights only)"); 
     String userNumber=sc.nextLine(); 
     while(true) { 
      if (userNumber.length()==10) { 
       cust01.setPhoneNumber(userNumber); 
       break; 
      } 
      else if (userNumber.length()!=10) { 
       System.out.println("Invalid phone number, Enter a valid one"); 
      } 
      userNumber=sc.nextLine(); 
     } 
     System.out.println(cust01.toString()+"\n"); 

     System.out.println("At this stage would you like to quit this order entry process? \n Enter 'Y' to quit or 'N' to continue on to build your pizza."); 
     choice=sc.next().charAt(0); 
     if (choice=='N'|| choice == 'n') { 
      pizza01.inputToppings(); 
      System.out.println("\n Your pizza contains the following toppings: \n" + pizza01.toString()); 
      order01.displaySummary(); 
     } 
     else if(choice == 'Y' || choice == 'y') { 
      System.out.println("Thank you, come again!"); 
     } 
    } 
    else 
     System.out.println("invalid choice"); 

} 
} 

처럼 마지막으로 드라이버 클래스 getToppingQuantity 메서드 (참고 : 현재 문제와 관련이 없기 때문에 고객 클래스는 포함하지 않았습니다)

답변

0

Order 클래스에는 Pizza 인스턴스 변수가 있지만 PizzaDriver에는 다른 Pizza 개체를 만들고 사용하고 있습니다. 이것은 아마도 문제의 근본 원인 일 수 있습니다. (당신은 또한 Customer 클래스와 같은 일을하고 있습니다.)

System.out.println("\n Your pizza contains the following toppings: \n" 
+ pizza01.toString()); 

... 그리고 ...

order01.displaySummary(); 

다른 Pizza 객체를 참조 것을 의미한다.

0

inputToppings()의 while (true) 블록은 while 조건을 종료하지 못하게합니다 (종료하려면 break 제외). while (true)는 받아 들일 수없는 프로그래밍 방법이지만, 무한 루프가 될 수 있습니다. 부울 플래그를 제공하는 것이 더 좋으며, 플래그의 값이 변경되면 종료하십시오.

관련 문제