2013-09-28 3 views
0

오케이, 잠시 후 질문을 올렸습니다. 여기있어. 문자열을 사용하여 if 문을 사용하여 비용을 얻으려고합니다. 내가하는 일에 상관없이 내 비용은 0으로 반환됩니다. 클래스 CarRental, getCost 메소드를 참조하십시오. 나는 그것을 바꿀 수 있고 스위치를 사용할 수 있다고 들었는데 괜찮습니다.하지만 지식을 위해서 내가하는 일이 어째서 작동하지 않는지 이해하고 싶습니다.문자열을 사용하는 If 문

업데이트 : 스위치로 변경하려고했는데 동일한 결과가 나타납니다.

import java.util.*; 

public class UseCarRental { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Thank you for choosing ICT car Rentals\n" 
       + "Pleae enter your full name:"); 
     String renterName = input.nextLine(); 
     System.out.println("Please enter your zip code:"); 
     int renterZipcode = input.nextInt(); 
     input.nextLine(); 
     System.out.println("Please enter the size car you would like:\n" 
       + "economy\n" 
       + "midsize\n" 
       + "fullsize\n" 
       + "luxury?"); 
     String carSize = input.next(); 
     System.out.println("How many days do you wish to rent this?"); 
     int rentalDays = input.nextInt(); 

     if (carSize.equals("luxury")) { 
      System.out.println("Will you be wanting a chauffer (y or n"); 
      String chauffer = input.next(); 
      LuxuryCarRental rentIt = new LuxuryCarRental(renterName, 
      renterZipcode, carSize, rentalDays,chauffer); 
      rentIt.display(); 
     } else { 
      CarRental rentIt = new CarRental(renterName, renterZipcode, 
      carSize, rentalDays); 
      rentIt.display(); 
     } 




    } // end main method 
} //end class UseCarRental 

class CarRental { 

    private int days; 
    private int zip; 
    private double cost; 
    private String size; 
    private double total; 
    private String name; 

     CarRental(String renterName, int renterZipcode, String carSize, int rentalDays){ 
      this.days = rentalDays; 
      this.zip = renterZipcode; 
      this.name = renterName; 
      this.size = carSize; 
     } 

     double getCost(){ 
      if(size.equals("economy")){ 
       cost = 29.99; 
      } 
      if(size.equals("midsize")){ 
       cost = 38.99; 
      } 
      if(size.equals("fullsize")){ 
       cost = 43.50; 
      } 
      return cost; 
     } 

     void display(){ 
      System.out.println("Thank you for using our service."); 
      System.out.println("Your order is as follows:"); 
      System.out.println("Name: " + name); 
      System.out.println("Zip code: " + zip); 
      System.out.println("Car size: " + size); 
      System.out.println("Cost per day: " + cost); 
      System.out.println("Days requested: " + days); 
      total = days * cost; 
      System.out.println("Total cost: " + total); 
      System.out.println("If any of the above information is incorrect, too bad bud, because it isn't."); 
     } 

} 

class LuxuryCarRental extends CarRental { 

    private int chauffer = 200; 
    private int days; 
    private int zip; 
    private double cost; 
    private String size; 
    private double total; 
    private String name; 

    LuxuryCarRental(String renterName, int renterZipcode, String carSize, int rentalDays, String chauffer){ 
     super(renterName, renterZipcode, carSize, rentalDays); 
     this.days = rentalDays; 
     this.zip = renterZipcode; 
     this.name = renterName; 
     this.size = carSize; 
    } 

    @Override 
    void display(){ 
      System.out.println("Thank you for using our service."); 
      System.out.println("Your order is as follows:"); 
      System.out.println("Name: " + name); 
      System.out.println("Zip code: " + zip); 
      System.out.println("Car size: Luxury"); 
      System.out.println("Cost per day: " + cost); 
      System.out.println("Days requested: " + days); 
      System.out.println("Chauffer cost: " + chauffer); 
      total = days * cost + chauffer; 
      System.out.println("Total cost: " + total); 
      System.out.println("If any of the above information is incorrect, too bad bud, because it isn't."); 
     } 
} 
+0

스위치로 변경해도 문제가 해결되지 않았 음을 확인할 수 있습니다. 표시 할 때 여전히 비용이 0입니다. 다른 문제가있을 것입니다 만, 나는 그것이 무엇인지 알 수 없습니다. –

+0

"원하는 크기의 자동차를 입력하십시오."귀하의 의견은 무엇입니까? 같은 원인을 가진 다른 질문을하기 전에 모든 의견과 대답을 읽으십시오. – porfiriopartida

+0

힌트 : 코드의 여러 위치에 값을 출력하기 위해'System.out.println'에 대한 호출을 삽입하십시오. 예를 들어, 첫 번째'if' 문 바로 앞에 하나를 삽입하여 "size"를 출력하십시오 : System.out.println ("Size ="+ size);'. –

답변

1

코드에서 getCost 메소드를 호출하지 않습니다.

1

display 메서드는 LuxuryCarRental에있는 cost이라는 인스턴스 변수에서 "하루당 비용"을 받고 있습니다. 어떤 코드도 그 필드에 아무 것도 지정하지 않으므로 (당연히!) 항상 0입니다.

+0

프로그램을 실행하면 다른 세 가지 자동차 크기 중 하나를 선택하더라도 0이됩니다. –

+0

@ E.Watson - 내 대답은 그것을 설명합니다. 필드가 항상 0을 포함하기 때문에 0이됩니다. 당신이 아무 것도 지정하지 않았기 때문에 항상 0을 포함합니다. 당신의'getCost()'메소드는 당신이 * 그것을 사용하지 않으면 무관하다. –