jeudi 26 mars 2020

Java OOP - Selecting from arrays with other classes?

I have three classes. My driver class, a People class, and a Chore class.

I have chores and family members listed in two separate csv files.

The chore file includes the area where the task is done, the task name, the number of times per week the task is performed (6 or 1 for daily/weekly), and the difficulty level of the task (easy, medium, hard).

The family members file just contains names and dob's (01/01/1901).

For family members under 7: It will assign 8 chores rated "easy" which will consist of 3 daily chores (which are performed 6 times per week) and 5 weekly chores (performed 1 time per week). For family members between 7 and 13: it will assign 8 chores in the same fashion as those under 7, but those chores can be easy or medium in difficulty. Then it will assign 12 chores each to the family members over the age of 13, which can be easy, medium, hard. It just depends on what wasn't assigned to the younger family members.

**My question is, how do I select random chores based on the age of each person? I'm not really understanding how to access the chore array or the family array. I thought this was right, but I have tons of "cannot find symbol" errors. My professor tried explaining it to me, and I've looked at examples. When they're basic examples I can kind of understand it, but once all these if statements and whatnot are thrown in I lose track of what is happening and what needs to come from where. **

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();
    }
}

Here is the People class.

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;
        }
    }
}

Here is the Chore class:


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);
    }

}

Aucun commentaire:

Enregistrer un commentaire