Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. package chegg_bar;
  2. import java.util.*;
  3.  
  4. //creating an item class for holding each item information.
  5. class Item{
  6. private int type;
  7. private String description;
  8. private String barCode;
  9. //Creating getters and setters method.
  10. public void setType(int t){
  11. //Range of type is 1 to 100 as mentioned in question.
  12. if(t>=1 && t<=100)
  13. this.type = t;
  14. else
  15. System.out.println("Type is invalid");
  16. }
  17. public void setDescription(String d){
  18. this.description = d;
  19. }
  20. public void setBarCode(String b){
  21. this.barCode = b;
  22. }
  23. public int getType(){
  24. return this.type;
  25. }
  26. public String getDescription(){
  27. return this.description;
  28. }
  29. public String getBarCode(){
  30. return this.barCode;
  31. }
  32. //Creating the parametrized constructor.
  33. public Item(int a, String b, String c){
  34. this.type = a;
  35. this.description = b;
  36. this.barCode = c;
  37. }
  38. }
  39. public class Chegg_Bar {
  40. public static void main(String[] args) {
  41. //Creating various types of items using Item class.
  42. Item one = new Item(1,"Book","12345679");
  43. Item two = new Item(1,"Pen","2323234");
  44. Item three = new Item(1,"Pencil","342323");
  45. Item four = new Item(7,"Chair","23232");
  46. Item five = new Item(7,"Table","343323");
  47. Item six = new Item(7,"Sofa","32323");
  48. Item seven = new Item(7,"Door","232323");
  49. Item eight = new Item(7,"Almirah","23232");
  50. Item nine = new Item(10,"Maruti","2323");
  51. Item ten = new Item(10,"Hyundai","12123");
  52. Item eleven = new Item(10,"Suzuki","23242524");
  53. Item twelve = new Item(10,"Honda","23234");
  54. Item thirteen = new Item(10,"Ford","2325423");
  55. Item fourteen = new Item(10,"Volkwagen","45342");
  56. Item fifteen = new Item(10,"Mercedes","455223");
  57. Item sixteen = new Item(10,"Buccati","232424");
  58. //Creating an array list which will holds our all items.
  59. List<Item> itemList = new ArrayList<Item>();
  60. //Adding various items that is created earlier to arrayList.
  61. itemList.add(one);
  62. itemList.add(two);
  63. itemList.add(three);
  64. itemList.add(four);
  65. itemList.add(five);
  66. itemList.add(five);
  67. itemList.add(six);
  68. itemList.add(seven);
  69. itemList.add(eight);
  70. itemList.add(nine);
  71. itemList.add(ten);
  72. itemList.add(eleven);
  73. itemList.add(twelve);
  74. itemList.add(thirteen);
  75. itemList.add(fourteen);
  76. itemList.add(fifteen);
  77. itemList.add(sixteen);
  78. //Creating the count varibles for various types so as to extract exactly those no items of specific types from the list.
  79. int itemOneCount = 3;
  80. int itemSevenCount = 5;
  81. int itemTenCount = 10;
  82. //Looping throught array to extract the items
  83. for(Item e:itemList){
  84. //Checking if the item is 1, 7 or 10 .
  85. if(e.getType()==1 && itemOneCount>0){
  86. //Printing the barcode.
  87. System.out.println(e.getBarCode());
  88. itemOneCount--;
  89. }
  90. else if(e.getType()==7 && itemSevenCount>0){
  91. System.out.println(e.getBarCode());
  92. itemSevenCount--;
  93. }
  94. else if(e.getType()==10 && itemTenCount>0){
  95. System.out.println(e.getBarCode());
  96. itemTenCount--;
  97. }
  98. }
  99.  
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement