0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

集計ツールを作る2

Posted at

行動の目的

前回の記事より第二版を作成(前回記事は内容が薄いですが、あえて残します)
・前回と同じ機能を実現するにあたり、リストをフィールドに持たせることを思いついたので実行
・医療費の集計等にも利用できそうなので、別の用途も視野に入れつつコードを書く

何をしたか

下記3クラスを作成

購入品一点当たりの価格、名称を入力できるクラス

Item.java
package pack2521;

public class Item{                          //consume rev2 購入品クラス

    private String name;                    //名称
    private int value;                      //購入価格(円)

    public Item(){                          //コンストラクタ
        this(0);
    }

    public Item(int value){                 //コンストラクタ(購入価格のみ入力)
        this(null,value);
    }

    public Item(String name,int value){    //コンストラクタ(名称,購入価格)
        this.name = name;
        this.value = value;
    }

    public String getname(){               //名称を取得
        return this.name;
    }

    public int getvalue(){                 //購入価格を取得
        return this.value;
    }

}

Itemクラスを利用し、カテゴリ別に分けて合計金額を集計するクラス

Categorry.java
package pack2521;

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

public class Categorry{                             //Categorry rev2
    
    private int categorry;                          //カテゴリに割り当てる整数
    private String cName;                           //カテゴリの名称
    private List<Item> items = new ArrayList<>();   //カテゴリに属するアイテムの配列
    private int total;                              //合計金額

    public Categorry(){}                            //コンストラクタ

    public Categorry(int ctg){
        this(ctg,null);
    }

    public Categorry(int ctg,String name){          //コンストラクタ(カテゴリ, 名称)
        this.categorry = ctg;
        this.cName = name;
    }

    public void add(int value){                    //リストにアイテムを追加(購入価格)
        add(null,value);
    }

    public void add(String name,int value){        //リストにアイテムを追加(名称,購入価格)
        items.add(new Item(name,value));
        calc(value);
    }

    public void show(){                             //集計結果を出力
        if(this.categorry != 0)
        System.out.print("カテゴリ" + categorry + " / ");

        if(this.cName != null)
        System.out.print(cName + " / ");

        System.out.println("合計金額 : " + total + "円 / " + items.size() + "件");
    }

    private void calc(int value){                     //totalにvalueを加算
        this.total += value;
    }

    
    
}

Item、Categorryクラスを利用し、実行するクラス

Consume.java
package pack2521;

import java.util.Scanner;

public class Consume{                                      //家計消費状況調査メイン rev2
    public static void main(String[] args){
        Categorry[] array = new Categorry[22];
        Scanner stdIn = new Scanner(System.in);
        int c;                                             //カテゴリ/51~72
        int v;                                             //金額/円

        for(int i = 0 ; i < array.length ; i++){           //カテゴリ51を要素数0に~カテゴリ72を要素数21に生成
            int n = i + 51;
            array[i] = new Categorry(n);
        }


        n1:
        while(true){
            do{
                System.out.println("カテゴリを入力/51~72");
                c = stdIn.nextInt();                         //51~72をキーボード入力
            }while(51 > c || c > 72);

            do{
                System.out.println("金額を入力/円");
                v = stdIn.nextInt();                         //金額をキーボード入力
            }while(0 > v);

            int n = c - 51;                                  //リストにアイテムを追加
            array[n].add(v);
            
            n2:
            while(true){
                System.out.println("0/集計 1/続けて入力");
                n = stdIn.nextInt();

                switch(n){
                    case 0 : break n1;
                    case 1 : break n2;
                    default :
                }
            }
        }

        System.out.println("");
        System.out.println("--集計結果--");
        
        for(Categorry ctg : array){
            ctg.show();
        }
    }
}

行動の結果どう考えたか

・実際の使ってみた結果、集計か続けて入力か毎回選ぶのが面倒だった
・リストを採用した結果、実現できる機能の幅も増え、保守性も高まった。

次にやりたいこと

・0or1の入力を、カテゴリの整数を入力する部分と一体化したコードを考える
・Categorryクラスの、別のプログラムでの利用を考える

0
1
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?