Question

In: Computer Science

Within Store.java, the refundOrder method has been started for you. Do not modify it’s method signature....

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
   }
}

Solutions

Expert Solution

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=====


Related Solutions

public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
Can you orally modify a contract that is in writing and within the Statute of Frauds?
Can you orally modify a contract that is in writing and within the Statute of Frauds?
A method has been engineered and an operator has been time studied. The method has produced...
A method has been engineered and an operator has been time studied. The method has produced an average overall time of 2.39 minutes by actual watch timing. The overall pace rating applied to the job was 125%. This company, which uses an incentive system, adds extra time into the production rates to cover personal time, fatigue, and unavoidable delays. The total allowance is 15%. Determine the rated time or normal minutes, the total allowed time or standard minutes, the standard...
> Orlando has outgrown it’s Citrus Bowl stadium and has not been invited to host the...
> Orlando has outgrown it’s Citrus Bowl stadium and has not been invited to host the best of College Football Championship games because of it > If a public works project (a very long term commitment) was planned in Orlando to construct a new, permanent football stadium, and it’s initial cost was $900M > The new stadium yearly maintenance cost was $1M/year for years 1-10, growing to $2M/year thereafter > The new stadium required repainting every 10 years at a...
select a small business in your community that has been started by an entrepreneur. (Note: you...
select a small business in your community that has been started by an entrepreneur. (Note: you may even select a small family business). Provide a summary of the key features of this business. Are there any obstacles to growth? What is the entrepreneur doing to ensure profitability and address risk? Explain fully and comment on classmates' responses. PLEASE ADDRESS THIS IN LIGHT OF THE IMPACT OF CORONAVIRUS ON SMALL BUSINESS.
select a small business in your community that has been started by an entrepreneur. (Note: you...
select a small business in your community that has been started by an entrepreneur. (Note: you may even select a small family business). Provide a summary of the key features of this business. Are there any obstacles to growth? What is the entrepreneur doing to ensure profitability and address risk? Explain fully and comment on classmates' responses. PLEASE ADDRESS THIS IN LIGHT OF THE IMPACT OF CORONAVIRUS ON SMALL BUSINESS
select a small business in your community that has been started by an entrepreneur. (Note: you...
select a small business in your community that has been started by an entrepreneur. (Note: you may even select a small family business). Provide a summary of the key features of this business. Are there any obstacles to growth? What is the entrepreneur doing to ensure profitability and address risk? Explain fully and comment on classmates' responses. PLEASE ADDRESS THIS DISCUSSION IN LIGHT OF THE IMPACT OF CORONAVIRUS ON SMALL BUSINESS.
Imagine that the small business that you have been working for has started to catch traction...
Imagine that the small business that you have been working for has started to catch traction and you are considering expanding into a few new markets. However, these markets make meeting with key managers inconvenient. You have been charged with developing and finding a way for the company to conduct meetings in an efficient and effective manner while keeping costs to a minimum. Your task is to search the internet and find two free online communicating services to present as...
Can you think of another application of blind signature rather than eCash? Do you think that...
Can you think of another application of blind signature rather than eCash? Do you think that Bitcoin could become a popular electronic currency? Explain
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT