1

トラベルカードをモデル化する一連のクラスを作成しようとしています。あるクラスのメンバーに別のクラスからアクセスする際に問題があります。WmBusPass クラスは現在コンパイルされず、「シンボルが見つかりません - コンストラクター Journey(int,int,java.lang.String,int)」というエラー メッセージが表示されます。より一般的には、クラスを編成するための最良のアプローチについて確信が持てません。Journey クラスを WmBusPass の内部クラスにすることは理にかなっていますか? また、クラスをパッケージに入れることで組織の問題を解決しようとしていましたが、パッケージのダイナミクスを完全には理解しておらず、さらに混乱するだけでした.

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;
        }
    }  
}        
4

4 に答える 4

5

のコンストラクターを 1 つだけ定義しました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);

busNumberではStringなくですint。次の 3 つの選択肢があります。

  1. を受け取るようにコンストラクターを変更しStringます。
  2. を取る別のコンストラクターを追加しますString
  3. コンストラクターを呼び出す前に に変換busNumberします。int
于 2012-04-09T17:54:40.487 に答える
2

それはあなたがJourneyオブジェクトを作成しているためです

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

しかし、あなたはそれのためのコンストラクタを持っていません。Journey にはコンストラクターのみが含まれます。

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

Journeyそのため、コンストラクターを...に追加します

于 2012-04-09T17:54:40.293 に答える
0

コンパイラを信じてください。

あなたが与えている引数を持つ Journey のコンストラクターがないと言っています。

あなたがコーディングしたものを見てください:

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

busNumberStringあなたの呼び出しでは a ですがint、あなたの ctor では:

public Journey(int date, double time, int busNumber, int journeyType)
于 2012-04-09T17:55:28.480 に答える
0

パッケージの混乱に応えて:

Eclipseなどの IDE (統合開発環境) を使用します。これにより、パッケージを使用したり、コーディングのほぼすべての側面を簡単に操作したりできます。

パッケージを定義するには、アクセス保護と名前空間管理を提供するコードを論理的にグループ化します。パッケージにはサブパッケージを含めることができます。たとえばcom.transport、子パッケージがcom.transport.vehiclecom.transport.billingなどの親パッケージです。

パッケージの詳細については、こちらをご覧ください。

于 2012-04-09T18:06:16.427 に答える