Skip to content

Java Library  #2229

@ashishsme14

Description

@ashishsme14

We had followed code from

https://www.coursehero.com/u/file/77382119/hashmaptxt

Code is

package com.fresco;
import java.util.*;
public class Library
{
 
 String bookName;
 String author;
 Library()
 {
 }
 @Override
 public int hashCode() {
 int hash = 3;
 hash = 83 * hash + Objects.hashCode(this.bookName);
 hash = 83 * hash + Objects.hashCode(this.author);
 return hash;
 }
 @Override
 public boolean equals(Object obj) {
 if (this == obj) {
 return true;
 }
 if (obj == null) {
 return false;
 }
 if (getClass() != obj.getClass()) {
 return false;
 }
 final Library other = (Library) obj;
 if (!Objects.equals(this.bookName, other.bookName)) {
 return false;
 }
 if (!Objects.equals(this.author, other.author)) {
 return false;
 }
 return true;
 }
 
 
 Library(String bookName,String author)
 {
 this.bookName=bookName;
 this.author=author;
 }
 public static HashMap<Integer,Library> createLibraryMap(String 
booksInLibrary){
 String output = booksInLibrary.replace("|",":");
 HashMap<Integer, Library> hmap= new HashMap<Integer, Library>();
 String parts1[] = output.split(":");
 for(String part : parts1){
 
 String lib[] = part.split(",");
 
 int bid=Integer.parseInt(lib[0]); 
 String bname = lib[1].trim();
 String bauthor = lib[2].trim();
 
 Library l=new Library(bname,bauthor); 
 hmap.put(bid,l);
 }
return hmap;
 }
 public static HashMap<Integer,Integer> createUserMap(String borrowedUsers){
 String output = borrowedUsers.replace("|",":");
 HashMap<Integer,Integer> hmap1= new HashMap<Integer,Integer>();
 String parts2[] = output.split(":");
 for(String part : parts2){
 ArrayList<String> list = new ArrayList<String>();
 
 String user[] = part.split(",");
 
 int bid=Integer.parseInt(user[0]); 
 int uid=Integer.parseInt(user[1]); 
 hmap1.put(bid,uid);
 }
 return hmap1;
 }
 
 public static String getQuery(String booksInLibrary,String 
borrowedUsers,String query){
 HashMap<Integer,Library> f1 = new HashMap<Integer, Library>();
 f1 = createLibraryMap(booksInLibrary);
 for (Map.Entry<Integer, Library> entry : f1.entrySet()) {
 int key = entry.getKey();
 Library values = entry.getValue();
 System.out.println(key+" : "+ values.bookName+" "+values.author);
 }
 System.out.println();
 
 
 HashMap<Integer,Integer> f2= new HashMap<Integer,Integer>();
 f2 = createUserMap(borrowedUsers);
 for (Map.Entry<Integer, Integer> entry : f2.entrySet()) {
 int key = entry.getKey();
 int values = entry.getValue();
 System.out.println(key+" : "+ values);
 }
 System.out.println();
 
 String parts3[] = query.split(",");
 String q1 = parts3[0];
 String q2 = parts3[1];
 System.out.println(q1+" -- "+q2);
 if(q1.equals("1")){
 int vbid = Integer.parseInt(q2);
 int count =0;
 for (Map.Entry<Integer, Integer> entry : f2.entrySet())
 {
 int bid = entry.getKey();
 if(vbid == bid)
 {
 count++;
 System.out.println(bid);
 System.out.println("No Stock\nIt is owned by "+f2.get(bid)
+"\n");
 }
 }
 if(count == 0){
 for (Map.Entry<Integer, Library> entry : f1.entrySet())
 {
 int bid = entry.getKey();
 Library v = entry.getValue();
 if(vbid == bid){
 System.out.println("It is available\nAuthor is "+v.author);
 }
 }
 }
 }
 
 return null;
 }
 public static void main(String[] args) {
 
 String output = getQuery("125,C programming,Brian W. Kernighan|564,C++ programming,Bjarne Stroustrup|546,Java programming,James Gosling|897,Data Structure,Adam Drozdek|785,System Programming,Johnson M|265,Data Structure,Adam Drozdek|469,Data Structure,Adam Drozdek|968,Advanced Java,James Gosling","125,101|564,102|546,103|897,104|785,101","1,968");
 }
 
}

Getting error as -

Results :

Failed tests:   testGetQuery3(com.fresco.HashmapHandsonTest): expected:<1 out(..)

  testGetQuery4(com.fresco.HashmapHandsonTest): expected:<1001 Adventures of Sherlock Holmes(..)

  testGetQuery2(com.fresco.HashmapHandsonTest): expected:<It is available(..)

  testGetQuery5(com.fresco.HashmapHandsonTest): expected:<All’s Well that Ends well(..)

  testGetQuery6(com.fresco.HashmapHandsonTest): expected:<1008 JavaScript Programming(..)

  testGetQuery1(com.fresco.HashmapHandsonTest): expected:<No Stock(..)

Tests run: 6, Failures: 6, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------

[INFO] BUILD FAILURE

[INFO] ------------------------------------------------------------------------

Un editable code on which result based are

package com.fresco;


import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.*;

public class HashmapHandsonTest{
    
    Library lib;
    public HashmapHandsonTest() {
    }
    
    @Before
    public void setUp() {
        lib=new Library();
    }
    
    
    @Test
    public void testGetQuery1() {
        if(testCheck() && testCheck1()){
        String actual=lib.getQuery("125,C programming,Brian W. Kernighan|564,C++ programming,Bjarne Stroustrup|546,Java programming,James Gosling|897,Data Structure,Adam Drozdek|785,System Programming,Johnson M|265,Data Structure,Adam Drozdek|469,Data Structure,Adam Drozdek|968,Advanced Java,James Gosling","125,101|564,102|546,103|897,104|785,101","1,546");
        String exp="No Stock\nIt is owned by 103\n";
        assertEquals(exp,actual);
        }else{
            assertEquals(0,1);
        }
        
    }
    @Test
    public void testGetQuery2() {
        if(testCheck() && testCheck1()){
        String actual=lib.getQuery("1001,Adventures of Sherlock Holmes,Sir Arthur Conan Doyle|1002,Adventures of Tom Sawyer,The Mark Twain|1003,Alice in the Wonderland,Lewis Carroll|1004,All’s Well that Ends well,William Shakespeare|1005,As you like it,William Shakespeare|1006,Anand Math,Bankim Chandra Chatterjee","1006,101|1003,102|1001,101","1,1005");
        String exp="It is available\nAuthor is William Shakespeare\n";
        assertEquals(exp,actual);
        }else{
            assertEquals(0,1);
        }
        
    }
    @Test
    public void testGetQuery3() {
        if(testCheck() && testCheck1()){
        String actual=lib.getQuery("125,C programming,Brian W. Kernighan|564,C++ programming,Bjarne Stroustrup|546,Java programming,James Gosling|897,Data Structure,Adam Drozdek|785,System Programming,Johnson M|265,Data Structure,Adam Drozdek|469,Data Structure,Adam Drozdek|968,Advanced Java,James Gosling","125,101|564,102|546,103|897,104|785,101","3,Data Structure");
        String exp="1 out\n2 in\n";
        assertEquals(exp,actual);
        }else{
            assertEquals(0,1);
        }
    }
    
    @Test
    public void testGetQuery4() {
        if(testCheck() && testCheck1()){
        String actual=lib.getQuery("1001,Adventures of Sherlock Holmes,Sir Arthur Conan Doyle|1002,Adventures of Tom Sawyer,The Mark Twain|1003,Alice in the Wonderland,Lewis Carroll|1004,All’s Well that Ends well,William Shakespeare|1005,As you like it,William Shakespeare|1006,Anand Math,Bankim Chandra Chatterjee","1006,101|1003,102|1001,101","2,101");
        String exp="1001 Adventures of Sherlock Holmes\n1006 Anand Math\n";
        assertEquals(exp,actual);
        assertEquals(exp,actual);
        }else{
            assertEquals(0,1);
        }
    }
    
    @Test
    public void testGetQuery5() {
        if(testCheck() && testCheck1()){
        String actual=lib.getQuery("1001,Adventures of Sherlock Holmes,Sir Arthur Conan Doyle|1002,Adventures of Tom Sawyer,The Mark Twain|1003,Alice in the Wonderland,Lewis Carroll|1004,All’s Well that Ends well,William Shakespeare|1005,As you like it,William Shakespeare|1006,Anand Math,Bankim Chandra Chatterjee","1006,101|1003,102|1001,101","4,William Shakespeare");
        String exp="All’s Well that Ends well\nAs you like it\n";
        assertEquals(exp,actual);
        }else{
            assertEquals(0,1);
        }
    }
    
    @Test
    public void testGetQuery6() {
        if(testCheck() && testCheck1()){
        String actual=lib.getQuery("1001,Python Tricks,Dan Bader|1002,The Complete Software Developer's Career Guide,John Sonmez|1003,Mastering Deep Learning Fundamentals with Python,Richard Wilson|1004,Cracking the Coding Interview,Gayle Laakmann McDowell|1005,Python Programming,Joseph Mining|1006,Effective Java,Joshua Bloch|1007,Java,Nathan Clark|1008,JavaScript Programming,Brian Jenkins|1009,Functional-Light JavaScript,Kyle Simpson","1006,101|1009,102|1003,101","5,java");
        String exp="1008 JavaScript Programming\n1009 Functional-Light JavaScript\n1006 Effective Java\n1007 Java\n";
        assertEquals(exp,actual);
        }else{
            assertEquals(0,1);
        }
    }
    
    
    
    public boolean testCheck() {
        
        Library bs1=new Library("Python Tricks", "Dan Bader");
        Library bs2=new Library("The Complete Software Developer's Career Guide", "John Sonmez");
        Library bs3=new Library("Mastering Deep Learning Fundamentals with Python","Richard Wilson");
        HashMap<Integer,Library> exp=new HashMap<>();
        exp.put(1001,bs1);exp.put(1002,bs2);exp.put(1003,bs3);
        HashMap<Integer,Library> actual=lib.createLibraryMap("1001,Python Tricks,Dan Bader|1002,The Complete Software Developer's Career Guide,John Sonmez|1003,Mastering Deep Learning Fundamentals with Python,Richard Wilson");

        return exp.equals(actual);
    }
    public boolean testCheck1() {
        
        HashMap<Integer,Integer> exp=new HashMap<>();
        exp.put(125,101);exp.put(564,102);exp.put(546,103);exp.put(897,104);exp.put(785,101);
        HashMap<Integer,Integer> actual=lib.createUserMap("125,101|564,102|546,103|897,104|785,101");
        
        return exp.equals(actual);
    }
}

Please suggest us

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions