Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

Print statements with objects. Java

I am currently writing a program that models an employee as a way to get started in object oriented programming. It gets a name, hire date, and address of each employee, and then must display the information

My current program has no compile errors, but I am confused as to how I would go about printing the information in a neat manner. Thanks!

public class Unit10Assignment1
{
    public static void main( String [] args )
{
    int numEmployees = Input.getInt("How many employees are you storing?");
    Employee database[] = new Employee[numEmployees];

    for( int i = 0; i < numEmployees; i++ )
    {
        String firstName = Input.getString("What is an employee's first name?");
        String lastName = Input.getString("What is their last name?");

        String street = Input.getString("What street do they live on?");
        String city = Input.getString("What city do they live in?");
        String state = Input.getString("What state do they live in?(2 characters)");
        String zip = Input.getString("What is their zipcode?");

        int month = Input.getInt ("In what month was he/she hired?(number)");
            int day = Input.getInt ("On what day was he/she hired(number)");
            int year = Input.getInt ("In what year was he/she hired?(number)");

        database[i] = new Employee(firstName, lastName, street, city, state, zip, month, day, year);
    }
}
}

class Employee
{
Name Name;
Address Address;
Date Date; 

Employee( String firstName, String lastName, String street, String city, String state, String zip, int month, int day, int year)
{
    Name = new Name( firstName, lastName );
    Address = new Address( street, city, state, zip );
    Date = new Date( month, day, year );
}
}

class Name
{
    String firstName = " ";
    String lastName = " ";

public Name(String newFirstName, String newLastName)
{
    firstName = newFirstName;
    lastName = newLastName;
}

public String getFirst()
{
    return firstName;
}
public String getLast()
{
    return lastName;
}
}

class Address
{
String street = " ";
String city = " ";
String state = " ";
String zip = " ";

public Address(String newStreet, String newCity, String newState, String newZip)
{
    street = newStreet;
    city = newCity;
    state = newState;
    zip = newZip;
}

public String getStreet()
{
    return street;
}

public String getCity()
{
    return city;
}

public String getState()
{
    return state;
}

public String getZip()
{
    return zip;
}
}

class Date
{
int month = 0;
int day = 0;
int year = 0;

public Date(int newMonth, int newDay, int newYear)
{
    month = newMonth;
    day = newDay;
    year = newYear;
}

public int getMonth()
{
    return month;
}

public int getDay()
{
    return day;
}

public int getYear()
{
    return year;
}


}

Having trouble formatting, hopefully you can understand it. If there are any problems with my current code, pointing them out to me would be appreciated. Also, my instructor uses his own class to get user input so no need to worry about that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For pretty printing an objects contents, I prefer overriding the toString method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...