Java2 1
Java2 1
UID : 19BCS2141
Section : 19_NTPP_CI_2_A
AIM:
1. Write a program to collect and store all the cards to assist the users in
finding all the cards in a given symbol.
This cards game consist of N number of cards. Get N number of cards
details from the user and store the values in Card object with the attributes
symbol and number.
Store all the cards in a map with symbol as its key and list of cards as its
value. Map is used here to easily group all the cards based on their symbol.
Once all the details are captured print all the distinct symbols in
alphabetical order from the Map. For each symbol print all the card details,
number of cards and their sum respectively.
1. import java.util.*;
public class CollectCards {
protected ArrayList<Integer> array;
protected Map<String,ArrayList<Integer>> card;
public CollectCards(){
this.array = new ArrayList<Integer>();
this.card = new HashMap<String,ArrayList<Integer>>();
}
public void addCard(String name,int number) {
if(this.card.containsKey(name)) {
this.card.get(name).add(number);
}else {
this.array.add(number);
this.card.put(name,this.array);
}
}
public void display(){
for(Map.Entry<String,ArrayList<Integer>> card :
this.card.entrySet()){
System.out.println(card.getKey()+"\t"+card.getValue());
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
CollectCards card = new CollectCards();
do{
System.out.println("Enter the card details");
card.addCard(input.next(),input.nextInt());
System.out.println("Want to repeat again!!!");
}while(input.next().equals("Yes"));
card.display();
}
}
LEARNING OUTCOMES:
RESULT /OUTPUT/SUMMARY:
1.