Groovy MultivaluedHash Map
Clash Royale CLAN TAG#URR8PPP
Groovy MultivaluedHash Map
Can someone help me to solve the problem
Problem Description
Create a class Asset with the following member variables.
def name
def assetNumber
def model
def lastAssignedDate
def price
Input Format:
The first input corresponds to string asset name.
The second input corresponds to an integer asset number.
The third input corresponds to string asset model.
The fourth input corresponds to the date last assigned the date.
The fifth input corresponds to double asset price.
Next input is a string denotes when there is one more asset detail or not. (Yes/No)
name
assetNumber
model
lastAssignedDate
price
Output Format:
If the assetNumber
is found print Asset with _assetNumber_ found.
or Asset with _assetNumber_ not found.
Refer sample input and output. [All bold text corresponds to input and rest are the output.]
assetNumber
Asset with _assetNumber_ found.
Asset with _assetNumber_ not found.
Asset name:name1
Asset number:1
Asset model:mod1
Assigned date:01/01/2015
Price: 100.24
Do you want to continue? (Yes/No): Yes
Asset name:name2
Asset number:2
Asset model:mod2
Assigned date:01/01/2016
Price: 200.24
Do you want to continue? (Yes/No): No
1
2
Enter the number to search:
The asset with 2 found.
name:name1
number:1
model:mod1
date:01/01/2015
100.24
name:name2
number:2
model:mod2
date:01/01/2016
200.24
My Code
def class Asset
def name;
def assetNumber
def model
def lastAssignedDate
def price
def constructAsset(Map classMap)
this.name=classMap['name'];
this.assetNumber=classMap['assetNumber'];
this.model=classMap['model'];
this.lastAssignedDate=classMap['lastAssignedDate'];
this.price=classMap['price']
return classMap;
def findNumber(Map classMap,int num)
//println classMap
for ( e in classMap )
if ("$e.key" == "assetNumber")
println ("$e.value")
def myKey = classMap.find it.value == num ?.key;
if(myKey)
println("Asset with " + num + " found.")
else
println("Asset with " + num + " not found.")
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
def name;
def assetNumber;
def model
def lastAssignedDate
def price
def cont="Yes"
Asset a=new Asset();
def list=[:]
while(cont=="Yes")
println("Asset name:");
name=br.readLine();
println("Asset number:");
assetNumber=br.readLine().toInteger();
println("Asset model:")
model=br.readLine()
println("Assigned date:")
lastAssignedDate=br.readLine();
lastAssignedDate=Date.parse("dd/mm/yyyy", lastAssignedDate).format("dd-mm-yyyy")
println("Price:")
price=br.readLine().toDouble()
list << a.constructAsset(name:"$name",assetNumber:"$assetNumber",model:"$model",lastAssignedDate:"$lastAssignedDate",price:"$price");
println("Do you want to continue?(Yes/No)");
cont=br.readLine();
println("Enter the number to search:")
def num=br.readLine().toInteger();
a.findNumber(list,num)
Expected Output
Asset name:name1
Asset number:1
Asset model:mod1
Assigned date:01/01/2015
Price:100.24
Do you want to continue? (Yes/No): Yes
Asset name:name2
Asset number:2
Asset model:mod2
Assigned date:01/01/2016
Price:200.24
Do you want to continue? (Yes/No): No
1
2
Enter the number to search:
The asset with 2 found.
Actual Output
Asset name:name1
Asset number:1
Asset model:mod1
Assigned date:01/01/2015
Price:100.24
Do you want to continue? (Yes/No): Yes
Asset name:name2
Asset number:2
Asset model:mod2
Assigned date:01/01/2016
Price:200.24
Do you want to continue? (Yes/No): No
1
2
Enter the number to search:
The asset with 2 found.
Issue
When adding key values, map always holds the recently entered value.
So when I am printing asset number value, it's printing the recent value. But not printing the first value entered. In this example expecting Asset Numbers 1 and 2. But printing only 2. (Next to Do you want to continue? (Yes/No))
Thank you. It works after adding a=new Asset() into the loop.
– Ezhil
Aug 14 at 14:29
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You only create a single Asset object, as the Asset a=new Asset(); is outside the while loop. Your list variable is a Map, so adding using the leftshift operator << just updates the map based on existing object. You should probably move a=new Asset() into the loop, and when adding the object to the list (assuming assetNumber is unique): list[ assetNumber] = a.constructAsset(name:"$name",assetNumber:"$assetNumber",model:"$model",lastAssignedDate:"$lastAssignedDate",price:"$price")
– Jacob Aae Mikkelsen
Aug 12 at 18:48