Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am stuck on what to do to get the ItemID to work.. import java.io.FileReader;

ID: 3530981 • Letter: I

Question

I am stuck on what to do to get the ItemID to work.. import java.io.FileReader; import java.io.PrintWriter; import java.util.Scanner; public class HomeAppliance { private int nAppliances; private int maxAppliances = 25; private Appliance[] appliances; Scanner console = new Scanner (System.in); public HomeAppliance() { appliances = new Appliance[maxAppliances]; for (int index=0; index < maxAppliances; ++ index) appliances[index] = new Appliance(); } // default constructor private void copyAppliance (Appliance to, Appliance from) // utility { to.applianceName = from.applianceName; to.itemID = from.itemID; to.price = from.price; to.quantity = from.quantity; } // end copyAppliance public void openStore(Scanner inFile) { Appliance oneAppliance; int numAppliances; Appliance(); numAppliances = inFile.nextInt(); inFile.nextLine(); //discard the newline character for (int index = 0; index < numAppliances; index++) { oneAppliance.applianceName = inFile.nextLine(); oneAppliance.price = inFile.nextDouble(); oneAppliance.quantity = inFile.nextInt(); oneAppliance.itemID = inFile.next(); inFile.nextLine(); // remove new line insert(oneAppliance); } } // end openStore public void process() { Appliance oneAppliance; Appliance(); int position; String itemID; char response; for (int index=0; index < nAppliances; index++) System.out.println(appliances[index].toString()); do { System.out.print(" Menu Options: " + "----------------------- " + "1 - Insert a new appliance. " + "2 - Output one appliance. " + "3 - Output out of stock. " + "4 - Sell appliance. " + "5 - Add Shipment " + "6 - Quit the program. Enter choice: "); response = console.next().charAt(0); console.nextLine(); switch(response) { case '1' : inputOneAppliance(oneAppliance); insert(oneAppliance); break; case '2' : itemID = inputItemID (); position = find(itemID); if (position == -1) System.out.println (" No appliance found for " + itemID); else { System.out.println (" Appliance for " + itemID + ": " ); outputOneAppliance(position+1); } break; case '3' : outputOutOfStock(); break; case '4' : sellAppliance(); break; case '5' : addShipment(); break; case '6' : break; default: System.out.println("Error: Incorrect input. (Please enter 1 - 7)"); } } while (response != '7'); } // end process public void insert (Appliance oneAppliance) { int index = nAppliances - 1; boolean found = false; if (nAppliances == 25) System.out.println ("Array is full - no insert"); else { while (index >= 0 && !found) { if (oneAppliance.itemID.compareTo(appliances[index].itemID) < 0) { copyAppliance(appliances[index+1],appliances[index]); // appliances[index+1] = appliances[index]; -- index; } else found = true; } copyAppliance(appliances[index+1], oneAppliance); // appliances[index+1] = oneAppliance; nAppliances++; } } // end insert public void outputOneAppliance (int position) { System.out.println(appliances[position-1].toString()); // end printOneAppliance } public int find(String searchItem) // binary search { int first = 0; int last = nAppliances - 1; int mid = 0; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (appliances[mid].itemID.compareTo(searchItem) == 0) found = true; else if (appliances[mid].itemID.compareTo(searchItem) > 0) last = mid - 1; else first = mid + 1; } if (!found) mid = -1; //it is an unsuccessful search return mid; }//end find public void inputOneAppliance(Appliance oneAppliance) { System.out.println(" Enter Appliance Info:"); System.out.print ("Enter name: "); oneAppliance.applianceName = console.nextLine(); System.out.print("Enter price: "); oneAppliance.price = console.nextDouble(); System.out.print("Enter quantity: "); oneAppliance.quantity = console.nextInt(); System.out.print("Enter item ID: "); oneAppliance.itemID = console.next(); console.nextLine(); // remove new line } // end inputOneAppliance public String inputClassID() { String itemID; itemID = new String(); System.out.print (" Enter item ID "); itemID = console.nextLine(); return itemID; } // end inputItemID public void outputOutOfStock() { int index; System.out.println (" Out of Stock Appliances:"); for (index = 0; index < nAppliances; ++ index) if (appliances[index].quantity == 0) { System.out.println(appliances[index].toString()); } } // end outputOutOfStock public void sellAppliance() { int position; String itemID = new String(); itemID = inputItemID(); position = find(itemID); if (position == -1) System.out.println (" No appliances found for " + itemID); else { if (appliances[position].quantity == 0) System.out.println("No appliances found for " + appliances[position].itemID); else { appliances[position].quantity--; System.out.printf("Please pay %.2f for appliance for %s%n", appliances[position].price, appliances[position].itemID); } } } // end sellAppliance public void returnAppliance() { String itemID = new String(); int qtyReceived, position; do { itemID = inputItemID (); position = find(itemID); if (position == -1) System.out.println (" No appliances available for " + itemID); } while (position == -1); appliances[position].quantity++; } // end returnAppliance public void addShipment() { String itemID = new String(); int qtyReceived, position; do { itemID = inputItemID (); position = find(itemID); if (position == -1) System.out.println (" No appliances available for " + itemID); } while (position == -1); System.out.print("Enter quantity received: "); qtyReceived = console.nextInt(); console.nextLine(); appliances[position].quantity += qtyReceived; } // end addShipment public void endOfDay(PrintWriter outFile) { outFile.println (nAppliances); for (int index = 0; index < nAppliances; index++) { outFile.println(appliances[index].applianceName); outFile.printf("%.2f %d %s%n", appliances[index].price, appliances[index].quantity, appliances[index].itemID); } } // end endOfDay } // end HomeAppliance

Explanation / Answer

In general, avoid using if/else/loops without brackets, even if they only contain a single line of code. Especially you have if's with brackets and their corresponding else's without. This just makes it confusing for me to read.

A database (DB) is like a kind of data structure. Naming an array variable with DB on the end is like having a variable "Set namesArrayList". It just says the variable refers to something which it doesn't. An array is not a database. Now potentially you may want to change the data structure you're using so you may just want to call it, 'names' to indicate it's a collection of things without specifying data structure, etc.

Try not to write to much continuous unfactored code. Code that goes.

if hard to read/keep track of.

It's probably better to take those huge slabs of text and put them into functions

Or even:

This makes your code more readable. Also the function names act like automatic comments for your subsections (ie. what's in the functions). Also since you redo alot of the same code, you may be able to just do this in a function, then call the function multiple times (with different parameters), ie. reuse it.

Also, what are you actually trying to do with the Contacts class. Why not have a class to deal with individual contacts, eg: a Contact class, with say, name, address, phoneNum, fields. And a class to deal with a collection of contacts, eg: AddressBook class. Which contains a field which is something along the lines of: ArrayList<Contact> contacts , ie. an collection of contacts, and some methods to operate of this, eg: add, search, delete etc. You don't have to store a string representation of each object in your collection, you can store the actual objects themselves.

Doing this would make the code to run the command line very simple.

Now regarding the errors:

First error:

You've mistyped the name of the function. It should be readLine() with a capital L.

Second error:

What are you actually trying to do here. There's no Contacts method on your contacts class. Seems pretty self-explanatory. Going w.Contacts() where w is a Contacts object, tries to invoke a method called Contacts of the object (which obviously doesn't exist). Now if you define a toString() method for Contacts which returns a String of the same form as what's in your list of names (ie. namesDB). Then you can go: if ( line.equals(w.toString() ) ...

Third error:

You haven't actually declared a Contacts variable called 'w' either in this else statement or anywhere "above" this else statement. ie. you haven't declared a 'w' variable within the "scope" of this line.

So you could go: Contacts w = new Contacts(fullName, phoneNo, address); at the top of the else statement or in the if statement outside it.

BUT, this is dodgy because you're making a Contacts object with a fullName you've got from a commandline arg (this part is fine) and some uninitialised variables (address/phoneNo) decalred at the top of your main function!!!

Actually, since this is a static method, you should really be going: Contacts.stripQuo()

You should call static methods using the name of the class, not the name of an object of that class (even though that might work). (see above for how).

Fourth error:

same as first error

Fifth error:

Same thing as both the second error and the third error.

Sixth error:

Same as third error. Except you actually do need to call this on a object (ie. because it's not a static method)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote