我的代码是一个简单的邮件系统。该系统由四个类组成:
我的问题是关于上三课。在我的代码中将以Users
类开头并弹出一个菜单,您可以在其中添加,删除系统LinkedList
。菜单中还有一个使用选项,它将从系统中选择现有用户并弹出另一个位于User类中的菜单。在此课程中,特定用户拥有另外LinkedList
封电子邮件。在新菜单中,还有选项,例如列出电子邮件,阅读电子邮件和发送电子邮件。我的问题从这里开始。我可以发送消息,但无法列出消息。如果我在look()
类中调用User
方法,则显示0条消息,但如果在主类中调用它,则显示消息。请给出一些建议。谢谢。
我的代码: 用户类
public class Users
{
public static void main(String[] args)
{
new Users();
}
private LinkedList<User> users = new LinkedList<User>();
private User user;
private Email email;
public Users()
{ menu();}
private void menu()
{ char c = readChoice();
while (!isEnd(c))
{ execute(c);
c = readChoice();}}
private char readChoice()
{ System.out.print("Choice (a/d/g/u/x): ");
return In.nextChar();}
private void execute(char c)
{ switch(c)
{ case 'a': add(); break;
case 'd': delete(); break;
case 'g': break;
case 'u': use(); break;
default : System.out.println(" Invalid choice");}}
private void use()
{ User user = new User(this);
if (exists(user.getName()))
user.use();
else
System.out.println(" No such user"); }
public void send()
{ User user = user(readRecipient());
String header = readHeader();
String message = readMessage();
if (user != null)
{ user.add(new Email(user, header, message));}
//user.look();this method is just for tes if it is included
// in the method above it lists the messages.
else
System.out.println("No such adress");}
}
我的用户类
public class User
{
private String name;
private Users users;
private LinkedList<Email> emails = new LinkedList<Email>();
public User(Users users)
{ this.users = users;
this.name = readName();}
public void use()
{ char c = readChoice();
while (!isEnd(c))
{ execute(c);
c = readChoice();}}
private char readChoice()
{ System.out.print(" Choice (l/r/s/d/x): ");
return In.nextChar();}
private void execute(char c)
{ switch(c)
{ case 'l': look(); break;
case 'r': read(); break;
case 's': send(); break;
default : System.out.println(" Invalid choice");}}
public void look()
{ String s = " ";
s += name + " has " + emailSize() + " messages";
System.out.println(s);}
public void add(Email email)
{ emails.add(email);}
private void send()
{ users.send();}
}
最后一堂课电邮:
package assignment;
public class Email {
private String header;
private String message;
private User user;
public Email(User user, String header, String message)
{ this.user = user;
this.header = header;
this.message = message;}
}
答案 0 :(得分:0)
您的send
方法会将电子邮件归属于收件人,并采取相应措施:
public void send()
{ User user = user(readRecipient());
String header = readHeader();
String message = readMessage();
if (user != null)
{ user.add(new Email(user, header, message));
user.look();}
else
System.out.println("No such adress");}
}
您致电user.look()
,用户为User user = user(readRecipient());
因此,如果您想要比较“ apples to apples ”,您还需要在使用“ l ”选项时选择收件人 。