我试图在一个对象中分配一个变量数组,但不知道如何

时间:2019-06-05 20:37:41

标签: java arrays object variable-assignment

我正在尝试使用main函数中的数组为对象分配数组。我遇到了麻烦,得到的输出为[null,1]。然后,下次我尝试更改它时,它会打印出[(this Collection),2]。为了解决这个问题,我在做什么,这是错误的。同样,对于我使用的任何草率技术,我都感到抱歉,我上这堂课只有大约三个星期。

import java.util.ArrayList;
import java.util.Scanner;

//making a class for the baby sitting info 
public class bbySit 
{
    DecimalFormat df = new DecimalFormat("####");
    String yearString;
    String yearConvert;
    private int year;
    private int jobNum = 0001;
    private int numChild;
    private int hours;
    private ArrayList sitter;

    public bbySit()
    {
        year = 00;
        jobNum = 0001;
    }
    public bbySit(String yearString)    //going to make this also change the year later
    {
        yearConvert = yearString.substring(2,4);
        year = Integer.parseInt(yearConvert);
    }


    //This is what I need help with, trying to assign this then read assign it later in the code
    public ArrayList getSitter()
    {
        return sitter;
    }
    public void setSitter(ArrayList book)
    {
        this.sitter = book;
    }


    //this will be used later
    public int getNumChild()
    {
        return numChild;
    }
    public void setNumChild(int numChild)
    {
        this.numChild = numChild;
    }


    //this will be used later
    public int getHours()
    {
        return hours;
    }
    public void setHours(int hours)
    {
        this.hours = hours;
    }


    public int getYear()
    {
        return year;
    }
    public void setYear(String yearString)
    {
        yearConvert = yearString.substring(2,4);
        this.year = Integer.parseInt(yearConvert);
    }


    public int getJobNum() 
    {
        return jobNum;
    }
    public void setJobNum(int jobNum)
    {
        this.jobNum = jobNum;
    }


    public String toString()
    {
        String result = Integer.toString(year)+Integer.toString(jobNum);
        return result;
    }

    public static void main(String[] args)
    {
        DecimalFormat df = new DecimalFormat("0000.##");
        ArrayList book = new ArrayList();
        Scanner taco = new Scanner(System.in);

        int tempYear;
        int tempJobNum = 0001;
        int stopNum = 0;
//  These variables are going to be used later, just have them declared now
        int[] tempEmp;
        int[] addTempEnt;
        int loop = 0;

        bbySit job1 = new bbySit("2019");
        bbySit job2 = new bbySit("2020");
        bbySit job3 = new bbySit("2021");
        bbySit job4 = new bbySit("2022");
        bbySit job5 = new bbySit("2023");
        bbySit job6 = new bbySit("2024");
        bbySit job7 = new bbySit("2025");

        while(stopNum != 1)
        {
            System.out.println("What year is the job in (last two digits of the year i.e. 2019 = 19): ");
            tempYear = taco.nextInt();

            //The only two that I have changed trying to make work is case 19, and case 20
            switch(tempYear)
            {
            case 19:
                System.out.println("job is in year 20"+ job1.getYear());
                tempJobNum = job1.getJobNum();
                System.out.println("The job number is 19"+df.format(tempJobNum)+"\n");
                job1.setJobNum(tempJobNum + 1);
                System.out.println("Who is taking the job?");
                book.add(job1.getSitter());
                book.add(tempJobNum);
                job1.setSitter(book);
                System.out.println("taco taco "+job1.getSitter());
                book.clear();
                break;

            case 20:
                System.out.println("job is in year 20"+ job2.getYear());
                tempJobNum = job2.getJobNum();
                System.out.println("The job number is 20"+df.format(tempJobNum)+"\n");
                job2.setJobNum(tempJobNum + 1);
                System.out.println("Who is taking the job?");
                book.add(job1.getSitter());
                book.add(tempJobNum);
                job2.setSitter(book);
                System.out.println("taco taco "+job2.getSitter());  
                book.clear();
                break;

            case 21:
                System.out.println("job is in year 20"+ job3.getYear());
                tempJobNum = job3.getJobNum();
                System.out.println("The job number is 21"+df.format(tempJobNum)+"\n");
                job3.setJobNum(tempJobNum + 1);
                System.out.println("Who is taking the job?");
                book.add(job1.getSitter());
                break;

            case 22:
                System.out.println("job is in year 20"+ job4.getYear());
                tempJobNum = job4.getJobNum();
                System.out.println("The job number is 22"+df.format(tempJobNum)+"\n");
                job4.setJobNum(tempJobNum + 1);
                break;

            case 23:
                System.out.println("job is in year 20"+ job5.getYear());
                tempJobNum = job5.getJobNum();
                System.out.println("The job number is 23"+df.format(tempJobNum)+"\n");
                job5.setJobNum(tempJobNum + 1);
                break;

            case 24:
                System.out.println("job is in year 20"+ job6.getYear());
                tempJobNum = job6.getJobNum();
                System.out.println("The job number is 24"+df.format(tempJobNum)+"\n");
                job6.setJobNum(tempJobNum + 1);
                break;
            case 25:
                System.out.println("job is in year 20"+ job7.getYear());
                tempJobNum = job7.getJobNum();
                System.out.println("The job number is 25"+df.format(tempJobNum)+"\n");
                job7.setJobNum(tempJobNum + 1);
                break;

            default:
                System.out.println("Please print a valid responce (numbers 19-25).");   
            }
        }   
    }
}

0 个答案:

没有答案