1

I am trying to write a set of classes that will model a travelcard. I am having problems accessing members in one class from another. The WmBusPass class will currently not compile and gives the error message "cannot find symbol - constructor Journey(int,int,java.lang.String,int)". More generally I am unsure as to the best approach in organizing the classes. Would making the Journey class an inner class of WmBusPass make sense? Also I was trying to solve my organisational problems by putting the classes into a package, however, I do not fully understand the dynamics of packages and it has only served to confuse me further.

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;
        }
    }  
}        
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
seanysull
  • 720
  • 1
  • 8
  • 24

4 Answers4

5

You have only defined one constructor for Journey:

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

but you are calling it like this:

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

where busNumber is a String, not an int. You have three choices:

  1. Modify the constructor to take a String.
  2. Add another constructor that takes a String.
  3. Convert busNumber to an int before calling the constructor.
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • There are also [answers](http://stackoverflow.com/questions/3226282/are-there-best-practices-for-java-package-organisation) to your question about packages. – Fuhrmanator Apr 10 '12 at 19:55
2

it's because you're creating Journey object with

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

but you don't have constructor for it. Journey only contains constructor:

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

so add constructor to Journey...

viliam
  • 503
  • 6
  • 23
0

Believe the compiler.

It says there's no constructor for Journey with the arguments you're giving.

Look what you coded:

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

busNumber is a String in your call, but an int in your ctor:

public Journey(int date, double time, int busNumber, int journeyType)
duffymo
  • 305,152
  • 44
  • 369
  • 561
0

In response to your package confusion:

Use a IDE(Integrated Development Environment) like eclipse. This will make your life with pacakges and with almost every aspect of coding easy.

To define a package, it is a logical grouping of the code that provides access protection and name space management. Package can have sub-packages, for example com.transport being the parent package with child packages being com.transport.vehicle or com.transport.billing, etc.

You can find more about packages here.

mtk
  • 13,221
  • 16
  • 72
  • 112