Java OOP-从具有其他类的数组中选择?

 收藏

我有三节课。我的驾驶员班,人民班和杂务班。

我在两个单独的csv文件中列出了家务和家庭成员。

杂务文件包括完成任务的区域,任务名称,每周执行任务的次数(每天/每周为6或1)以及任务的难度级别(容易,中等,困难) 。

家庭成员文件仅包含名称和Dob(01/01/1901)。

对于7岁以下的家庭成员:它将分配8个等级为“简单”的琐事,其中包括3个日常琐事(每周执行6次)和5个每周琐事(每周执行1次)。对于7到13岁之间的家庭成员:它将以与7岁以下的家庭相同的方式分配8个琐事,但这些琐事可能容易或中等难度。然后,它将为13岁以上的家庭成员分配12个家务活,这很容易,中等,困难。这仅取决于未分配给年轻家庭成员的内容。

**我的问题是,如何根据每个人的年龄选择随机的琐事?我不太了解如何访问杂项数组或家庭数组。我以为这是正确的,但是我有很多“找不到符号”错误。我的教授试图向我解释它,我看了一些例子。当它们是基本示例时,我会有所理解,但是一旦将所有这些if语句和诸如此类的东西扔进去,我就无法跟踪正在发生的事情以及需要从何而来。 **

import java.io.File;
import java.util.Scanner;
import java.util.Random;
import java.util.Date;

public class Project1 {
    static final int NUMBER_OF_CHORES = 48;
    static final int NUMBER_OF_FAMILY_MEMBERS = 4;

    static final String[] AREA = new String[NUMBER_OF_CHORES];
    static final String[] TASK = new String[NUMBER_OF_CHORES];
    static final String[] TIMESPERWEEK = new String[NUMBER_OF_CHORES];
    static final String[] DIFFICULTY = new String[NUMBER_OF_CHORES];

    static final String[] NAME = new String[NUMBER_OF_FAMILY_MEMBERS];
    static final Date[] DOB = new Date[NUMBER_OF_FAMILY_MEMBERS];

    public void main(String[] args) {
        System.out.println(">>> Weekly Family Chore Generator <<<");
        System.out.println("----------------------------------------------------");

        readChoresFromFile("Chore List.csv");
        readFamilyFromFile("Family List.csv");

        boolean checkIfAllChoresAssigned = false;
        int assignedChoresCount = 0;
        //only want a max of 40 chores assigned; the extras can be for the kids to do for extra money


        while (true) {
            Random rnd=new Random();
            for (int i = 0; i < NUMBER_OF_FAMILY_MEMBERS; i++){            
                if (People.getCalculateAge <= 7) {
                    System.out.println(">>" + People.getName + "'s Daily Chores: ");
                    for(int j= 0; j < 3; j++){
                        int selected = rnd.nextInt(ChoreArray.length); //select random chore
                        if(ChoreArray[selected].DIFFICULTY = "Easy" && ChoreArray[selected].assigned = "N" && ChoreArray[selected].TIMESPERWEEK = "6"){
                           System.out.println("> " + ChoreArray[selected].TASK + " located in " + ChoreArray[selected].AREA);
                           ChoreArray[selected].assigned = "Y"; //mark the chore as assigned
                                       assignedChoresCount = assignedChoresCount +1; //increase the number of assigned chores by 1          
                                           // assigned 8 chores: 3 daily chores (done 6 times per week each), 5 weekly
                                           // chores (done 1 time per week each)
                                       }
                                       //if not easy or assigned N, do not use
                    }
                    System.out.println(">>" + People.getName + "'s Weekly Chores: ");

                    for(int k = 0; k<5; k++){
                        int selected = rnd.nextInt(ChoreArray.length); //select random chore
                            if(ChoreArray[selected].DIFFICULTY == "Easy" && ChoreArray[selected].assigned == "N" && ChoreArray[selected].TIMESPERWEEK == "1"){
                                System.out.println("> " + ChoreArray[selected].TASK + " located in " + ChoreArray[selected].AREA);
                                ChoreArray[selected].assigned = "Y"; //mark the chore as assigned
                                            assignedChoresCount = assignedChoresCount +1; //increase the number of assigned chores by 1         
                                           // assigned 8 chores: 3 daily chores (done 6 times per week each), 5 weekly
                                           // chores (done 1 time per week each)
                                       }
                                       //if not easy or assigned N, do not use

                    }
                                }
                if (People.calculateAge > 7 && People.calculateAge <= 14) {
                     System.out.println(">>" + People.getName + "'s Daily Chores: ");
                     for(int j= 0; j < 3; j++){
                        int selected = rnd.nextInt(ChoreArray.length); 
                        if(ChoreArray[selected].DIFFICULTY != "Difficult" && ChoreArray[selected].assigned == "N" && ChoreArray[selected].TIMESPERWEEK == "6"){
                           System.out.println("> " + ChoreArray[selected].TASK + " located in " + ChoreArray[selected].AREA);
                           ChoreArray[selected].assigned = "Y"; 
                                       assignedChoresCount = assignedChoresCount +1;
                                       }
                                       //if not easy or assigned N, do not use
                    }
                    System.out.println(">>" + People.getName + "'s Weekly Chores: ");

                    for(int k = 0; k<5; k++){
                        int selected = rnd.nextInt(ChoreArray.length); 
                            if(ChoreArray[selected].DIFFICULTY != "Difficult" && ChoreArray[selected].assigned == "N" && ChoreArray[selected].TIMESPERWEEK == "1"){
                                System.out.println("> " + ChoreArray[selected].TASK + " located in " + ChoreArray[selected].AREA);
                                ChoreArray[selected].assigned = "Y"; 
                                            assignedChoresCount = assignedChoresCount +1;
                                       }
                                       //if not easy or assigned N, do not use

                    }
                } else if (People.calculateAge > 14) {
                    System.out.println(">>" + People.getName + "'s Daily Chores: ");
                        for(int j= 0; j < 6; j++){
                            int selected = rnd.nextInt(ChoreArray.length); 
                            if(ChoreArray[selected].assigned == "N" && ChoreArray[selected].TIMESPERWEEK == "6"){
                                System.out.println("> " + ChoreArray[selected].TASK + " located in " + ChoreArray[selected].AREA);
                                ChoreArray[selected].assigned = "Y"; 
                                           assignedChoresCount = assignedChoresCount +1;
                                        }
                                       //if not easy or assigned N, do not use
                    }
                    System.out.println(">>" + People.getName + "'s Weekly Chores: ");

                    for(int k = 0; k<6; k++){
                        int selected = rnd.nextInt(ChoreArray.length); 
                            if(ChoreArray[selected].assigned == "N" && ChoreArray[selected].TIMESPERWEEK == "1"){
                                System.out.println("> " + ChoreArray[selected].TASK + " located in " + ChoreArray[selected].AREA);
                                ChoreArray[selected].assigned = "Y"; 
                                            assignedChoresCount = assignedChoresCount +1;           
                                        }               }

            }
        }

    }
}

    static void readChoresFromFile(String filePath) {
        File choreFile = new File(filePath);

        Scanner choreScanner;
        try {
            choreScanner = new Scanner(choreFile);
        } catch (Exception e) {
            System.out.println("File not found.");
            return;
        }
        choreScanner.nextLine();

        for (int i = 0; i < NUMBER_OF_CHORES; i++) {
            String line = choreScanner.nextLine();
            String[] ChoreArray = line.split(",");

            AREA[i] = ChoreArray[0];
            TASK[i] = ChoreArray[1];
            TIMESPERWEEK[i] = ChoreArray[2];
            DIFFICULTY[i] = ChoreArray[3];

        }
        choreScanner.close();
    }

    static void readFamilyFromFile(String filePath) {
        File familyFile = new File(filePath);

        Scanner familyScanner;
        try {
            familyScanner = new Scanner(familyFile);
        } catch (Exception e) {
            System.out.println("File not found.");
            return;
        }
        familyScanner.nextLine();

        for (int i = 0; i < NUMBER_OF_CHORES; i++) {
            String line = familyScanner.nextLine();
            String[] FamilyArray = line.split(",");

            NAME[i] = FamilyArray[0];
            DOB[i] = Date.parseDate(FamilyArray[1]);
        }
        familyScanner.close();
    }
}

这是人民课。

import java.time.LocalDate;
import java.time.MonthDay;
import java.util.Date;

public class People {
    private final String name;
    private final Date dob;
    private final int calculateAge;

    public People(String name, Date dob, int calculateAge) {
        this.name = name;
        this.dob = dob;
        this.calculateAge = calculateAge;
    }

    public String getName() {
        return name;
    }

    public Date getDob() {
        return dob;
    }

    public int calculateAge(LocalDate dob) {
        LocalDate today = LocalDate.now();
        int age = today.getYear() - dob.getYear();
        if (MonthDay.from(today).isBefore(MonthDay.from(dob)))
            age--;
            return age;
        }
    public int getCalculateAge(){
        return calculateAge;
    }

    public class ChoreList {
        Chore choreArray[] = new Chore[12];

        public ChoreList(String choreName, String difficulty, String location, String assigned) {
            for (int i = 0; i < choreArray.length; i++) {
                choreArray[i] = new Chore(choreName, difficulty,location);
            }
        }

        public Chore[] getChoreArray() {
            return choreArray;
        }
    }
}

这是家务课:


public class Chore{

    private final String choreName; 
    private final String difficulty;
    private String location;
    private String assigned;

    public Chore(String choreName, String difficulty, String location){
       this.choreName = choreName;
       this.difficulty = difficulty;
       this.location = location;
       this.assigned = "N";
    }

    public Chore(String choreName, String difficulty){
       this(choreName, difficulty, "Whole house");      
       this.assigned = "N";
    }

    public String getChoreName(){
        return choreName; 
    }    
    public String getDifficulty(){
        return difficulty;
    }
    public String getLocation(){
        return location;
    }
    public String getAssigned(){
        return assigned;
    }

    public void prettyPrint(){
        System.out.printf("Complete the chore: %s (%s) in location: %s.%n", choreName, difficulty, location);
    }

}
回复