我编写了一个简单的邮件系统。该系统由四类组成:
- 项目清单
- 用户
- 用户(主类)
- 电子邮件组
我的问题是关于高三班的。在我的代码中,将以Users
class 开头并弹出一个菜单,您可以在其中添加、删除到系统作为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;}
}