2012-04-09 28 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オブジェクトを作成しているので、それはだが、あなたは肝炎はありませんそれのためのeコンストラクタです。あなただけJourneyのための1つのコンストラクタを定義している

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); 

busNumberStringintではありません。あなたは3つの選択肢があります:

  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(統合開発環境)。これはあなたの人生をパカクジで、コーディングのあらゆる面を簡単にします。

パッケージを定義するには、アクセス保護と名前空間管理を提供するコードを論理的にグループ化します。パッケージは、パッケージhereについての詳細を見つけることができます

など、例com.transportは、子パッケージがcom.transport.vehiclecom.transport.billingていると親パッケージであることのため、サブパッケージを持つことができます。