質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java EE

Java EE(Java Enterprise Edition)はJavaベースのテクノロジーとその相互運用の仕様をまとめたものです。サーバとクライアントのアーキテクチャを規定し、特定アプリケーションのクラス用に定義されたテクノロジー設定のプロファイルを使用します。

JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

5826閲覧

リスト内の数値の合計の出し方が分かりません

icecleam

総合スコア46

Java EE

Java EE(Java Enterprise Edition)はJavaベースのテクノロジーとその相互運用の仕様をまとめたものです。サーバとクライアントのアーキテクチャを規定し、特定アプリケーションのクラス用に定義されたテクノロジー設定のプロファイルを使用します。

JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2020/08/29 16:25

編集2020/08/30 01:38

以下のadd.jspで点数を入力し、[明細追加]ボタンを押すと、プルダウンで選択した商品データがリストとして追加されていき、その点数*単価で小計を出すところまで実装したのですが、その各商品の小計の合計を同一画面内で常に表示させたいのですが、以下のソースだと常に0が出力されてしまいますので、合計の出し方を教えていただきたいです。

申し訳ありませんが、よろしくおねがいします。

RecordManager.javaのcalTotalメソッド

java

1 public static int calTotal(int subtotal) { 2 int total = 0; 3 for(Item item:list) { 4 total += item.getSubtotal(); 5 } 6 return total; 7 //return 0; 8 }

add.jsp

<%@ page session="false" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.time.LocalDate"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="/sales/resources/css/common.css" /> <title>売上システム(初期画面)</title> </head> <body> <form:form modelAttribute="salesForm" action="/sales/system" var="list"> <div class="header"> <span class="titleName">オンラインショップ</span> <div class="date"><%=LocalDate.now()%></div> </div> <div class="main"></div> <div>明細追加画面</div> 商品: <form:select path="goodsName" items="${nameList}" /> <div> 点数: <form:input path="quantity" items="${allList}"/> </div> <div> <input type="submit" name="add" value="明細追加" /> </div> <table> <tr> <th>削除</th> <th>商品ID</th> <th>商品名</th> <th>単価</th> <th>点数</th> <th>小計</th> </tr> <c:forEach items="${allList}" var="item" varStatus="status"> <tr class="even"> <form:hidden path="quantity" /> <td><input type="radio" name="remove"/></td> <td><c:out value="${item.id}" /></td> <td><c:out value="${item.name}" /></td> <td><c:out value="${item.price}" /></td> <td><c:out value="${item.quantity}" /></td> <td><c:out value="${item.subtotal}" /></td> </tr> </c:forEach> </table>           合計: <c:out value="${salesForm.total}" /> <input type="submit" name="firm" value="確定" /> <input type="submit" name="remove" value="削除" /> </form:form> </body> </html>

SalesSystemController.java

java

1package jp.practice.sales; 2 3import org.springframework.stereotype.Controller; 4import org.springframework.ui.Model; 5import org.springframework.web.bind.annotation.RequestMapping; 6import org.springframework.web.bind.annotation.SessionAttributes; 7 8/** 9 * Handles requests for the application home page. 10 */ 11@Controller 12@RequestMapping(value = "/system") 13@SessionAttributes 14public class SalesSystemController { 15 16 17 /** 18 * 初期画面から呼ばれる。選択した商品の明細追加画面を表示する。 19 * @param form フォームオブジェクト 20 * @param model モデルオブジェクト 21 * @return Viewとしてadd.jspを指定 22 */ 23 @RequestMapping(params = "add") 24 public String add(SalesForm form, Model model) { 25 Item item = RecordManager.findItem(form.getGoodsName()); 26 RecordManager.setNewItem(new Item(item.getId(), form.getGoodsName(), item.getPrice(), form.getQuantity(), item.getSubtotal())); 27 Item newItem = RecordManager.getNewItem(); 28 int sb = newItem.getPrice() * newItem.getQuantity(); 29 newItem.setSubtotal(sb); 30 RecordManager.addToAllList(newItem); 31 model.addAttribute("total", RecordManager.calTotal(sb)); 32 model.addAttribute("nameList", RecordManager.makeNameList()); 33 model.addAttribute("allList", RecordManager.getallList()); 34 35 if (1 <= form.getQuantity() && form.getQuantity() <= 100) { 36 model.addAttribute("message2", ADDMSG); 37 return "add"; 38 } else { 39 model.addAttribute("message", ERRMSG); 40 return "add"; 41 } 42 } 43 44 /** 45 * 確認画面で登録を押した時に呼ばれる。データを登録して初期画面表示へ遷移する。 46 * @param form フォームオブジェクト 47 * @param model モデルオブジェクト 48 * @return initメソッドへのリンクを指定 49 */ 50 @RequestMapping(params = "toInit") 51 public String toInit(SalesForm form, Model model) { 52 //RecordManager.findItem(name); 53 return init(form, model); 54 } 55} 56 57

SalesForm.java

java

1package jp.practice.sales; 2 3import java.util.ArrayList; 4import java.util.List; 5 6public class SalesForm { 7 8 /** 商品ID */ 9 private String id; 10 /** 商品名 */ 11 private String goodsName; 12 /** 単価 */ 13 private int price; 14 /** 売上点数 */ 15 private int quantity; 16 /** 小景 */ 17 private int subtotal; 18 /** 商品点数 */ 19 private int point; 20 /** 削除番号 */ 21 private int delNumber; 22 /** 合計 */ 23 private int total; 24 25 26 27 List<Item> allList = new ArrayList<>(); 28 29 public SalesForm() { 30 31 } 32 /** 33 * @return id 34 */ 35 public String getId() { 36 return id; 37 } 38 39 /** 40 * @param id セットする id 41 */ 42 public void setId(String id) { 43 this.id = id; 44 } 45 /** 46 * @return price 47 */ 48 public int getPrice() { 49 return price; 50 } 51 52 /** 53 * @param price セットする price 54 */ 55 public void setPrice(int price) { 56 this.price = price; 57 } 58 59 /** 60 * @return quantity 61 */ 62 public int getQuantity() { 63 return quantity; 64 } 65 66 /** 67 * @param quantity セットする quantity 68 */ 69 public void setQuantity(int quantity) { 70 this.quantity = quantity; 71 } 72 73 /** 74 * @return subtotal 75 */ 76 public int getSubtotal() { 77 return subtotal; 78 } 79 80 public void setSubtotal(int subtotal) { 81 82 this.subtotal = subtotal; 83 } 84 85 /** 86 * @return goodsName 87 */ 88 public String getGoodsName() { 89 return goodsName; 90 } 91 92 /** 93 * @param goodsName セットする goodsName 94 */ 95 public void setGoodsName(String goodsName) { 96 this.goodsName = goodsName; 97 } 98 99 /** 100 * @return point 101 */ 102 public int getPoint() { 103 return point; 104 } 105 106 /** 107 * @param point セットする point 108 */ 109 public void setPoint(int point) { 110 this.point = point; 111 } 112 113 114 public int getDelNumber() { 115 return delNumber; 116 } 117 118 public void setDelNumber(int delNumber) { 119 this.delNumber = delNumber; 120 } 121 122 123 public int getTotal() { 124 return total; 125 } 126 127 public void setTotal(int total) { 128 this.total = total; 129 } 130 131 /** 132 * @return allList 133 */ 134 public List<Item> getAllList() { 135 return allList; 136 } 137 138}

RecordManager.java

java

1package jp.practice.sales; 2 3import java.util.ArrayList; 4import java.util.List; 5 6/** 7 * 商品データ全体を保持するクラス 8 */ 9public final class RecordManager { 10 11 /** 明細データ */ 12 static List<Item> allList = new ArrayList<>(); 13 14 /** 商品データ */ 15 private static final List<Item> list = new ArrayList<Item>(); 16 static { 17 list.add(new Item("A00101", "油性ボールペン", 60, 0, 0)); 18 list.add(new Item("A00201", "極細ボールペン", 120, 0, 0)); 19 list.add(new Item("A00301", "蛍光ペン6色セット", 420, 0, 0)); 20 list.add(new Item("A00401", "シャープペンシル", 100, 0, 0)); 21 list.add(new Item("A00501", "鉛筆H(1ダース)", 400, 0, 0)); 22 23 } 24 25 /** 26 * 更新した商品データ 27 * 確認ボタンを押すとlistに書き込む。 28 */ 29 private static Item newItem; 30 31 /** 32 * 引数で指定された商品名に一致する商品データを返却 33 * @param name 検索キーとなる商品名 34 * @return 検索結果の商品データ 35 */ 36 public static Item findItem(String goodsName) { 37 int index = list.indexOf(new Item("", goodsName, 0,0,0)); 38 return list.get(index); 39 } 40 41 /** 42 * 商品データを返す。 43 * @return 商品データ 44 */ 45 public static List<Item> getNameist() { 46 return list; 47 } 48 49 /** 50 * 明細データを返す。 51 * @return 明細データ 52 */ 53 public static List<Item> getallList() { 54 return allList; 55 } 56 57 /** 58 * リストに商品情報を 1 件追加する 59 */ 60 public static void addToAllList(Item newItem) { 61 Item item = new Item(newItem.getId(), newItem.getName(), newItem.getPrice(),newItem.getQuantity(),newItem.getSubtotal()); 62 allList.add(item); 63 } 64 65 public static void removeToAllList(Item newItem) { 66 Item item = new Item(newItem.getId(), newItem.getName(), newItem.getPrice(),newItem.getQuantity(),newItem.getSubtotal()); 67 allList.remove(item); 68 } 69 70 /** 71 * newItemを商品データlistに書き込む。 72 */ 73 public static void updateItem() { 74 list.set(list.indexOf(newItem), newItem); 75 } 76 77 /** 78 * newItemにデータを書き込む 79 * @param ni セットする item 80 */ 81 public static void setNewItem(Item ni) { 82 newItem = ni; 83 } 84 85 public static Item getNewItem() { 86 return newItem; 87 } 88 public static String[] makeNameList() { 89 String[] nameList = new String[list.size()]; 90 for (int i = 0; i < list.size(); i++) { 91 nameList[i] = (list.get(i)).getName(); 92 } 93 return nameList; 94 } 95 96 public static Item selectItem(String name) { 97 98 for(Item item:list) { 99 if (item.getName().equals(name)) { 100 return item; 101 } 102 } 103 return null; 104 } 105 106 public static int calTotal(int subtotal) { 107 int total = 0; 108 for(Item item:list) { 109 total += item.getSubtotal(); 110 } 111 return total; 112 } 113} 114

Item.java

java

1package jp.practice.sales; 2 3public class Item { 4 5 /** 商品ID */ 6 private String id; 7 /** 商品名 */ 8 private String name; 9 /** 単価 */ 10 private int price; 11 /** 売上点数 */ 12 private int quantity; 13 /** 小景 */ 14 private int subtotal; 15 16 public Item(String id, String name, int price, int quantity, int subtotal) { 17 this.id = id; 18 this.name = name; 19 this.price = price; 20 this.quantity = quantity; 21 this.subtotal = subtotal; 22 } 23 24 public String getId() { 25 return id; 26 } 27 28 public void setId(String id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public int getPrice() { 41 return price; 42 } 43 44 public void setPrice(int price) { 45 this.price = price; 46 } 47 48 public int getQuantity() { 49 return quantity; 50 } 51 52 public void setQuantity(int quantity) { 53 this.quantity = quantity; 54 } 55 56 public int getSubtotal() { 57 return subtotal; 58 } 59 60 /** 61 * @param subtotal セットする subtotal 62 */ 63 public void setSubtotal(int subtotal) { 64 this.subtotal = subtotal; 65 } 66 67 68 69 70}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Daregada

2020/08/30 00:36

SalesSystemController.java に「Item newItem = RecordManager.getNewItem();」とあるにもかかわらず、RecordManager.javaには getNewItem が定義されていませんね。実際のソースではないのでは?
icecleam

2020/08/30 01:25

ありがとうございます。 質問をする際に、見やすくするつもりが必要な箇所まで消してしまったようですね、申し訳ありません。。 以下がRecordManager.javaになります。 package jp.practice.sales; import java.util.ArrayList; import java.util.List; /** * 商品データ全体を保持するクラス */ public final class RecordManager { /** 明細データ */ static List<Item> allList = new ArrayList<>(); /** 商品データ */ private static final List<Item> list = new ArrayList<Item>(); static { list.add(new Item("A00101", "油性ボールペン", 60, 0, 0)); list.add(new Item("A00201", "極細ボールペン", 120, 0, 0)); list.add(new Item("A00301", "蛍光ペン6色セット", 420, 0, 0)); list.add(new Item("A00401", "シャープペンシル", 100, 0, 0)); list.add(new Item("A00501", "鉛筆H(1ダース)", 400, 0, 0)); list.add(new Item("B00101", "無線綴ノートA4", 100, 0, 0)); list.add(new Item("B00201", "リングノートA4", 120, 0, 0)); list.add(new Item("B00301", "領収書", 350, 0, 0)); list.add(new Item("C00101", "はさみ(青)", 128, 0, 0)); list.add(new Item("C00201", "ステープラー", 338, 0, 0)); list.add(new Item("C00301", "2穴パンチ", 128, 0, 0)); list.add(new Item("C00401", "ゼムクリップ", 98, 0, 0)); list.add(new Item("C00501", "消しゴム", 58, 0, 0)); } /** * 更新した商品データ * 確認ボタンを押すとlistに書き込む。 */ private static Item newItem; /** * 引数で指定された商品名に一致する商品データを返却 * @param name 検索キーとなる商品名 * @return 検索結果の商品データ */ public static Item findItem(String goodsName) { int index = list.indexOf(new Item("", goodsName, 0,0,0)); return list.get(index); } /** * 商品データを返す。 * @return 商品データ */ public static List<Item> getNameist() { return list; } /** * 明細データを返す。 * @return 明細データ */ public static List<Item> getallList() { return allList; } /** * リストに商品情報を 1 件追加する */ public static void addToAllList(Item newItem) { Item item = new Item(newItem.getId(), newItem.getName(), newItem.getPrice(),newItem.getQuantity(),newItem.getSubtotal()); allList.add(item); } /** * リストに商品情報を 1 件削除する */ public static void removeToAllList(Item newItem) { Item item = new Item(newItem.getId(), newItem.getName(), newItem.getPrice(),newItem.getQuantity(),newItem.getSubtotal()); allList.remove(item); } /** * newItemを商品データlistに書き込む。 */ public static void updateItem() { list.set(list.indexOf(newItem), newItem); } /** * newItemにデータを書き込む * @param ni セットする item */ public static void setNewItem(Item ni) { newItem = ni; } public static Item getNewItem() { return newItem; } /** * 全員の名前のリストを返す。 * @return 名前の配列 */ public static String[] makeNameList() { String[] nameList = new String[list.size()]; for (int i = 0; i < list.size(); i++) { nameList[i] = (list.get(i)).getName(); } return nameList; } /** * 名前で検索し、その商品のデータを返す。 * @param name 商品名 * @return 一商品データ */ public static Item selectItem(String name) { for(Item item:list) { if (item.getName().equals(name)) { return item; } } return null; } public static int calTotal(int subtotal) { int total = 0; for(Item item:list) { total += item.getSubtotal(); } return total; } }
Daregada

2020/08/30 01:33

質問は編集可能ですから、質問中のRecordManager.javaを置き換えてください。ここに書いても隠れてしまうので。
icecleam

2020/08/30 01:40

今、編集しました! その際に文字数制限で引っかかってしまったので、RecordManager.java の listの項目を少し削らせていただきましたので、その部分だけ少しだけ差異があります。 ご指摘いただきありがとうございました。
guest

回答1

0

ベストアンサー

原因になってそうのはこの辺りでしょうか

SalesSystemController.java

@RequestMapping(params = "add") public String add(SalesForm form, Model model) { model.addAttribute("total", RecordManager.calTotal(RecordManager.getallList())); }

RecordManager.java

public static int calTotal(List<Item> list) { int total = 0; for(Item item: list) { total += item.getSubtotal(); } return total; }

add.jsp

合計: <c:out value="${total}" />

投稿2020/08/30 02:01

ihigaku

総合スコア71

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

icecleam

2020/08/30 02:04

そうですね 合計を出す際にはその箇所が関連しているので、原因はそこにあると思います。 いろいろ試したのですが、うまくいかずにご質問させていただきました…
ihigaku

2020/08/30 02:38

原因っぽい箇所変えてみたので、試してみてくださいという意味でした。 言葉足りずすみません
icecleam

2020/08/30 03:39

いえいえ、そういうことだったのですね! こちらの確認不足ですので、気にしないでください! それと 上記のコードで、無事に実装することができました! 重ね重ねありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問