In: Computer Science
In Java
The Order class should have: Seven instance variables: the
order number (an int), the Customer who made the order, the
Restaurant that receives the order, the FoodApp through which the
order was placed, a list of food items ordered (stored as an array
of FoodItem), the total number of items ordered (an int), and the
total price of the order (a double). A class constant to set the
maximum number of items that can be ordered (an int). Set it to 10.
A constructor that accepts three parameters, the Customer, the
Restaurant and the FoodApp, and initializes the corresponding
instances variables. The array of FoodItems will be a partially
filled array. It should be initialized according to the maximum
number of items that can be ordered, and be empty at the beginning.
The total number of items ordered should be initialized
accordingly. The total price shall be initialized to 0. The order
number shall have 6 digits, and start with a 9. Every order should
have a distinct order number, starting from 900001, then 900002,
then 900003 and so on. You will need to add either an instance
variable or a class variable to accomplish this (choose wisely).
An addToOrder(FoodItem) method that adds the FoodItem received as a
parameter to the FoodItem array, only if it is available (i.e. not
sold out) and if there is space left in the array. If the FoodItem
was added, the method returns true (it returns false otherwise).
You can assume that the FoodItem belongs to the restaurant’s menu
(no need to check if it’s on the menu). Don’t forget to update
here: the amount in stock for the FoodItem (decrement by 1), the
total price of the order, and the total number of items ordered.
A toString method that returns a String containing the FoodApp
name, the order #, the customer name, the Restaurant name, the list
of items ordered and the total price (formatting it exactly as
shown in the example below). You will need to add some accessors in
the previous classes (aka get methods) to get only the name of the
Customer, FoodApp and Restaurant, instead of the full String
representation of those.
=============================================
public class FoodItem {
/*Four instance variables: a name (a String), a cost (a double), a selling price (a double), and the number of
items available in stock for selling (an int)*/
String name;
double cost;
double price;
int numberOfItem;
/* A constructor that takes four parameters, in the above order, which are used to initialize the four instance
variables.*/
public FoodItem(String name, double cost, double price, int numberOfItem){
this.name = name;
this.cost = cost;
this.price = price;
this.numberOfItem = numberOfItem;
}
/* A method isAvailable that checks if there are items available for selling (returns false is no items are available,
true otherwise)*/
public boolean isAvailable(int numberOfItem){
if ( numberOfItem <= 0)
return false;
else
return true;
}
/* A toString method which returns a String containing the name of the food item and the selling price. If the
item is not available, add “(SOLD OUT)” next to the price, making use of the isAvailable method. Follow the
format shown in the example output below.*/
@Override
public String toString(){
String update ="";
if ( this.numberOfItem <= 0)
update =" (SOLD OUT)";
return"- "+this.name +"\n$ "+ this.price + update;
}
// A method setSellingPrice that takes a new price as a parameter, and updates the selling price instance variable.
public void setSellingPrice( double TheSellingPrice){
price = TheSellingPrice;
}
// A method decrementStock that decrements the number of items in stock by 1.
public int decrementStock(int x){
return numberOfItem -= x;
}
// A method increaseStock that takes an amount of additional stock as a parameter, and adds it to the existing stock available for selling. */
public int increaseStock(int x){
return numberOfItem += x;
}
}//FoodItem
file-name : FoodItem.java -------------------------------------------- this code is provided by you in the question itself, but I made one small change in this code ( indicated in red-bold color ) ---------------------------------------------------------------------------------------------------- public class FoodItem { /*Four instance variables: a name (a String), a cost (a double), a selling price (a double), and the number of items available in stock for selling (an int)*/ String name; double cost; double price; int numberOfItem; /* A constructor that takes four parameters, in the above order, which are used to initialize the four instance variables.*/ public FoodItem(String name, double cost, double price, int numberOfItem) { this.name = name; this.cost = cost; this.price = price; this.numberOfItem = numberOfItem; } /* A method isAvailable that checks if there are items available for selling (returns false is no items are available, true otherwise)*/ // I think, here we should not pass any parameter. // because it checks the item is available or not based on numberOfItem member. So we should not pass it as parameter. public boolean isAvailable() { if ( this.numberOfItem <= 0) return false; else return true; } /* A toString method which returns a String containing the name of the food item and the selling price. If the item is not available, add “(SOLD OUT)” next to the price, making use of the isAvailable method. Follow the format shown in the example output below.*/ @Override public String toString(){ String update =""; if ( this.numberOfItem <= 0) update =" (SOLD OUT)"; return"- "+this.name +"\n$ "+ this.price + update; } // A method setSellingPrice that takes a new price as a parameter, and updates the selling price instance variable. public void setSellingPrice( double TheSellingPrice){ price = TheSellingPrice; } // A method decrementStock that decrements the number of items in stock by 1. public int decrementStock(int x){ return numberOfItem -= x; } // A method increaseStock that takes an amount of additional stock as a parameter, and adds it to the existing stock available for selling. */ public int increaseStock(int x){ return numberOfItem += x; } }//FoodItem
========================================================================================================================
file-name: Order.java
----------------------------------------
import java.util.Arrays;
public class Order {
// instance variables
int orderNumber;
String customerName;
String restaurantName;
String appName;
FoodItem[] foodItem;
int numOfItemsOrdered;
double price;
// order number should be unique for each order.
static int currentOrderNumber = 900000;
// maximum number of items that can be ordered
final int maxOrders = 10;
//constructors
Order ( String name, String placeName, String appName) {
this.customerName = name;
this.restaurantName = placeName;
this.appName = appName;
foodItem = new FoodItem[maxOrders];
this.price = 0;
this.numOfItemsOrdered = 0;
this.orderNumber = ++currentOrderNumber;
} // END of constructor.
public String getRestaurantName() {
return restaurantName;
}
public String getAppName() {
return appName;
}
public String getCustomerName() {
return customerName;
}
public int getOrderNumber() {
return orderNumber;
}
public double getPrice() {
return price;
}
public FoodItem[] getFoodItem() {
return foodItem;
}
/**
* takes item name as input and checks whether the number of orders crosses MAX_ORDERS or not.
* if not then adds into the array.
* Every time an item is added into array ( i.e.. order successfully placed , stock of the item should be decremented by 1)
* @param item
* @return true , if added to the foodItem array
* else returns false
*/
public boolean addToOrder(FoodItem item) {
if(this.numOfItemsOrdered < 10 && item.isAvailable()) {
this.foodItem[numOfItemsOrdered] = item;
this.numOfItemsOrdered++;
this.price = this.price + item.price;
item.decrementStock(1);
return true;
}
return false;
}
public String toString() {
// helps in printing the food items:
StringBuffer sb = new StringBuffer();
FoodItem[] temp = getFoodItem();
for(FoodItem each: temp) {
if(each != null) {
sb.append(each.name);
sb.append(" | ");
}
}
String str = "Food App Name: "+getAppName() +
"\nOrder number: "+getOrderNumber() +
"\ncustomer Name: "+getCustomerName() +
"\nrestaurant name: " + getRestaurantName()+
"\nItems Ordered: " + sb +
"\n Total price: " +getPrice();
return str;
}
}
=====================================================================================
file name: Main.java --------------------------------------- NOTE : I Have implemented this Main class, just to show the functionality of Order and FoodItem class, I am not taking any user input here. I declared Objects to show you how it works. you can use Scanner class to read inputs from user ( like which food item to add )and intialise the object with constructor. It is easy to do that. ---------------------------------------------------------------------------------------------------------- public class Main { public static void main(String[] args) { //Creating some FoodItem objects FoodItem f1 = new FoodItem("Biryani",50,75,7); FoodItem f2 = new FoodItem("Dosa",10,15,1); FoodItem f3 = new FoodItem("Pizza",25,30,6); // Now here I am demonstrating how Orders work. Order o1 = new Order("Rahul", "Mc Donald's", "Swiggy"); o1.addToOrder(f1); o1.addToOrder(f2); // see here, f2 is not there in stock and now I am calling addToOrder method, so it returns false. if(o1.addToOrder(f2)) { System.out.println(" item ordered successfully"); } else { System.out.println(" Cannot add now, already reached max order limit"); } System.out.println(o1); System.out.println("**********************"); Order o2 = new Order("kiran","BAWARCHI","zomato"); o2.addToOrder(f1); o2.addToOrder(f3); System.out.println(o2); } }
---------------------------------------------------------------------------------------------------------------------------------------
OUTPUT:
THANK YOU !! If you have doubts ask in comments , I will add that in the answer. Please UPVOTE if you like my effort.