2014-10-22 84 views
0

談到Java時,我知道如何計算大多數事情,但是這要麼困擾了我很多,要麼我的大腦正在死亡。無論如何,我有一個名爲「Jobs」的類,在該類中是一個名爲「day」的String變量。已經創建了多個新作業(確切的編號是未知的),現在我需要查詢並瞭解每天有多少個作業。我認爲這會很容易,但我不知道如何創建一個從整體看待喬布斯而不是一個特定的喬布斯。Java:數據在一個類中出現次數的計數

作業數據是通過掃描器讀取文件(其名稱是jobFile)而創建的。

public class Job_16997761 { 

    private int jobID;    // unique job identification number 
    private int customerID;  // unique customer identification number 
    private String registration; // registration number for vehicle for this job 
    private String date;   // when the job is carried out by mechanic 
    private String day;   // day of the week that job is booked for 
    private double totalFee;  // total price for the Job 
    private int[] serviceCode;  // the service codes to be carried out on the vehicle for the job 

    //Constructor 
    public Job_16997761(int jobID, int customerID, String registration, 
      String date, String day, double totalFee, int[] serviceCode) { 
     this.jobID = jobID; 
     this.customerID = customerID; 
     this.registration = registration; 
     this.date = date; 
     this.day = day; 
     this.totalFee = totalFee; 
     this.serviceCode = serviceCode; 
    } 
+1

嘗試發佈一些代碼,如「Jobs」類。此外,如何創建'Jobs',並在應用程序中存在哪些位置(例如:誰知道它們存在)? – 2014-10-22 07:10:22

+3

如果你想得到準確的答案,我建議你發佈一些代碼。 – ortis 2014-10-22 07:10:23

+1

你有沒有類似'List'的地方,所有'Job'都被引用? – Steffen 2014-10-22 07:15:05

回答

0

這只是衆多方法之一,且並非是有效的,但是這是你可以考慮(在你編輯了自己的最後一個職位)的一種方式。使用arrayList來包含所有的Job對象並迭代對象。

import java.util.*; 

public class SomeClass { 

    public static void main(String[] args) 
    { 
     Jobs job1 = new Jobs(1); 
     Jobs job2 = new Jobs(1); 
     Jobs job3 = new Jobs(2); 
     Jobs job4 = new Jobs(2); 
     Jobs job5 = new Jobs(2);         

     ArrayList<Jobs> jobList = new ArrayList<Jobs>(); 
     jobList.add(job1); 
     jobList.add(job2); 
     jobList.add(job3); 
     jobList.add(job4); 
     jobList.add(job5); 

     System.out.println(numOfJobOnDayX(jobList, 2)); //Jobs which falls on day 2 
    } 

    public static int numOfJobOnDayX(ArrayList<Jobs> jobList, int specifiedDay) 
    { 
     int count=0; 
     for(int x=0; x<jobList.size(); x++) //May use a for-each loop instead 
      if(jobList.get(x).days == specifiedDay) 
       count ++; 
     return count;     
    } 
} 

OUTPUT:3

班作業..

class Jobs 
{ 
    int days;  
    public Jobs(int days) 
    { 
     this.days = days; 
    } 
} 

爲簡單起見,我沒有使用任何getter和setter方法。您可能想要考慮要使用什麼數據結構來保存對象。再一次,我需要重新強調這可能不是一種有效的方式,但它給你一些想法做點數的可能性。

+1

'String'不能與'=='相比較。而且你似乎在比較'Jobs'和'String'。 – RandomQuestion 2014-10-22 07:22:22

1

不確定爲什麼要創建一個作業的動態實例(例如Job_16997761,似乎每個作業都有它自己的類)。但是,在創建作業時,您可以維護一張每天會有一定作業數量的地圖。喜歡的東西:

Map<String, Long> jobsPerDay=new HashMap<String,Long>(); 

然後創建一個新的工作時,你可以簡單地增加每一天的計數器:

jobsPerDay.put(day,jobsPerDay.get(day)!=null?jobsPerDay.get(day)++:1); 

這樣,您就能夠通過使用獲得的就業人數一天:jobsPerDay.get(day)

請注意,您可以使用java.time.DayOfWeek而不是String

+0

這個方法只是一個附註:當你使用'jobsPerDay.get(day)'時,如果你當天還沒有工作,結果將是NPE,它將返回null。您應該使用三元運算符或「if」測試來處理它。 – 2014-10-22 07:27:24

+0

@ug_是,如果某一天沒有添加任何工作,則爲true。 – dan 2014-10-22 07:28:20

+0

這個號碼就是我在uni的學生號,它需要在我們提交的所有java文件中。 – 2014-10-22 09:41:56

0

除非您提供更多詳細信息,否則很難告訴您正確的解決方案。你在說你可以寫while循環,所以我會假設你已經收集了Job

int count = 0; 
List<Job> jobs = readJobsFromFile(); 
for(Job job : jobs) { 
    if(job.getDay().equals(inputDay)){ //inputDay is day you have to find number of jobs on. 
     count++; 
    } 
} 

System.out.Println(count);