My professor returned my assignment with these comments, I just can't figure out how to implement them in to my code:
No check of id length. -14
There is no Inventory class. -10
All coding is in the main () method that should be a driver. -10
HashMap not in the requirements. -5
Instantiation of Inventory not present. -5
Please help!
THIS IS MY CODE:
import java.util.*;
public class WK2GarciaM {
public static void main(String[] args) { Scanner input = new Scanner(System.in); HashMap ar1 = new HashMap(); int choice; do { System.out.println("MENU 1: Add book 2: Remove book 3: Find book 4: Display all books 9: Exit program "); System.out.print("Enter your selection: "); choice = input.nextInt(); switch(choice) { case 1: System.out.print("Book id = "); int id = input.nextInt(); input.nextLine(); System.out.print("What is Book title? "); String title = input.nextLine(); System.out.print("Book price (double value)? "); double price = input.nextDouble(); Book b = new Book(id,title,price); ar1.put(id,b); break; case 2: if(ar1.size() == 0) { System.out.println("The inventory has no Books to remove"); } else { Iterator> it = ar1.entrySet().iterator(); int found = 0; System.out.print("Enter the book id you want to remove: "); int Bid = input.nextInt(); while(it.hasNext()) { Map.Entry b1 = it.next(); int key = b1.getKey(); if(key == Bid) { found = 1; ar1.remove(key); System.out.println("Book is removed from inventory"); break; } } if(found == 0) { System.out.println("There is no book with given id"); } } break; case 3: if(ar1.size() == 0) { System.out.println("The inventory has no Books to search"); } else { Iterator> it = ar1.entrySet().iterator(); int found = 0; System.out.print("Enter the book id you want to search: "); int Bid = input.nextInt(); while(it.hasNext()) { Map.Entry B2 = it.next(); int key = B2.getKey(); if(key == Bid) { found = 1; Book b2 = B2.getValue(); System.out.println("Book id: "+b2.id+" Book title: "+b2.title+" Book price: "+b2.price); break; } } if(found == 0) { System.out.println("There is no book with given id"); } } break; case 4: if(ar1.size() == 0) { System.out.println("The inventory has no Books to Display"); } else { Iterator> it = ar1.entrySet().iterator(); System.out.println("Book(s) in inventory list are: "); while(it.hasNext()) { Map.Entry B3 = it.next(); Book b3 = B3.getValue(); System.out.println("Book id: "+b3.id+" Book title: "+b3.title+" Book price: "+b3.price); } } break; case 9: System.out.println("Thank you for using the program.GoodBye!"); break; } }while(choice!=9);
}
}
class Book { int id; String title; double price; Book(int id, String title, double price) { this.id = id; this.title = title; this.price = price; } }