2012-04-09 4 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 с

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

но вы не ВГА e конструктор для него. Путешествие содержит только конструктор:

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

так добавить конструктор Journey ...

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 является String, а не int. У вас есть три варианта:

  1. Измените конструктор, чтобы принять String.
  2. Добавить еще один конструктор, который принимает String.
  3. Конвертировать busNumber в int перед вызовом конструктора.
+0

Есть также [ответы] (http://stackoverflow.com/questions/3226282/are-there-best-practices-for-java-package-organisation) на ваш вопрос о пакетах. – Fuhrmanator

0

Верьте в компилятор.

В нем говорится, что нет конструктора для Путешествий с аргументами, которые вы даете.

Посмотрите, что вы закодированы:

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

busNumber в вашем вызову String, но int в вашем CTOR:

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

В ответ на ваш пакет путанице:

использования IDE (интегрированная среда разработки), например eclipse. Это облегчит вашу жизнь с помощью pacakges и почти с каждым аспектом кодирования.

Чтобы определить пакет, это логическая группировка кода, обеспечивающего защиту доступа и управление пространством имен. Пакет может иметь суб-пакеты, например com.transport будучи родителем пакет с дочерними пакетами являются com.transport.vehicle или com.transport.billing и т.д.

Вы можете узнать больше о пакетах here.

Смежные вопросы