The structure diagram is as follows.
UserDataBean
package mapScore_plus_rere;
import java.util.List;
public class UserDataBean {
private String userName;
private List<ScoreBean> scoresList;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public List<ScoreBean> getScoresList() {
return scoresList;
}
public void setScoresList(List<ScoreBean> scoresList) {
this.scoresList = scoresList;
}
}
ScoreBean
package mapScore_plus_rere;
public class ScoreBean {
private String scoreName;
private int score;
public String getScoreName() {
return scoreName;
}
public void setScoreName(String scoreName) {
this.scoreName = scoreName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
Main
package mapScore_plus_rere;
public class Main {
public static void main(String[] args) {
ScoreReturn scoreReturn = new ScoreReturn();
CalculateSum calc = new CalculateSum();
//Set the argument name (Sato) to the variable of userDataBean&
//Set the subject name and score in the variable of scoreBean
//Set the list of that scoreBean instance to the List variable of userDataBean
UserDataBean udb1 = scoreReturn.returnUDB("Sato");
UserDataBean udb2 = scoreReturn.returnUDB("Tanaka");
//Calculate the instance received as the return value&Pass to output method
calc.calculate(udb1);
System.out.println("");
calc.calculate(udb2);
}
}
ScoreReturn Since the same process continues for the number of subjects, it is cleaner to put it together as a method. (Since the processing is the same except for the subject name and score, it is unified. It is easier to read and respond to changes) Whether this class is good or bad, I think it makes a difference depending on the person who writes the most.
package mapScore_plus_rere;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class ScoreReturn {
public UserDataBean returnUDB(String userName) {
UserDataBean userDataBean = new UserDataBean();
//First, set the Name
userDataBean.setUserName(userName);
//List generation to be a setter argument
List<ScoreBean> SCList = new LinkedList<ScoreBean>();
Random random = new Random();
//National language
SCList.add(set_and_return_scoreName_and_score("National language", random.nextInt(101)));
//Math
SCList.add(set_and_return_scoreName_and_score("Math", random.nextInt(101)));
//English
SCList.add(set_and_return_scoreName_and_score("English", random.nextInt(101)));
//Science
SCList.add(set_and_return_scoreName_and_score("Science", random.nextInt(101)));
//society
SCList.add(set_and_return_scoreName_and_score("society", random.nextInt(101)));
userDataBean.setScoresList(SCList);
return userDataBean;
}
//The same process continues 5 times, so put it together in a method
public ScoreBean set_and_return_scoreName_and_score(String scoreName, int score) {
ScoreBean scoreBean = new ScoreBean();
scoreBean.setScoreName(scoreName);
scoreBean.setScore(score);
return scoreBean;
}
}
CalculateSum In order to write a for statement, I thought again that I needed to understand the structural diagram properly.
package mapScore_plus_rere;
public class CalculateSum {
public void calculate(UserDataBean udb) {
udb.getUserName();
int scoreSum = 0;
for (ScoreBean element : udb.getScoresList()) {
element.getScoreName();
int score = element.getScore();
System.out.println(element.getScoreName() + "The score of" + score + "is");
scoreSum += score;
}
// System.out.println("mapScore_plus_rere");
System.out.println(udb.getUserName() + "The total score of" + scoreSum + "is");
}
}
Recommended Posts