Please finish the last 4 bullet points of this inside the gemstoneApp in java. I
ID: 3880119 • Letter: P
Question
Please finish the last 4 bullet points of this inside the gemstoneApp in java. I cant figure them out.
My code is below.
public class Gemstone {
private String color;
private int weight;
private String shape;
private final int id;
private static int count;
/**
* @param color
* @param weight
* @param shape
*/
public Gemstone(String color, int weight, String shape) {
super();
this.color = color;
this.weight = weight;
this.shape = shape;
id = 12345678 + count;
count++;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @return the weight
*/
public int getWeight() {
return weight;
}
/**
* @return the shape
*/
public String getShape() {
return shape;
}
/**
* @return the id
*/
public int getId() {
return id;
}
@Override
public String toString(){
String result = "The gemstone weighs " + weight + " pounds and has a " + shape + " shape, id number: " + id;
return result;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class GemstoneApp {
static Scanner input = new Scanner(System.in);
static int choice = 0;
public static void displayMenu() {
System.out.println("1. Display all Gemstones");
System.out.println("2. Add an Gemstone");
System.out.println("3. Find an Gemstone");
System.out.println("4. Delete an Gemstone");
System.out.println("5. Number of items in list");
System.out.println("6. Exit");
System.out.print("Enter your selection: ");
choice = input.nextInt();
//System.out.println(choice);
}
//I tried to start ths method but doesnt work
/*private static int displayAllGemstones() {
for (Gemstone gem: gemstones) {
System.out.print(gem);
}
}*/
public static void main(String[] args) {
ArrayList<Gemstone> gemstones = new ArrayList<>();
gemstones.add(new Gemstone("Red", 9, "Rectangle"));
gemstones.add(new Gemstone("Blue", 7, "Square"));
gemstones.add(new Gemstone("Red", 11, "Diamond"));
gemstones.add(new Gemstone("Red", 3, "Circle"));
displayMenu();
switch(choice) {
case 1:
//displayAllGemstones();
break;
case 2:
case 3:
case 4:
case 5:
}
}
}
constructor setters-in-eclips It needs to override the method toString. The method toString has no parameters and returns a string that represents an instance of the class. This string needs to include the values of all fields except for count (e.g. 2015 Honda Accord id: 12345679 Labels should be added when the value itself is not self-explanatory. (e.g. id) Sample Output: 1. Display all items 2. Add an item 3. Pind an item 4. Delete an item S. Number of items in list 6. Exit Enter your salection: 1 Instructions continued: At this point you have one class that represents one item (e.g. Book, Bike Gemstone, etc.). This class does not contain the list of items nor should it include any print statements. You still need at least one more class (optionally two) that include(s) the list of items, the menu with options, and the user communication (input / 2011 Toyota Corolla id:12345678 2015 Honda Ascord id:12345679 2a08 Audi 4 id : 12345680 2Q17 Teslaodel 3 ia : 12345681 Create one or two additional classes to do the following Declare an ArrayList of items (each element in the list should be an instance of the new Java type you just wrote) Initialize the list with four different elements (hard coded) 1. Display all items 2. Add an item 3. Find an item 4. Delete an item 5. Nuber of items in 1ist 6. Exit Ente your selection: Display a menu with the following choices until the user chooses to exit 1. Display all items 2. Add an item 3. Find an iten 4. Delete an item 5. Number of items in list 6. Exit Ent r your selection: Important: replace the word item and items with the actual type you store in the list. (e.g. Display all cars) Provide the functionality chosen by the user Use private methods to print the menu and to display, find, add, or delete items. Using private methods in such a way structures the code and to makes the code more clear and easier to read When the user wants to find or delete an item s/he should be prompte for the id number If the user provides and id number that doesn't exist, an appropriate message should be displayed (e.g. The id . could not be found.) If the id number could be found the item information should be displayed as part of the response (e.g. 2015 Honda Accord id:12345679 if the item was found or 2015 Honda Accord has been deleted if it was deleted) Number of ars: 4 .Display all items 2. Add an item 3. Find an item 4. Delete an item 5 Number of items in list 6. Exit Enter your aelection: 3 d 12345680 2008 Audi id:12345680 Display all items 2. Add an item 3· ird an item 4. Delete an item 5. Number of items in list 6. Exit Enter your selection: 4 Id 12345680 2008 Audi A4 has beer deleted If the user enters an invalid choice from the menu (e.g. 9) an 1. Display all items 2. Acld an item 3. Find an item 4, Deleten item S. Nmber of items in liat 6. Exit Enter your selection: 2 appropriate message should be printed (e.g. Enter a selection 1 - 6) If the user enters 6 a message should be printed before terminating th program (e.g. Good bye) Below you can find a sample output. This sample is about cars. Your output needs show elements of the item type you chose above Year 2013 M ke: Ford Model: KustangExplanation / Answer
Explanation::
Code in JAVA::
GemstoneApp.java code::
import java.util.ArrayList;
import java.util.Scanner;
public class GemstoneApp {
static Scanner input = new Scanner(System.in);
static int choice = 0;
public static void displayMenu() {
System.out.println("1. Display all Gemstones");
System.out.println("2. Add an Gemstone");
System.out.println("3. Find an Gemstone");
System.out.println("4. Delete an Gemstone");
System.out.println("5. Number of items in list");
System.out.println("6. Exit");
System.out.print("Enter your selection: ");
choice = input.nextInt();
//System.out.println(choice);
}
//I tried to start ths method but doesnt work
private static void displayAllGemstones(ArrayList<Gemstone> gemstones) {
for (Gemstone gem: gemstones) {
System.out.print(gem);
}
}
private static void addGemstone(ArrayList<Gemstone> gemstones){
input.nextLine();
System.out.print("Enter color :: ");
String color=input.nextLine();
System.out.print("Enter weight :: ");
int weight=input.nextInt();
input.nextLine();
System.out.print("Enter shape :: ");
String shape=input.nextLine();
gemstones.add(new Gemstone(color,weight,shape));
}
private static void findGemstone(ArrayList<Gemstone> gemstones){
input.nextLine();
System.out.print("Enter ID of Gemstone to be searched::");
int id=input.nextInt();
boolean find=false;
for(Gemstone gem:gemstones){
if(gem.getId()==id){
find=true;
System.out.println("Gemstone with ID "+id+" found! It has following properties::");
System.out.println("Color :"+gem.getColor());
System.out.println("Weight :"+gem.getWeight());
System.out.println("Shape : "+gem.getShape());
break;
}
}
if(find==false){
System.out.println("Gemstone with id "+id+" is not present!");
}
}
private static void deleteGemstone(ArrayList<Gemstone> gemstones){
input.nextLine();
System.out.print("Enter ID of Gemstone to be deleted::");
int id=input.nextInt();
boolean deleted=false;
for(Gemstone gem:gemstones){
if(gem.getId()==id){
deleted=true;
gemstones.remove(gem);
System.out.println("Gemstone with id "+id+" is deleted!");
break;
}
}
if(deleted==false){
System.out.println("Gemstone with id "+id+" is not present to be deleted!");
}
}
public static void main(String[] args) {
ArrayList<Gemstone> gemstones = new ArrayList<>();
gemstones.add(new Gemstone("Red", 9, "Rectangle"));
gemstones.add(new Gemstone("Blue", 7, "Square"));
gemstones.add(new Gemstone("Red", 11, "Diamond"));
gemstones.add(new Gemstone("Red", 3, "Circle"));
while(true){
displayMenu();
if(choice==6){
System.out.println("Good Bye!");
break;
}else if(choice>6 || choice<1){
System.out.println("Enter a selection 1-6");
}else{
switch(choice) {
case 1:
displayAllGemstones(gemstones);
break;
case 2:
addGemstone(gemstones);
break;
case 3:
findGemstone(gemstones);
break;
case 4:
deleteGemstone(gemstones);
break;
case 5:
System.out.println("There are "+gemstones.size()+" elements in list!");
break;
}
}
}/*While ends here*/
}/*Main ends here*/
}/*Class ends here*/
Gemstone.java code::
public class Gemstone {
private String color;
private int weight;
private String shape;
private final int id;
private static int count;
/**
* @param color
* @param weight
* @param shape
*/
public Gemstone(String color, int weight, String shape) {
super();
this.color = color;
this.weight = weight;
this.shape = shape;
id = 12345678 + count;
count++;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @return the weight
*/
public int getWeight() {
return weight;
}
/**
* @return the shape
*/
public String getShape() {
return shape;
}
/**
* @return the id
*/
public int getId() {
return id;
}
@Override
public String toString(){
String result = "The gemstone weighs " + weight + " pounds and has a " + shape + " shape, id number: " + id+" ";
return result;
}
}
Output::
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 5
There are 4 elements in list!
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 1
The gemstone weighs 9 pounds and has a Rectangle shape, id number: 12345678
The gemstone weighs 7 pounds and has a Square shape, id number: 12345679
The gemstone weighs 11 pounds and has a Diamond shape, id number: 12345680
The gemstone weighs 3 pounds and has a Circle shape, id number: 12345681
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 2
Enter color :: Pink
Enter weight :: 20
Enter shape :: Circle
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 1
The gemstone weighs 9 pounds and has a Rectangle shape, id number: 12345678
The gemstone weighs 7 pounds and has a Square shape, id number: 12345679
The gemstone weighs 11 pounds and has a Diamond shape, id number: 12345680
The gemstone weighs 3 pounds and has a Circle shape, id number: 12345681
The gemstone weighs 20 pounds and has a Circle shape, id number: 12345682
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 3
Enter ID of Gemstone to be searched::56712
Gemstone with id 56712 is not present!
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 3
Enter ID of Gemstone to be searched::12345682
Gemstone with ID 12345682 found!
It has following properties::
Color :Pink
Weight :20
Shape : Circle
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 4
Enter ID of Gemstone to be deleted::262626
Gemstone with id 262626 is not present to be deleted!
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 4
Enter ID of Gemstone to be deleted::12345678
Gemstone with id 12345678 is deleted!
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 1
The gemstone weighs 7 pounds and has a Square shape, id number: 12345679
The gemstone weighs 11 pounds and has a Diamond shape, id number: 12345680
The gemstone weighs 3 pounds and has a Circle shape, id number: 12345681
The gemstone weighs 20 pounds and has a Circle shape, id number: 12345682
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 5
There are 4 elements in list!
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 4
Enter ID of Gemstone to be deleted::12345682
Gemstone with id 12345682 is deleted!
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 5
There are 3 elements in list!
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 1
The gemstone weighs 7 pounds and has a Square shape, id number: 12345679
The gemstone weighs 11 pounds and has a Diamond shape, id number: 12345680
The gemstone weighs 3 pounds and has a Circle shape, id number: 12345681
1. Display all Gemstones
2. Add an Gemstone
3. Find an Gemstone
4. Delete an Gemstone
5. Number of items in list
6. Exit
Enter your selection: 6
Good Bye!
Please provide feedback.
Thank You!!!