Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chegg_bar;
- import java.util.*;
- //creating an item class for holding each item information.
- class Item{
- private int type;
- private String description;
- private String barCode;
- //Creating getters and setters method.
- public void setType(int t){
- //Range of type is 1 to 100 as mentioned in question.
- if(t>=1 && t<=100)
- this.type = t;
- else
- System.out.println("Type is invalid");
- }
- public void setDescription(String d){
- this.description = d;
- }
- public void setBarCode(String b){
- this.barCode = b;
- }
- public int getType(){
- return this.type;
- }
- public String getDescription(){
- return this.description;
- }
- public String getBarCode(){
- return this.barCode;
- }
- //Creating the parametrized constructor.
- public Item(int a, String b, String c){
- this.type = a;
- this.description = b;
- this.barCode = c;
- }
- }
- public class Chegg_Bar {
- public static void main(String[] args) {
- //Creating various types of items using Item class.
- Item one = new Item(1,"Book","12345679");
- Item two = new Item(1,"Pen","2323234");
- Item three = new Item(1,"Pencil","342323");
- Item four = new Item(7,"Chair","23232");
- Item five = new Item(7,"Table","343323");
- Item six = new Item(7,"Sofa","32323");
- Item seven = new Item(7,"Door","232323");
- Item eight = new Item(7,"Almirah","23232");
- Item nine = new Item(10,"Maruti","2323");
- Item ten = new Item(10,"Hyundai","12123");
- Item eleven = new Item(10,"Suzuki","23242524");
- Item twelve = new Item(10,"Honda","23234");
- Item thirteen = new Item(10,"Ford","2325423");
- Item fourteen = new Item(10,"Volkwagen","45342");
- Item fifteen = new Item(10,"Mercedes","455223");
- Item sixteen = new Item(10,"Buccati","232424");
- //Creating an array list which will holds our all items.
- List<Item> itemList = new ArrayList<Item>();
- //Adding various items that is created earlier to arrayList.
- itemList.add(one);
- itemList.add(two);
- itemList.add(three);
- itemList.add(four);
- itemList.add(five);
- itemList.add(five);
- itemList.add(six);
- itemList.add(seven);
- itemList.add(eight);
- itemList.add(nine);
- itemList.add(ten);
- itemList.add(eleven);
- itemList.add(twelve);
- itemList.add(thirteen);
- itemList.add(fourteen);
- itemList.add(fifteen);
- itemList.add(sixteen);
- //Creating the count varibles for various types so as to extract exactly those no items of specific types from the list.
- int itemOneCount = 3;
- int itemSevenCount = 5;
- int itemTenCount = 10;
- //Looping throught array to extract the items
- for(Item e:itemList){
- //Checking if the item is 1, 7 or 10 .
- if(e.getType()==1 && itemOneCount>0){
- //Printing the barcode.
- System.out.println(e.getBarCode());
- itemOneCount--;
- }
- else if(e.getType()==7 && itemSevenCount>0){
- System.out.println(e.getBarCode());
- itemSevenCount--;
- }
- else if(e.getType()==10 && itemTenCount>0){
- System.out.println(e.getBarCode());
- itemTenCount--;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement