I have trouble troubleshooting my project and sincerely hoping someone will be able to point out my coding mistakes. I was looking to add an Item in the application which I am working on my project.
I had attached my Item and ItemManager as well as the BidMenu. I did not attached my User and UserManager as they're both working perfectly, and so therefore I assumed either my ItemManager or my method to add item in BidMenu are wrongly-coded. Pls advice! Thank you.
public class Item{ private String itemId; private String itemDescription; private User seller; private double minBid; public Item (String itemId, String itemDescription, User seller, double minBid){ this.itemId = itemId; this.itemDescription = itemDescription; this.seller = seller; this.minBid = minBid; } public String getItemId(){ return itemId; } public String getItemDescription(){ return itemDescription; } public User getSeller(){ return seller; } public double getMinBid(){ return minBid; } public String toString(){ return "The item " +itemDescription +" has been added with ID " +itemId; } }import java.util.*; public class ItemManager{ private UserManager userMgr; private ArrayList<Item> itemList; public ItemManager(){ itemList = new ArrayList<Item>(); itemList.add(new Item("I1","Java 6 book", userMgr.retrieveUser("plaura"),10.00)); itemList.add(new Item("I2","Baby pram", userMgr.retrieveUser("johna"),20.00)); itemList.add(new Item("I3","Water heater", userMgr.retrieveUser("mklee"),10.00)); itemList.add(new Item("I4","Wedding gown", userMgr.retrieveUser("johna"),50.00)); } public ArrayList<Item> retrieveAll(){ return itemList; } public Item retrieveItem (String itemId) { for (int i=0; i<itemList.size(); i++){ Item item = itemList.get(i); if (item.getItemId().equals(itemId)){ return item; } } return null; } public void addItem (String itemId, String itemDescription, User seller, double minBid){ Item item = new Item (itemId, itemDescription, seller, minBid); itemList.add(item); } }import java.util.*; public class BidMenu{ private ItemManager itemMgr; private UserManager userMgr; //private BidManager bidMgr; private Scanner sc; public BidMenu(){ itemMgr = new ItemManager(); userMgr = new UserManager(); //bidMgr = new BidManager(); sc = new Scanner(System.in); } public void display(){ System.out.println("=== Merlion Xchange :: Main Menu ==="); System.out.println("1. Add an Item"); System.out.println("2. Bid for an Item"); System.out.println("3. Add User"); System.out.println("4. List all users"); System.out.println("5. Close bid"); System.out.println("6. View Report"); System.out.println("7. Quit application"); System.out.print("\nEnter your option > "); } public void processAddItem(){ System.out.println("\n=== Merlion Xchange :: Add Item ==="); System.out.print("\nEnter Item Description > "); String itemDescription = sc.nextLine(); ArrayList<User> userList = userMgr.retrieveAll(); String userName = ""; ArrayList<Item> itemList = itemMgr.retrieveAll(); do { System.out.print("\nEnter seller username > "); userName = sc.nextLine(); if (userMgr.retrieveUser(userName) == null){ System.out.println("The username doesn't exist"); } }while (userMgr.retrieveUser(userName) == null); User seller = userMgr.retrieveUser(userName); double minBid = 0; do { System.out.print("\nEnter minimum bid price > "); minBid = sc.nextDouble(); if (minBid <= 0) { System.out.println("Kindly enter a bid price greater than $0"); } } while (minBid <= 0); int listSize = itemList.size(); int newId = itemList.size()+1; String itemId = "I"+newId; itemMgr.addItem(itemId, itemDescription, seller, minBid); /*for (int i= listSize; i<itemList.size(); i++){ Item item = itemList.get(i); System.out.print(item.toString()); }*/ System.out.println(itemId); } public void processBidItem(){ System.out.println("\n=== Merlion Xchange :: Bid for Item ==="); } public void processAddUser(){ System.out.println("\n=== Merlion Xchange :: Add User ===\n"); String userName = ""; double balance = 100.00; ArrayList<User> userList = userMgr.retrieveAll(); do { System.out.print("Enter username for the new user > "); userName = sc.nextLine(); if (userMgr.retrieveUser(userName) != null){ System.out.println("The username already exists"); } }while (userMgr.retrieveUser(userName) != null); System.out.println("\nThe following user has been added:\n"); System.out.println("Username Balance"); System.out.println("===================================="); int listSize = userList.size(); userMgr.addUser(userName, balance); for (int i= listSize; i<userList.size(); i++){ User user = userList.get(i); System.out.print(user.toString()); } System.out.println(); } public void processListAllUsers(){ System.out.println("\n=== Merlion Xchange :: List all users ==="); System.out.println("\nUsername Balance"); System.out.println("===================================="); ArrayList<User> userList = userMgr.retrieveAll(); for (int i=0; i<userList.size(); i++){ User user = userList.get(i); System.out.print(user.toString()); } System.out.println(); } public void processCloseBid(){ System.out.println("\n=== Merlion Xchange :: Close Bid ==="); } public void processListHighestPoints(){ System.out.println("\n=== Merlion Xchange :: View Report ==="); System.out.println("\nUser(s) with the highest number of Xchange points:"); System.out.println("\nUsername Xchange Points"); System.out.println("===================================="); } public void readOption(){ int option; do { display(); option = sc.nextInt(); sc.nextLine(); switch(option){ case 1:processAddItem(); break; case 2:processBidItem(); break; case 3:processAddUser(); break; case 4:processListAllUsers(); break; case 5:processCloseBid(); break; case 6:processListHighestPoints(); break; case 7:System.out.println("\nThank you for using Merlion Xchange Bidding Items Application!\n"); break; default:System.out.println("Please enter a valid option (1 to 7)!"); } } while(option != 7); } }