likikalkulus

Untitled

Jan 21st, 2021
588
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Random;
  5.  
  6. public class Shopping {
  7.     public static void main(String[] args) throws IOException {
  8.         int count = 0;
  9.         Item[] items = new Item[10];
  10.  
  11.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  12.         Random random = new Random();
  13.         for (int i = 0; i < 10; i++) {
  14.             System.out.print("Would you like to shop more (1/0)?  ");
  15.             int answer = Integer.parseInt(bufferedReader.readLine());
  16.  
  17.             if (answer == 0)
  18.                 break;
  19.  
  20.             int nextBuy = random.nextInt(items.length);
  21.             items[i] = allItems[nextBuy];
  22.             count++;
  23.         }
  24.  
  25.         int sum = 0;
  26.         for (int i = 0; i < count; i++) {
  27.             sum += items[i].price;
  28.             System.out.println(items[i]);
  29.         }
  30.         System.out.println("Total price: " + sum);
  31.  
  32.     }
  33.  
  34.     public static Item[] allItems = {
  35.             new Item("Jeans", 10000),
  36.             new Item("Blouse", 600),
  37.             new Item("T-Shirt", 300),
  38.             new Item("Sportswear", 2000),
  39.             new Item("Running shoes", 4500),
  40.             new Item("Socks", 100),
  41.             new Item("Shirt", 800),
  42.             new Item("Pullover", 650),
  43.             new Item("Shorts", 900),
  44.             new Item("Belt", 200)
  45.     };
  46. }
  47.  
  48. class Item {
  49.     public String name;
  50.     public int price;
  51.  
  52.     public Item(String name, int price) {
  53.         this.name = name;
  54.         this.price = price;
  55.     }
  56.  
  57.     @Override
  58.     public String toString() {
  59.         return "Item{" +
  60.                 "name='" + name + '\'' +
  61.                 ", price=" + price +
  62.                 '}';
  63.     }
  64. }
  65.  
RAW Paste Data