Java中的范围和包问题

时间:2012-04-09 17:51:11

标签: java oop

我正在尝试编写一组将为旅行卡建模的类。我在访问另一个类中的成员时遇到问题。 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 个答案:

答案 0 :(得分: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);

其中busNumberString,而不是int。你有三个选择:

  1. 修改构造函数以获取String
  2. 添加另一个带有String
  3. 的构造函数
  4. 在调用构造函数之前将busNumber转换为int

答案 1 :(得分:2)

这是因为您正在使用

创建Journey对象
Journey thisJourney = new Journey(date, time, busNumber, journeyType);

但你没有构造函数。 Journey只包含构造函数:

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

所以将构造函数添加到Journey ...

答案 2 :(得分:0)

相信编译器。

它说Journey的构造函数没有你给出的论据。

看看你的编码:

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

busNumber在您的通话中为String,但在您的电话中为int

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

答案 3 :(得分:0)

回应您的包裹混淆:

使用像eclipse这样的IDE(集成开发环境)。这将使您的生活与pacakges和几乎编码的每个方面都很容易。

要定义包,它是提供访问保护和名称空间管理的代码的逻辑分组。包可以包含子包,例如com.transport是父包,子包为com.transport.vehiclecom.transport.billing等。

您可以找到有关包here的更多信息。