2014-10-22 82 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);