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

您只定义了一个构造函数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是 a String,而不是int。你有三个选择:

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

busNumber在您的电话中是 a String,但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.vehicleorcom.transport.billing等​​。

您可以在此处找到有关软件包的更多信息。

于 2012-04-09T18:06:16.427 回答