2012-04-09 4 views
1

나는 여행 카드를 모델로 할 일련의 클래스를 작성하려고합니다. 한 클래스에서 다른 클래스로 멤버에 액세스하는 데 문제가 있습니다. WmBusPass 클래스는 현재 컴파일되지 않으며 "심볼을 찾을 수 없습니다 - 생성자 Journey (int, int, java.lang.String, int)"오류 메시지를 제공합니다. 더 일반적으로 나는 수업 구성에있어서 최선의 접근법에 대해 확신 할 수 없다. Journey 클래스를 WmBusPass의 내부 클래스로 만들겠습니까? 또한 클래스를 패키지로 묶어서 조직적인 문제를 해결하려고했지만 패키지의 역학을 완전히 이해하지 못하고 나를 혼란에 빠지게했습니다.범위와 Java의 패키지 문제

WmBusPass.java

package buscard; 
import java.util.ArrayList; 
public class WmBusPass implements BusPass{ 
    private String ID; 
    private String Name; 
    private String Address; 
    private double Balance; 
    public static ArrayList<Payment> payArray; 
    public static ArrayList<Journey> jArray; 
    public static int weekOfYear = 0; 
    public static int monthOfYear = 0; 
    public static int dayOfYear = 0; 

    public WmBusPass(String ID,String Name, String Address, ArrayList<Payment> payArray, ArrayList<Journey> jArray){                                     
     this.ID = ID; 
     this.Name = Name; 
     this.Address = Address; 
     Balance = 0; 
     this.payArray = payArray; 
     this.jArray = jArray; 
    } 
    public static void main(String [ ] args){ 

    }  
    public String getID(){ 
     return ID; 
    }  
    public String getName(){ 
     return Name; 
    } 
    public void setName(String Name){ 
     this.Name = Name; 
    } 
    public String getAddress(){ 
     return Address; 
    } 
    public void setAddress(String Address){ 
     this.Address = Address; 
    } 
    public double getBalance(){ 
     return Balance; 
    } 
    public static ArrayList<Journey> getjArray(){ 
     return jArray; 
    } 

    public void payment(int date1, double amount1){ 
     Payment thisPay = new Payment(date1, amount1); 
     Balance = Balance + amount1; 
     payArray.add(thisPay); 
    } 

    public int getLastPaymentDate(){ 
     Payment lastOne = payArray.get(payArray.size()-1); 
     return lastOne.date; 
    } 
    public boolean journey(int date, int time, String busNumber, int journeyType){ 
     Journey thisJourney = new Journey(date, time, busNumber, journeyType); 

     if (thisJourney.isInSequence(date, time) == false) 
     { 
      return false;    
     } 
     else 
     { 
      Journey.updateCurrentCharges(date); 

      thisJourney.costOfJourney = journeyCost(thisJourney); 
      Journey.dayCharge = Journey.dayCharge + thisJourney.costOfJourney; 
      Journey.weekCharge = Journey.weekCharge + thisJourney.costOfJourney; 
      Journey.monthCharge = Journey.monthCharge + thisJourney.costOfJourney; 

      Balance = Balance - thisJourney.costOfJourney; 

     } 

    } 
    public boolean isInSequence(int date, int time){ 
     Journey prevJourney = jArray.get(jArray.size()-1); 
     return((prevJourney.date < date)||(prevJourney.date = date && prevJourney.time < time));      
    } 
} 

Journey.java

package buscard; 
import java.util.ArrayList; 
class Journey 
{ 
    public int date; 
    public double time; 
    public int busNumber; 
    public int journeyType; 
    public static double dayCharge = 0; 
    public static final double maxDayCharge = 3.50; 
    public static double weekCharge = 0; 
    public static final double maxWeekCharge = 15; 
    public static double monthCharge = 0; 
    public static final double maxMonthCharge = 48; 
    private int journeyNumber; 
    private static int numberOfJourneys = 0; 
    private double costOfJourney; 

    public Journey(int date, double time, int busNumber, int journeyType) 
    { 
     this.date = date; 
     this.time = time; 
     this.busNumber = busNumber; 
     this.journeyType = journeyType;  
     journeyNumber = ++numberOfJourneys; 
    } 
    public int getDate(){ 
     return date; 
    } 
    public double getTime(){ 
     return time; 
    } 
    public int getBusNumber(){ 
     return busNumber; 
    } 
    public boolean isInSequence(int date, int time){ 
     ArrayList<Journey> jArray = WmBusPass.getjArray(); 
     Journey prevJourney = jArray.get(jArray.size()-1); 
     return((prevJourney.date < date)||(prevJourney.date == date && prevJourney.time < time));      
    }   

    public static double returnLeast(){ 
     double d = maxDayCharge - dayCharge; 
     double m = maxMonthCharge - monthCharge; 
     double w = maxWeekCharge - weekCharge; 
     double least = 0; 
     if (d <= w && d <= m) 
     { 
      least = d; 
     } 
     else if(w <= d && w <= m) 
     { 
      least = w; 
     } 
     else if(m <= d && m <= w) 
     { 
      least = m; 
     } 

     return least; 
    } 
     public double journeyCost(Journey reqJourney){     
     if (journeyType == 1){         
      if (dayCharge <= 2.50 && weekCharge <= 14 && monthCharge <= 47) 
      { 
       costOfJourney = 1; 
      } 
      else 
      { 
       costOfJourney = returnLeast(); 
      } 

     } 
     else if (journeyType == 2) 
     { 
      if (dayCharge <= 1.80 && weekCharge <= 13.30 && monthCharge <= 46.30) 
      { 
       costOfJourney = 1.70; 
      } 
      else 
      { 
       costOfJourney = returnLeast(); 
      } 
     } 
     else if (journeyType == 3) 
     { 
      if (dayCharge <= 1.60 && weekCharge <= 13.10 && monthCharge <= 46.10) 
      { 
       costOfJourney = 1.90; 
      } 
      else 
      { 
       costOfJourney = returnLeast(); 
      } 

     }   
     return costOfJourney;  
    } 
    public void updateCurrentCharges(int date){ 
     int newDayOfYear = DateFunctions.getYearDay(date); 
     int newWeekOfYear = DateFunctions.getYearWeek(date); 
     int newMonthOfYear = DateFunctions.getYearMonth(date); 

     if (newDayOfYear > WmBusPass.dayOfYear) 
     { 
      WmBusPass.dayOfYear = newDayOfYear; 
      dayCharge = 0; 
     } 
     if (newWeekOfYear > WmBusPass.weekOfYear) 
     { 
      WmBusPass.weekOfYear = newWeekOfYear; 
      weekCharge = 0; 
     } 
     if (newMonthOfYear > WmBusPass.monthOfYear) 
     { 
      WmBusPass.monthOfYear = newMonthOfYear; 
      monthCharge = 0; 
     } 
    } 
}   

답변

2

당신이

Journey thisJourney = new Journey(date, time, busNumber, journeyType); 

Journey 객체를 생성하고 그것 때문에하지만 근래하지 않습니다 그것을위한 생성자. 당신은 Journey에 대해 하나의 생성자를 정의

5

...

public Journey(int date, double time, int busNumber, int journeyType) 

그래서 Journey에 생성자를 추가합니다 : 여행은 생성자 포함

public Journey(int date, double time, int busNumber, int journeyType) 

하지만 당신은 이런 식으로 호출됩니다

public boolean journey(int date, int time, String busNumber, int journeyType){ 
     Journey thisJourney = new Journey(date, time, busNumber, journeyType); 

여기서 busNumberString, int이 아닙니다. 세 가지 선택 사항이 있습니다.

  1. String을 취하도록 생성자를 수정하십시오.
  2. String을 사용하는 다른 생성자를 추가하십시오.
  3. 생성자를 호출하기 전에 busNumberint으로 변환하십시오.
+0

패키지에 대한 질문에 [답변] (http://stackoverflow.com/questions/3226282/are-there-best-practices-for-java-package-organisation)도 있습니다. – Fuhrmanator

0

컴파일러를 믿으십시오.

Journey의 생성자에는 제공하는 인수가 없습니다. 당신이 코딩 무엇

봐 :

Journey thisJourney = new Journey(date, time, busNumber, journeyType); 

busNumber는 전화에 String, 그러나 당신의 ctor에 int입니다 :

사용 : 패키지 혼란에 대응

public Journey(int date, double time, int busNumber, int journeyType) 
0

eclipse과 같은 IDE (통합 개발 환경). 이렇게하면 pacakges를 사용하고 코딩의 모든 측면을 쉽게 수행 할 수 있습니다.

패키지를 정의하기 위해 액세스 보호와 이름 공간 관리를 제공하는 코드를 논리적으로 그룹화합니다. 패키지는 자식 패키지가

당신은 패키지 here에 대한 자세한 내용을 찾을 수 등 com.transport.vehicle 또는 com.transport.billing을 인과 부모 패키지 인 예를 com.transport를 들어, 서브 패키지를 가질 수 있습니다.