Simplified version of number calculation by collecting information in an instance

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

Structural drawing.

スクリーンショット 2017-10-28 23.59.40.png

UserDataBean

package mapScore_plus_easy;

import java.util.List;

public class UserDataBean {

    private String userName;
    private List<String> scoresList;

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }


    public List<String> getScoresList() {
        return scoresList;
    }
    public void setScoresList(List<String> scoresList) {
        this.scoresList = scoresList;
    }

}

Main

package mapScore_plus_easy;

public class Main {
	public static void main(String[] args) {

		//Instantiate total point calculation class
		CalculateSum calculatesum = new CalculateSum();
		//Instantiate a class that packs information into a userDataBean instance and returns it
		ScoreReturn sr = new ScoreReturn();

		UserDataBean inputResult = sr.inputReturn("Tanaka");
		System.out.println(inputResult.getUserName() + inputResult.getScoresList());
		//Call the total point calculation method using the return value of the above method as an actual argument
		calculatesum.calculate(inputResult);

		UserDataBean inputResult2 = sr.inputReturn("Sato");
		System.out.println(inputResult2.getUserName() + inputResult2.getScoresList());
		//Call the total point calculation method using the return value of the above method as an actual argument
		calculatesum.calculate(inputResult2);
	}
}

Personally, I was a little worried and struggled with how to use the list setter. Create a sampleList as a new list, add values to it, and finally use it as a setter argument. (If you do not create a new list and use the value as a setter argument multiple times, the setter will be updated instead of add, so it will be rewritten each time.)

package mapScore_plus_easy;

import java.util.ArrayList;
import java.util.List;

public class ScoreReturn {

	//A method that sets values in userName and scoresList of userDataBean and returns its instance
	public UserDataBean inputReturn (String person_Name){
		UserDataBean userDataBean = new UserDataBean();
		userDataBean.setUserName(person_Name);

		//Create a sample list.
		List<String> sampleList = new ArrayList<String>();

		//If you initialize these random variables as fields,
		//Mr. Tanaka and Mr. Sato got exactly the same score, which is unnatural.
		int randomScore_Japanese = new java.util.Random().nextInt(101);
		int randomScore_Math     = new java.util.Random().nextInt(101);
		int randomScore_English  = new java.util.Random().nextInt(101);

		//Course list
		String score_japanese = Integer.toString(randomScore_Japanese);
		sampleList.add(("National language" + ":" + score_japanese ));

		String score_math = Integer.toString(randomScore_Math);
		sampleList.add("Math" + ":" + score_math );

		String score_eng = Integer.toString(randomScore_English);
		sampleList.add(("English" + ":" + score_eng ));

		userDataBean.setScoresList(sampleList);

		//Returns an instance that holds the information.
		return userDataBean;
	}
}

CalculateSum

package mapScore_plus_easy;

import java.util.List;


public class CalculateSum {
	UserDataBean userdatabean = new UserDataBean();

	public void calculate(UserDataBean userDataBean){

		 List<String> scoresList = userDataBean.getScoresList();

		int sumScore = 0;

		for(String element : scoresList){
//			String deletedElement =  element.replaceAll("[^0-9]","");
			//Split is more versatile in this case than replace.
			String[] splitedElement = element.split(":");

			String t = splitedElement[1];
			int integerElement = Integer.parseInt(t);
			sumScore += integerElement;
		}

		System.out.println(userDataBean.getUserName() + "The total score of" + sumScore + "Is a point");
	}
}

Execution result


Tanaka[National language:20,Math:8,English:19]
Mr. Tanaka's total score is 47 points
Sato[National language:37,Math:78,English:89]
Mr. Sato's total score is 204 points

List variables are easier to understand if they are named "○○ List" as a naming convention.

Recommended Posts

Simplified version of number calculation by collecting information in an instance
Get information in an instance and calculate numbers Standard version
Install by specifying the version of Django in the Docker environment
Collecting client information when an error occurs in a web application
openssl version information in ruby OPENSSL_VERSION
Pagination sorted by number of likes
Number of digits in numeric item