LoginSignup
0
1

More than 5 years have passed since last update.

インスタンスに情報詰めて取得して数計算 標準版

Last updated at Posted at 2017-10-28

生徒名と5教科の点数を保持するインスタンスをつくり、出力と合計点計算

構造図は以下になります。

スクリーンショット 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();

        //引数の名前(佐藤)をuserDataBeanの変数にセットする &
        //scoreBeanの変数に科目名と点数をセットして
        //そのscoreBeanインスタンスのリストをuserDataBeanのList変数にセットする
        UserDataBean udb1 = scoreReturn.returnUDB("佐藤");
        UserDataBean udb2 = scoreReturn.returnUDB("田中");

        //戻り値として受け取ったインスタンスを計算&出力メソッドに渡す
        calc.calculate(udb1);
        System.out.println("");
        calc.calculate(udb2);
    }
}

ScoreReturn
同じ処理が科目の数だけ続くので、メソッドとしてまとめた方が綺麗になりました。
(科目名と点数以外は同じ処理なので、一元化。その方が可読性が上がるし、変化に対応しやすい)
このクラスが良くも悪くも、もっとも書く人で違いが出てくるのかなと思います。


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();
        //まずはNameをセット
        userDataBean.setUserName(userName);

        //セッターの引数にするためのリスト生成
        List<ScoreBean> SCList = new LinkedList<ScoreBean>();

        Random random = new Random();

        //国語
        SCList.add(set_and_return_scoreName_and_score("国語", random.nextInt(101)));
        //数学
        SCList.add(set_and_return_scoreName_and_score("数学", random.nextInt(101)));
        //英語
        SCList.add(set_and_return_scoreName_and_score("英語", random.nextInt(101)));
        //理科
        SCList.add(set_and_return_scoreName_and_score("理科", random.nextInt(101)));
        //社会
        SCList.add(set_and_return_scoreName_and_score("社会", random.nextInt(101)));

        userDataBean.setScoresList(SCList);
        return userDataBean;
    }

    //同じ処理が5回続くので、メソッドにまとめる
    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
for文を書くためには、構造図をきちんと理解している必要があると改めて思いました。


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() + "の点数は" + score + "です");
            scoreSum += score;
        }
//      System.out.println("mapScore_plus_rere");
        System.out.println(udb.getUserName() + "の合計点は" + scoreSum + "です");
    }
}
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1