在庫分析プログラムを考える
概要
在庫分析プログラムを作る。クラス継承の応用。
/*ものを表すItemクラス*/ class Item { private int count = 0;// 在庫数量 private int price = 0;// 単価 public int getCount() { return count; } public int getPrice() { return price; } public void setCount(int count) { this.count = count; } public void setPrice(int price) { this.price = price; } public int getAmount() { return price * count; } } class Book extends Item { Book(String input) { String[] data = input.split(","); setPrice(Integer.parseInt(data[3])); setCount(Integer.parseInt(data[4])); } public boolean isMany() { return getCount() >= 100; } } class DVD extends Item { DVD(String input) { String[] data = input.split(","); setPrice(Integer.parseInt(data[2])); setCount(Integer.parseInt(data[3])); } } public class ZaikoAnalyzer { public static void main(String[] args) { String[] lines = { "Book, カムイ伝, 白土三平, 500, 10", "DVD, インターステラー, 3000, 10", "Book, バタアシ金魚, 望月峯太郎, 500, 200" }; analyze(lines); } public static void analyze(String[] lines) { int countOFBooks = 0; int countOFManyBookTitle = 0; int countOFDVDs = 0; int amount = 0; for (String line : lines) { if (line.startsWith("Book")) { Book book = new Book(line); countOFBooks += book.getCount(); if (book.isMany()) { countOFManyBookTitle++; } amount += book.getAmount(); } else { DVD dvd = new DVD(line); countOFDVDs += dvd.getCount(); amount += dvd.getAmount(); } } System.out.println("本の総数=" + countOFBooks); System.out.println("DVDの総数=" + countOFDVDs); System.out.println("100冊以上在庫のある本のタイトル数=" + countOFManyBookTitle); System.out.println("総金額=" + amount); } }
実行結果
本の総数=210
DVDの総数=10
100冊以上在庫のある本のタイトル数=1
総金額=135000
カムイ伝 全15巻完結 (文庫版) [マーケットプレイス コミックセット]
- 作者: 白土三平
- 出版社/メーカー: 小学館
- メディア: 文庫
- 購入: 1人 クリック: 1回
- この商品を含むブログを見る
インターステラー ブルーレイ&DVDセット(初回限定生産/3枚組/デジタルコピー付) [Blu-ray]
- 出版社/メーカー: ワーナー・ブラザース・ホームエンターテイメント
- 発売日: 2015/04/08
- メディア: Blu-ray
- この商品を含むブログ (24件) を見る
- 作者: 望月峯太郎
- 出版社/メーカー: 講談社
- 発売日: 1986/05/15
- メディア: コミック
- クリック: 21回
- この商品を含むブログ (24件) を見る