Answer to Question #58916 in Java | JSP | JSF for Nithya

Question #58916
4. Modify quantity of apples in fruit shop
Here a user is asked to enter the ID of the store to change. If not found, a proper message must be printed, otherwise your program asks for the user to enter the new quantity of apples for this store and your program must update that store’s quantity.

5. Modify price of apples in a fruit shop
Same as 4, except for the price.
1
Expert's answer
2016-04-06T12:25:04-0400
public static void main(String[] args) {

Map<Integer, Apple> fruitShop = new HashMap<>();
Scanner in = new Scanner(System.in);

// change the quantity of apples according to their id

System.out.println("Please enter the ID of the store to change");
int id = in.nextInt();
if (fruitShop.get(id) == null) {
System.out.println("There is no apples according to this id");
}
else {
System.out.println("Please enter the new quantity of apples for this store");
int quandity = in.nextInt();
fruitShop.get(id).setQuantity(quandity);
}

// change the price of apples according to their id
System.out.println("Please enter the ID of the store to change");
id = in.nextInt();
if (fruitShop.get(id) == null) {
System.out.println("There is no apples according to this id");
}
else {
System.out.println("Please enter the new price of apples for this store");
double price = in.nextDouble();
fruitShop.get(id).setPrice(price);
}

}




Assume that you have the class Apple like this:
public class Apple {
private int id;
private double price;
private double quantity;

public int getId() {
return id;
}

public double getPrice() {
return price;
}

public double getQuantity() {
return quantity;
}

public void setPrice(double price) {
this.price = price;
}

public void setQuantity(double quantity) {
this.quantity = quantity;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS