Advertisement
kliba

ShoppingList

Mar 25th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.57 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class MainClassCreatedByIdea {
  5.  
  6.     public static void main(String[] args) {
  7.         /** This the documentation of this for cycle */
  8.         /* simple text */
  9.         // comment for me
  10.        
  11.         for (int i = 0; i < args.length; i++){
  12.             System.out.println("args[" + i + "] = " + args[i]);
  13.         }
  14.         int decVal = 26;
  15.         System.out.println("decVal = " + decVal);
  16.         int hexVal = 0x1b;
  17.         System.out.println("hexVal = " + hexVal);
  18.         int binVal = 0b0100010111;
  19.         System.out.println("binVal = " + binVal);
  20.        
  21.         float f1 = 432.4f;
  22.         System.out.println("f1 = " + f1);
  23.         double d1 = 1.23123;
  24.         System.out.println("d1 = " + d1);
  25.         System.out.println("-----------------------");
  26.         System.out.println(" Milne said \" this great\". This sentence \'i\'\'s\' full of chars");
  27.  
  28.         // 1 / 2 = 0!!! if we are using integers
  29.         System.out.println("-----------------------");
  30.         int a = 2;
  31.         int b = 1;
  32.  
  33.         System.out.println(b / a);
  34.         float c = b / a;
  35.         System.out.println(c);
  36.  
  37.         //Enhanced for statement
  38.         System.out.println("-----------------------");
  39.         int[] numbers = {1, 2, 3, 4, 5, 6};
  40.         for (int stg : numbers){
  41.             System.out.println("Stg is: " + stg);
  42.         }
  43.         // List creating
  44.         System.out.println("-----------------------");
  45.         List theList = new ArrayList();
  46.         theList.add("String");
  47.         theList.set(0, "String1 overwritten");
  48.         theList.remove(0);
  49.  
  50.         // Create a list where you put the same element more than once and literate over it enhance for!
  51.         //Create two lists and merge them into a third one by interweaving them
  52.         // Create a list of integers and separate even and odd numbers into two separate lists!
  53.         // Find the maximum element of the lists!
  54.         System.out.println("-----------------------");
  55.  
  56.         List<Integer> list1 = new ArrayList();
  57.         list1.add(0, 2);
  58.         list1.add(1, 2);
  59.         list1.add(2, 4);
  60.         list1.add(3, 5);
  61.         list1.add(4, 3);
  62.         List<Integer> list2 = new ArrayList();
  63.         list2.add(0, 5);
  64.         list2.add(1, 7);
  65.         list2.add(2, 9);
  66.         list2.add(3, 412);
  67.         list2.add(4, 444);
  68.  
  69.         for (Integer list2ToPrint : list2){
  70.             System.out.println("this is one element of list2: " + list2ToPrint);
  71.         }
  72.         System.out.println("-----------------------");
  73.         List<Integer> listSum = new ArrayList<>();
  74.  
  75.  
  76.             for (int i = 0; i < list2.size(); i++) {
  77.                 listSum.add(list1.get(i));
  78.                 listSum.add(list2.get(i));
  79.             }
  80.             System.out.println(listSum);
  81.  
  82.         System.out.println("-----------------------");
  83.         List<Integer> listOdd = new ArrayList<>();
  84.         List<Integer> listEven = new ArrayList<>();
  85.  
  86.         for (int i = 0; i < listSum.size(); i++){
  87.             if (listSum.get(i) % 2 == 1){
  88.                 listOdd.add(listSum.get(i));
  89.                 } else {
  90.                 listEven.add(listSum.get(i));
  91.             }
  92.         }
  93.         System.out.println("listOdd = " + listOdd);
  94.         System.out.println("listEven = " + listEven);
  95.         System.out.println("-----------------------");
  96.         // Create a set where you put the same element more than once and iterate over it with the enhanced for!
  97.         // Create two sets and merge them into a third and print the elements!
  98.         // Create a set of integers and separate even and odd numbers into two separate sets!
  99.         // Find the maximum element of the set!
  100.         Set<Integer> setContainer = new TreeSet();
  101.         setContainer.add(333);
  102.         setContainer.add(3);
  103.         setContainer.add(5);
  104.         setContainer.add(6);
  105.         setContainer.add(6);
  106.         setContainer.add(6);
  107.         setContainer.add(38);
  108.         setContainer.add(3);
  109.  
  110.         for (Object enhancedBehavior : setContainer){
  111.             System.out.println("enhancedBehavior = " + enhancedBehavior);
  112.         }
  113.  
  114.         Set<Integer> setContainer2 = new TreeSet();
  115.         setContainer2.add(3);
  116.         setContainer2.add(2);
  117.         setContainer2.add(77);
  118.         setContainer2.add(43);
  119.         setContainer2.add(55);
  120.         setContainer2.add(66);
  121.         setContainer2.add(88);
  122.  
  123.         Set<Integer> thirdContainer = new TreeSet();
  124.         for (Integer s1elem : setContainer){
  125.             thirdContainer.add(s1elem);
  126.         }
  127.         for (Integer s2elem : setContainer2){
  128.             thirdContainer.add(s2elem);
  129.         }
  130.         System.out.println("thirdContainer = " + thirdContainer);
  131.  
  132.         Set<Integer> oddSet = new TreeSet();
  133.         for (Integer oddNr : thirdContainer ){
  134.             if (oddNr % 2 == 1){
  135.                 oddSet.add(oddNr);
  136.             }
  137.         }
  138.  
  139.         System.out.println("oddSet = " + oddSet);
  140.        
  141.         Set<Integer> evenSet = new TreeSet();
  142.         for (Integer eventNr : thirdContainer){
  143.             if (eventNr % 2 == 0){
  144.                 evenSet.add(eventNr);
  145.             }
  146.         }
  147.  
  148.         System.out.println("evenSet = " + evenSet);
  149.  
  150.  
  151.         System.out.println("-----------------------");
  152.         //MAP using
  153.         //create map width my nex shopping list (key= product name, value = price)
  154.         //Print out my shopping list and the price of them
  155.         //store also the quantity of each shopping list netry and calculate price using aso the quality
  156.  
  157.         Map<String, Integer> shoppingList1 = new Hashtable<>();
  158.         shoppingList1.put("cheese", 1200);
  159.         shoppingList1.put("milk", 200);
  160.         shoppingList1.put("nutella", 1000);
  161.         shoppingList1.put("orange", 800);
  162.         shoppingList1.put("fish", 4200);
  163.         Map<String, Integer> list1qty = new Hashtable<>();
  164.         list1qty.put("cheese", 2);
  165.         list1qty.put("milk", 1);
  166.         list1qty.put("nutella", 1);
  167.         list1qty.put("orange", 6);
  168.         list1qty.put("fish", 3);
  169.  
  170.         for (Object key1 : shoppingList1.keySet()){
  171.             System.out.println("The " + key1 + " costs: " + shoppingList1.get(key1) * list1qty.get(key1) + " Huf");
  172.         }
  173.  
  174.         Map<String, Integer> shoppingList2 = new Hashtable<>();
  175.         shoppingList2.put("egg", 930);
  176.         shoppingList2.put("tuna", 1400);
  177.         shoppingList2.put("apple", 1200);
  178.         shoppingList2.put("tobacco", 1100);
  179.         shoppingList2.put("potato", 400);
  180.         Map<String, Integer> list2qty = new Hashtable<>();
  181.         list2qty.put("egg", 12);
  182.         list2qty.put("tuna", 2);
  183.         list2qty.put("apple", 6);
  184.         list2qty.put("tobacco", 1);
  185.         list2qty.put("potato", 22);
  186.  
  187.         for (Object key2 : shoppingList2.keySet()){
  188.             System.out.println("The " + key2 + " costs: " + shoppingList2.get(key2) * list2qty.get(key2) + " good Hungarian forint");
  189.         }
  190.  
  191.  
  192.         Map<String, Integer> shoppingList3 = new Hashtable<>();
  193.         shoppingList3.put("milk", 220);
  194.         shoppingList3.put("sugar", 380);
  195.         shoppingList3.put("wc_papper", 1400);
  196.         shoppingList3.put("cat_food", 2200);
  197.         shoppingList3.put("onion", 410);
  198.         Map<String, Integer> list3qty = new Hashtable<>();
  199.         list3qty.put("milk", 2);
  200.         list3qty.put("sugar", 1);
  201.         list3qty.put("wc_papper", 24);
  202.         list3qty.put("cat_food", 12);
  203.         list3qty.put("onion", 4);
  204.  
  205.         for (Object key3 : shoppingList3.keySet()){
  206.             System.out.println("This product costs " + shoppingList3.get(key3) * list3qty.get(key3) + " much money.");
  207.         }
  208.  
  209.  
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement