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

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

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

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

Q&A

解決済

1回答

2064閲覧

for文中のreturn文について

icecleam

総合スコア46

Java

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

0グッド

0クリップ

投稿2020/08/25 15:26

以下のselectItemメソッドでitemを返したいのですが、

returnを記載していても
「このメソッドは型 Item の結果を戻す必要があります」
というエラーが出てしまいます。

itemを返しつつ解決する方法を教えて頂きたいです。
必要なソースは以下に記載しておきます。

selectItemメソッド

java

1 public static Item selectItem(String name) { 2 3 //Item item = new Item(name,"",0,0,0); 4 5 for(Item item:list) { 6 if (item.getName().equals(name)) { 7 return item; 8 } 9 } 10 //Item item = new Item(name,"",0,0,0); 11     //return list.get(list.indexOf(item)); 12     //return item; 13 }

RecordManager

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 private static final List<Item> list = new ArrayList<Item>(); 13 static { 14 list.add(new Item("A00101", "油性ボールペン", 60, 0, 0)); 15 list.add(new Item("A00201", "極細ボールペン", 120, 0, 0)); 16 list.add(new Item("A00301", "蛍光ペン6色セット", 420, 0, 0)); 17 list.add(new Item("A00401", "シャープペンシル", 100, 0, 0)); 18 list.add(new Item("A00501", "鉛筆H(1ダース)", 400, 0, 0)); 19 list.add(new Item("B00101", "無線綴ノートA4", 100, 0, 0)); 20 list.add(new Item("B00201", "リングノートA4", 120, 0, 0)); 21 list.add(new Item("B00301", "領収書", 350, 0, 0)); 22 list.add(new Item("C00101", "はさみ(青)", 128, 0, 0)); 23 list.add(new Item("C00201", "ステープラー", 338, 0, 0)); 24 list.add(new Item("C00301", "2穴パンチ", 128, 0, 0)); 25 list.add(new Item("C00401", "ゼムクリップ", 98, 0, 0)); 26 list.add(new Item("C00501", "消しゴム", 58, 0, 0)); 27 } 28 29 /** 30 * 引数で指定された商品名に一致する商品データを返却 31 * @param name 検索キーとなる商品名 32 * @return 検索結果の商品データ 33 */ 34 public static Item findItem(String goodsName) { 35 int index = list.indexOf(new Item("", goodsName, 0,0,0)); 36 return list.get(index); 37 } 38 39 /** 40 * 商品データを返す。 41 * @return 商品データ 42 */ 43 public static List<Item> getNameist() { 44 return list; 45 } 46 47 /** 48 * 全員の名前のリストを返す。 49 * @return 名前の配列 50 */ 51 public static String[] makeNameList() { 52 String[] nameList = new String[list.size()]; 53 for (int i = 0; i < list.size(); i++) { 54 nameList[i] = (list.get(i)).getName(); 55 } 56 return nameList; 57 } 58 59 /** 60 * 名前で検索し、その人のスケジュールデータを返す。 61 * @param name 商品名 62 * @return 一商品データ 63 */ 64 public static Item selectItem(String name) { 65 66 //Item item = new Item(name,"",0,0,0); 67 68 for(Item item:list) { 69 if (item.getName().equals(name)) { 70 return item; 71 } 72 } 73 //Item item = new Item(name,"",0,0,0); 74     //return list.get(list.indexOf(item)); 75     //return item; 76 } 77}

Item

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 /** 25 * @return id 26 */ 27 public String getId() { 28 return id; 29 } 30 31 /** 32 * @param id セットする id 33 */ 34 public void setId(String id) { 35 this.id = id; 36 } 37 38 /** 39 * @return name 40 */ 41 public String getName() { 42 return name; 43 } 44 45 /** 46 * @param name セットする name 47 */ 48 public void setName(String name) { 49 this.name = name; 50 } 51 52 /** 53 * @return price 54 */ 55 public int getPrice() { 56 return price; 57 } 58 59 /** 60 * @param price セットする price 61 */ 62 public void setPrice(int price) { 63 this.price = price; 64 } 65 66 /** 67 * @return quantity 68 */ 69 public int getQuantity() { 70 return quantity; 71 } 72 73 /** 74 * @param quantity セットする quantity 75 */ 76 public void setQuantity(int quantity) { 77 this.quantity = quantity; 78 } 79 80 /** 81 * @return subtotal 82 */ 83 public int getSubtotal() { 84 return subtotal; 85 } 86 87 88 89 /** 90 * @param subtotal セットする subtotal 91 */ 92 public void setSubtotal(int subtotal) { 93 this.subtotal = subtotal; 94 } 95 /* (非 Javadoc) 96 * @see java.lang.Object#hashCode() 97 */ 98 @Override 99 public int hashCode() { 100 final int prime = 31; 101 int result = 1; 102 result = prime * result + ((name == null) ? 0 : name.hashCode()); 103 return result; 104 } 105 /* (非 Javadoc) 106 * @see java.lang.Object#equals(java.lang.Object) 107 */ 108 @Override 109 public boolean equals(Object obj) { 110 if (this == obj) { 111 return true; 112 } 113 if (obj == null) { 114 return false; 115 } 116 if (!(obj instanceof Item)) { 117 return false; 118 } 119 Item other = (Item) obj; 120 if (name == null) { 121 if (other.name != null) { 122 return false; 123 } 124 } else if (!name.equals(other.name)) { 125 return false; 126 } 127 return true; 128 } 129}

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

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

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

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

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

m.ts10806

2020/08/26 00:42

解決されたようですが解決したようには思えません。 「一致しないときにどうするか」 設計どうなってますか?それ次第で作り方も違います。
guest

回答1

0

ベストアンサー

java

1public static Item selectItem(String name) { 2 for(Item item:list) { 3 if (item.getName().equals(name)) { 4 return item; 5 } 6 } 7 // 同じ名前がなかったときはnullを返す 8 return null; 9}

が一番単純な解決法ですが、nullを扱いたくない場合は、

  • Optionalを使う
  • 空のItemを作成するコンストラクタを別途作成し、空であることを判定するロジックを設ける

ができます。

投稿2020/08/25 16:10

A-pZ

総合スコア12011

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問