所以我正在研究一个小型的联系组织者,它的作用是存储不同的联系人(电话,电子邮件和邮政)并显示或删除它们,以及什么不是。到目前为止,该程序可以创建联系人,现在我正在尝试显示它们。但它只显示内存位置而不是实际的Object。元素是对象,SOP对象数组是否有特殊的方法?
此外,我为每种类型的联系人提供了单独的类,包括他们的mutator,访问器和构造函数(Telephone class,Email class和Postal class。Contact是Interface)。
我的程序代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.NumberFormatException;
/**
* Contact program.
*
* @author Janarthanan Manoharan
* @version 1.0 2013-11-24
*/
public class ContactOrganizer
{
// class constants
private static final String C = "[C]reate contact";
private static final String D = "[D]isplay contacts";
private static final String E = "[E]rase contact";
private static final String F = "[F]ind contact";
private static final String H = "[H]elp";
private static final String Q = "[Q]uit";
// other constants
private static final String CREATE_CONTACT = "C";
private static final String DISPLAY_CONTACT = "D";
private static final String ERASE_CONTACT = "E";
private static final String FIND_CONTACT = "F";
private static final String HELP = "H";
private static final String QUIT = "Q";
private static final String one = "telephone";
private static final String two = "email";
private static final String three = "postal";
private static final int CONTACT_LIMIT = 1000;
private static final int PHONE_NUMBER_LIMIT = 10;
private static Contact[] contactArray = new Contact[CONTACT_LIMIT];
private static int contactCounter = 0;
private static BufferedReader input;
/**
* Contact program.
*
* @param arguments not used
*/
public static void main(String[] argument)
{
String choice = "";
String command = "";
input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("CONTACT ORGANIZER");
System.out.println("\n" + C);
System.out.println(D);
System.out.println(E);
System.out.println(F);
System.out.println(H);
System.out.println(Q);
do
{
System.out.println("\nEnter a command, master: ");
try
{
command = input.readLine();
if (command.equalsIgnoreCase(CREATE_CONTACT))
{
System.out.println("Telephone, Email, Postal? ");
choice = input.readLine();
if (choice.equalsIgnoreCase(one))
{
createTelephone();
}
else if (choice.equalsIgnoreCase(two))
{
createEmail();
}
else if (choice.equalsIgnoreCase(three))
{
createPostal();
}
}
if (command.equalsIgnoreCase(DISPLAY_CONTACT))
{
displayContacts(contactArray);
}
if (command.equalsIgnoreCase(HELP))
{
System.out.println("CONTACTS");
System.out.println("\n" + C);
System.out.println(D);
System.out.println(E);
System.out.println(F);
System.out.println(H);
System.out.println(Q);
} // end of if (command.equalsIgnoreCase(HELP))
if (command.equalsIgnoreCase(QUIT))
{
System.out.println("\nO");
System.out.println(" F");
System.out.println(" F");
System.out.println(" !");
System.exit(0);
}
}
catch (IOException exception)
{
System.out.println("YOU DONE MESSED UP AARON!");
}
}
while(!command.equalsIgnoreCase(null));
}
private static void createTelephone()
{
long customer = 0;
int areaCode = 0;
int exchange = 0;
boolean isNumberValid = false;
int length = 0;
int number = 0;
// int phoneNumber = 0;
int type = 0;
try
{
System.out.println("Enter customer ID: ");
customer = Long.parseLong(input.readLine());
do
{
System.out.println("Enter phone number: ");
long phoneNumber = Long.parseLong(input.readLine());
if (Long.toString(phoneNumber).length() == PHONE_NUMBER_LIMIT)
{
isNumberValid = true;
areaCode = Integer.parseInt(Long.toString(phoneNumber).substring(0,3));
exchange = Integer.parseInt(Long.toString(phoneNumber).substring(3,6));
number = Integer.parseInt(Long.toString(phoneNumber).substring(6));
} // end of if (Long.toString(phoneNumber).length() == PHONE_NUMBER_LIMIT)
else
{
System.out.println("INVALID NUMBER, TRY AGAIN");
}
}
while(!isNumberValid);
System.out.println("Home: 10, Work: 20, Other: 30");
System.out.println("Enter type: ");
type = Integer.parseInt(input.readLine());
contactArray[contactCounter] = new TelephoneContact(customer, areaCode, exchange, number, type);
contactCounter++;
System.out.println("CONTACT CREATED!");
System.out.println(areaCode + "" + exchange + "" + number);
}
catch (NumberFormatException exception)
{
System.out.println("YOU DONE MESSED UP AARON!");
}
catch (IOException exception)
{
System.out.println("YOU DONE MESSED UP AARON!");
} // end of try block
} // end of createTelephone()
private static void createEmail()
{
long customer = 0;
String domain = "";
String user = "";
int type = 0;
try
{
System.out.println("Enter customer ID: ");
customer = Long.parseLong(input.readLine());
System.out.println("Enter username: ");
user = input.readLine();
System.out.println("Enter domain: ");
domain = input.readLine();
System.out.println("Enter type: ");
type = Integer.parseInt(input.readLine());
contactArray[contactCounter] = new EmailContact(customer, user, domain, type);
contactCounter++;
System.out.println("CONTACT CREATED!");
}
catch (NumberFormatException exception)
{
System.out.println("YOU DONE MESSED UP AARON!");
}
catch (IOException exception)
{
System.out.println("YOU DONE MESSED UP AARON!");
} // end of try block
} // end of createEmail()
private static void createPostal()
{
long customer = 0;
String address = "";
String postalCode = "";
int type = 0;
try
{
System.out.println("Enter customer ID: ");
customer = Long.parseLong(input.readLine());
System.out.println("Enter address: ");
address = input.readLine();
System.out.println("Enter postal code: ");
postalCode = input.readLine();
System.out.println("Enter type: ");
type = Integer.parseInt(input.readLine());
contactArray[contactCounter] = new EmailContact(customer, address, postalCode, type);
contactCounter++;
System.out.println("CONTACT CREATED!");
}
catch (NumberFormatException exception)
{
System.out.println("YOU DONE MESSED UP AARON!");
}
catch (IOException exception)
{
System.out.println("YOU DONE MESSED UP AARON!");
} // end of try block
} // end of createPostal()
private static void displayContacts(Contact[] contactArray)
{
Contact[] telephoneArray = new Contact[CONTACT_LIMIT];
Contact[] emailArray = new Contact[CONTACT_LIMIT];
Contact[] postalArray = new Contact[CONTACT_LIMIT];
//System.out.println("\nTELEPHONE CONTACTS");
for (int i = 0; i < contactArray.length; i++)
{
if (contactArray[i] != null)
{
System.out.println(contactArray[i]);
}
}
}
} // end of class ContactProgram
答案 0 :(得分:0)
对于打印对象时的可理解输出,您需要覆盖班级上的Object.toString()
,如"Object as a Superclass"中所述。例如:
public class Contact {
// some fields
// some accessors
public String toString() {
return "this will be printed";
}
}
请注意,toString()
几乎总是严格用于调试目的。
答案 1 :(得分:0)
你应该覆盖EmailContact类中的toString()
,因为那是你要打印出的数组类型。例如:
class EmailContact{
String customer;
String address;
String postalCode;
String type;
public EmailContact(String customer, String address,
String postalCode, String type) {
this.customer = customer;
this.address = address;
this.postalCode = postalCode;
this.type = type;
}
...
@Override
public String toString(){
return "Customer: " + customer +
"\nAddress: " + address +
"\nPostal Code: " + postalCode +
"\nType: " + type;
}
}
或覆盖toString()
课程中的Contact
。无论您想要格式化打印。两者都必要
编辑:在评论中提出OP问题
问题: @Override到底做了什么,因为我在电话,电子邮件和邮政课程中的toDisplayString()上尝试了它,它仍然显示在内存位置。
答案: @Override
会覆盖从toString()
类继承的Object
。例如,如果要打印System.out.println(someEmailContactObject);
,它将使用Object#toString
方法并打印内存位置,除非您按照希望的方式覆盖它。
如果您想使用toDisplayString()
,则必须执行此操作System.out.println(someEmialContactObject.toDisplayString());
。
覆盖时,必须使用确切的方法签名覆盖
@Override
public String toString(){
}
下面的内容不会覆盖任何内容
@Override
public String toDisplayString(){
}