Get information in an instance and calculate numbers Standard version

Create an instance that holds the student name and the score of 5 subjects, output and calculate the total score

The structure diagram is as follows.

スクリーンショット 2017-10-29 0.15.46.png

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

Get information in an instance and calculate numbers Standard version
Simplified version of number calculation by collecting information in an instance
Get location information in Rails and sort in ascending order
Get EXIF information in Java
Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3
Calculate prime numbers in Java
Get attributes and values from an XML file in Java
openssl version information in ruby OPENSSL_VERSION
Get YouTube video information with Retrofit and keep it in the Android app.