In: Computer Science
Within Store.java, the refundOrder method has been started for you. Do not modify it’s method signature. Ensure you understand how it works.
Complete the refundOrder method (method stub provided for you) so that it:
- Searches through the Store’s ArrayList of Order objects (hint: this method is in the same class as the ArrayList attribute) checking if each Order object’s orderNum matches the orderNum passed in as the method’s argument.
- If a matching orderNum is found, that Order object’s price should be updated to 0 (hint: price is private!) - If the orderNumber is not found, do nothing.
public class Order {
private int orderNum;
private int price;
public Order(int orderNum, int price)
{
this.orderNum = orderNum;
this.price = price;
}
public int getOrderNum()
{
return orderNum;
}
public void setOrderNum(int orderNum)
{
this.orderNum = orderNum;
}
public int getPrice()
{
return price;
}
public void setPrice(int price)
{
this.price = price;
}
public void print()
{
System.out.print("Order Number: " +
orderNum);
System.out.println(" Price: " +
price);
}
}
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class Store {
private ArrayList<Order> orders = new
ArrayList<Order>();
public ArrayList<Order> getOrders()
{
return orders;
}
/*
* Generates five Order objects, each with a hard-coded
orderNum and price
* and adds them to the Store's ArrayList
*/
public void generateFiveOrders()
{
int numOrders = 5;
//loop for each of the 5
orders
int orderNum = 100;
int price = 50;
for(int i =0; i< numOrders;
i++)
{
Order o = new
Order(orderNum + i,price); //each orderNum +1
orders.add(o);
price =
price+10; //each order's price increments by 10
}
}
/*
* Complete the refundOrder method below so that
it:
*
* - Accepts an int orderNum as its parameter //already
implemented
* - Searches through the ArrayList of Orders in the
Store class
* checking if each Order object's orderNum
* matches the orderNum passed into the method.
* - If an Order object with the specified order number
is found,
* - Set that Order object's price to 0 (note: price is
a private int!)
*/
public void refundOrder(int orderNum)
{
//YOUR CODE HERE
}
}
In case of any query do comment. Please rate answer as well. Thanks
Note: I have provided you the coed of refund order method only. You just replace this in your class. Sample main.java is also attached which has nothing to do with your question.
Code:
=========== Updated Method refundOrder========
public void refundOrder(int
orderNum)
{
// iterate over all the orders
for (Order order : orders){
//if order number match with the given order number
if(order.getOrderNum() == orderNum){
order.setPrice(0); //set the price to 0
break;
}
}
}
=========Sample driver program (not reequired in your question)========
public class Main
{
public static void main(String[] args) {
Store myStore = new Store();
//create an object of Store
myStore.generateFiveOrders();
//generateFiveOrders in the store
//print the order to display the
number and price
for (Order order :
myStore.getOrders()){
order.print();
}
System.out.println();
System.out.println("Order Details
after refund order called");
System.out.println();
myStore.refundOrder(102); //price
of order 102 should be set to 0
myStore.refundOrder(110); //it is
not present
//print orders again
for (Order order :
myStore.getOrders()){
order.print();
}
}
}
========screen shot of main program and output for display=====