打印带有对象的语句.爪哇 [英] Print statements with objects. Java

查看:50
本文介绍了打印带有对象的语句.爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个程序,该程序为员工建模,以此作为开始面向对象编程的一种方式.它获取每个员工的姓名、雇用日期和地址,然后必须显示信息

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.

推荐答案

您可以覆盖 NameAddresstoString 方法中的Date,并让 Employee 在自己的 toString 中使用这些方法.

You could override the toString-method in Name, Address and Date, and let Employee use these methods in its own toString.

例如:

@Override
public String toString() {
    return firstName + " " + lastName;
}

地址

@Override
public String toString() {
    return street + ", " + city + ", " + state + ", " + zip;
}

日期

@Override
public String toString() {
    return String.valueOf(month) + "." +
           String.valueOf(day)   + "." +
           String.valueOf(year);
}

员工

@Override
public String toString() {
    return name.toString() + "\n" +
           date.toString() + "\n" +
           address.toString();
}

这篇关于打印带有对象的语句.爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆