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...
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?
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
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.
> 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...
"It’s only when the tide goes out that you learn who has been swimming naked." ---Warren...
"It’s only when the tide goes out that you learn who has been swimming naked." ---Warren Buffett Please comment on this statement and give examples from previous accounting scandals and financial crises. Initial Post Length: minimum of 350 words. Using APA format, cite your sources.
Provide immediate feedback for each mistyped sentence. To do so, modify the Test class’s present_test method...
Provide immediate feedback for each mistyped sentence. To do so, modify the Test class’s present_test method so that it informs the player a mistake has been made, then display the challenge sentence followed by the player’s sentence so the player can determine where the error lies. #-------------------------------------------------------------------------- # # Script Name: TypingChallenge.rb # Version: 1.0 # Author: Jerry Lee Ford, Jr. # Date: March 2010 # # Description: This Ruby script demonstrates how to apply conditional logic # in order...
You have been hired by a used-car dealership to modify the price of cars that are...
You have been hired by a used-car dealership to modify the price of cars that are up for sale. You will get the information about a car, and then change its price tag depending on a number of factors. Write a program (a script named: 'used_cars.m' and a function named 'car_adjust.m').The script passes the file name 'cars.dat' to the function. The following information is stored inside the file: Ford, 2010 with 40,000 miles and no accident at marked price of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT