在我的应用程序中,我有两个名为Platform和另一个Organizer的类。
平台类
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Platform {
private List<User> users;
private List<Competition> competitions;
public Platform() {
/**
* PR1 Ex 2.1: We need to initialize the list of users
*/
users = new ArrayList<User>();
}
User findUser(String username) {
/**
* PR1 Ex 2.1: Implementation of method findUser to find a user in the list users by username
*/
User user = null;
Iterator<User> itr = this.users.iterator();
while(itr.hasNext() && user == null) {
User u = itr.next();
if(u.getUserName().equals(username)) {
user = u;
}
}
return user;
}
public User registerUser(String username, String password, String fullname) {
/**
* PR1 Ex 2.1: Register a new user, checking that it does not exist
*/
User newUser = null;
// Check if the user is new or already exists
User queryUser = findUser(username);
if(queryUser==null) {
newUser = new User(this, username, password, fullname);
this.users.add(newUser);
}
return newUser;
}
public User login(String username, String password) {
/**
* PR1 Ex 2.2: Login an already existing user
*/
User user = null;
// Find the user in the list of registered users
User queryUser = findUser(username);
// If the user exists, check the password
if(queryUser!=null && queryUser.checkPassword(password)) {
user = queryUser;
}
return user;
}
public Integer getNumUsers() {
/**
* PR1 Ex 2.1: Required for test, to check if a new user is registered
*/
return this.users.size();
}
public Integer getNumCompetitions() {
return null;
}
public Message sendMessage(User from, String to, String subject, String message) throws CompetitionException {
User receiver = findUser(to);
if (to == null) {
throw new CompetitionException(CompetitionException.SENDER_NOT_FOUND);
}
else if(receiver == null ) {
throw new CompetitionException(CompetitionException.SENDER_NOT_FOUND); //Aquí debes lanza el tipo de excepción apropiado de tu lógica de negocio
}
Message m = new Message(this, this, subject, message);
return m;
}
public void registerCompetition(Competition competition) {
}
public List<Competition> getOpenCompetitions() {
return null;
}
private void evaluateAll() {
}
public void run() {
// Simulates system call for evaluation
evaluateAll();
}
}
和课程组织者
import java.util.ArrayList;
import java.util.List;
public class Organizer extends User {
private List<Competition> competitions;
public Organizer(User user) {
super(user);
// Initialize the list of competitions
competitions = new ArrayList<Competition>();
}
public void removeSubmission(Submission submission) {
/* NOT IMPLEMENTED */
}
public boolean sendMessage(Competition competition, String subject, String message) {
/* NOT IMPLEMENTED */
return false;
}
public Competition newCompetition(String title, float target) {
Competition s =new Competition(platform, this, title, target);
}
public void closeCompetition(Competition competition) {
}
public List<Competition> getCompetitions() {
return this.competitions;
}
private void updateCompetitions() {
}
}
我想实现Organizer类的newCompetition方法,以便它通过a的参数提供数据 新竞争:
- 为新比赛实例化一个对象,然后使用registerCompetition在平台上注册比赛
类Organizer newCompetition的方法我有它
public Competition newCompetition(String title, float target) {
Competition s =new Competition(platform, this, title, target);
competitions.add(s);
return s;
}
如何使用平台的registerCompetition
方法注册竞赛?
我有以下类测试,我想知道如何实现这些方法来传递它们
班级考试
public class PR2_Ex2_1_Test {
private final String username1 = "username1";
private final String password1 = "password1";
private final String fullName1 = "Test User 1";
private final String username2 = "username2";
private final String password2 = "password2";
private final String fullName2 = "Test User 2";
private final String comp_title1 = "Approximate number PI";
private final float comp_target1 = 3.141592653589793238f;
private Platform platform = null;
private User u1 = null;
private User u2 = null;
public PR2_Ex2_1_Test() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
// Create the test scenario
platform = new Platform();
// Register new users
u1 = platform.registerUser(username1, password1, fullName1);
u2 = platform.registerUser(username2, password2, fullName2);
}
@After
public void tearDown() {
}
@Test
public void createCompetitionObject() {
Organizer o1 = u1.asOrganizer();
Competition c1 = o1.newCompetition(comp_title1, comp_target1);
assertNotNull(c1);
assertEquals(o1, c1.getOwner());
assertTrue(c1.isOpen());
assertNotNull(c1.getSubmissions());
assertEquals(0, c1.getSubmissions().size());
}
@Test
public void duplicatedCompetitionObjects() {
Organizer o1 = u1.asOrganizer();
Competition c1 = o1.newCompetition(comp_title1, comp_target1);
Competition c2 = o1.newCompetition(comp_title1, comp_target1);
// If id is correct, they cannot be equals
assertThat(c1, is(not(equalTo(c2))));
// Id cannot depend on the owner
Organizer o2 = u2.asOrganizer();
Competition c3 = o2.newCompetition(comp_title1, comp_target1);
assertThat(c1, is(not(equalTo(c3))));
}
@Test
public void AssociatedObjects() {
Organizer o1 = u1.asOrganizer();
Competition c1 = o1.newCompetition(comp_title1, comp_target1);
assertEquals(new Integer(1), platform.getNumCompetitions());
assertEquals(1, o1.getCompetitions().size());
}
}