-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item2.java
135 lines (98 loc) · 2.53 KB
/
Item2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.util.* ;
class Item {
Integer itemId ;
String itemName ;
Item(Integer i , String s){
this.itemId = i ;
this.itemName = s ;
}
@Override
public String toString(){
return this.itemId+" - "+this.itemName+" " ;
}
@Override
public boolean equals(Object o){
if(o instanceof Item){
Item i3 = (Item)o ;
if(this.itemId == i3.itemId && this.itemName == i3.itemName ){
return true ;
}
}
return false ;
}
@Override
public int hashCode(){
int prime = 13;
int val = 1;
val = val*prime + this.itemId.hashCode();
val = val*prime + this.itemName.hashCode();
return val;
}
}
class Ids implements Comparator<Item>{
public int compare(Item i1,Item i2){
return i1.itemId.compareTo(i2.itemId) ;
}
}
class Names implements Comparator<Item>{
public int compare(Item i1,Item i2){
return i1.itemName.compareTo(i2.itemName) ;
}
}
class Item2 {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
ArrayList<Item> item = new ArrayList<>();
while(true){
System.out.println(" 1. Add Item ");
System.out.println(" 2. Display Items ");
System.out.println(" 3. Remove Item ");
System.out.println(" 4. Exit ");
System.out.println();
int choice = sc.nextInt();
switch(choice){
case 1 : {
System.out.println("Enter itemid and item name");
int i = sc.nextInt();
String n = sc.next();
Item i1 = new Item (i,n);
if (item.contains(i1)){
System.out.println("Item already exist ");
}else {
item.add(i1);
System.out.println("Item added successfully !! ");
}
break;
}
case 2 : {
Ids id = new Ids();
Collections.sort(item,id);
System.out.println("items sorted by id : "+item);
Names ns = new Names();
Collections.sort(item,ns);
System.out.println("items sorted by names : "+item);
break;
}
case 3 :{
System.out.println("Enter item to be removed :");
int i = sc.nextInt();
String n = sc.next();
Item i2 = new Item (i,n);
if (item.contains(i2)){
item.remove(i2);
}else {
System.out.println("item not available !");
}
break ;
}
case 4 : {
break ;
}
default : System.out.println("Please enter a valid choice !");
}
if (choice == 4){
break;
}
}
}
}