In: Computer Science
Please complete a program that will be an order entry program for a store or
organization. The program will accept order entry input from two customers, calculate
the total order amount of each order and display data about each order to the
customer. There will be two orders, one will be a “delivery” order and the other will be a “pickup” order.
The data entered by each customer will be:
Customer name
Item purchased
Quantity purchased
Price of the item
The information displayed for each customer order will be:
Order Date (system generated)
Delivery Location (hardcoded in constructor)
Order Type (program supplied)
Item description
Price
Quantity
Order total (program calculates)
Delivery Charge (constant = $10.00)
You can follow these steps to guide you through the process.
1. Create a public class named Order.
2. The Order class will have instance variables for each object to hold data pertaining to each
object as needed (for the input from the customer, program calculated and system generated
values).
3. The Order class will have two constructors (both considered overloaded) of which will set the
current system date for the order as well as the delivery type (Hint: use the LocalDate class for
the date).
a. One constructor will accept a parameter for the address and will be called when a
delivery order is placed. It will set a delivery type instance variable value as “delivery”.
b. The second overloaded constructor will not require a parameter for the address when a
pickup order is placed. It will set a delivery type instance variable value as “pickup”.
4. Create a second public class named OrderCart.
5. Create a main() method within the OrderCart class that will do the following:
a. Display a greeting.
b. Instantiate two Order class objects one for “pickup” and one for “delivery”.
o The “delivery” class object will pass a delivery address argument to the constructor.
At this time, you should hard code an address into the call to the constructor to
keep it simple. (Decision structures are not needed and therefore not used for this
program).
o The “pickup” class object will not pass any delivery information to the constructor.
c. Prompts and accepts input from the user for the all of the instance variables (customer
name, item description, quantity and price).
d. Calls two overloaded methods named calculateOrderTotal() to calculate the order total of
each order (no sales tax is charged in this example). Each method accepts an “Order”
object as it’s parameter.
o One overloaded method will accept a delivery charge of $10.00 and will be used to
calculate the order total for the “delivery” order. The total should include the
delivery charge (total = price * quantity + delivery charge).
o The second overloaded method will not accept any delivery charges and will used
to calculate the order total for the “pickup” order. The total charge will not include
a delivery charge (total = price * quantity).
o
e. Calls two overloaded methods named displayOrderData() to output the data for each
order. Each method accepts an “Order” object as it’s parameter.
o One overloaded method will accept a delivery charge of $10.00 and will be used to
display the order details including the delivery charge.
o The second overloaded method will not accept any delivery charges and will used
display the order specifics.
Hi,
Hope you are doing fine. I have coded the above question in java keeping all the requirements in mind. Since there is no sample output given, I have written the output according to my convenience. You may change it as per your needs. The code hhas been clearly explained using comments that have been highlighted in bold.
Program:
Order.java
import java.time.LocalDate;
//class order
public class Order {
//declaring all the attributes of the class as
per the question
String name;
String item;
int qty;
//price is taken in float. You may also
declare it as double
float price;
//using LocalDate to declare
orderDate
LocalDate orderDate;
String address;
String orderType;
//total stores the total price of
items
float total;
//no parameter constructor that sets orderType
to "pickup" and sets orderDate to current date
public Order() {
this.orderType = "pickup";
this.orderDate=LocalDate.now();
}
//parameterized constructor that sets address
along with setting orderType to "delivery" and oderDate to current
date
public Order(String address) {
this.address=address;
this.orderType = "delivery";
this.orderDate=LocalDate.now();
}
}
orderCart.java
//class orderCart
import java.util.Scanner;
public class OrderCart {
public static void main(String[] args) {
// TODO Auto-generated method
stub
//declaring scanner for
input
Scanner sc=new
Scanner(System.in);
//Welcome
message
System.out.println("Welcome to the
store");
//creating an object
delivery for order class with the address as
parameter
Order delivery=new Order("Brooklyn
street");
//creating an object pickup
for order class with no parameter constructor called
Order pickup=new
Order("Pickup");
//Taking details of
customer1 i.e delivery object
System.out.println("Customer
1");
System.out.println("Enter name:
");
delivery.name=sc.next();
System.out.println("Enter item
purchased: ");
delivery.item=sc.next();
System.out.println("Enter the
quantity: ");
delivery.qty=sc.nextInt();
System.out.println("Enter price of
the item: ");
delivery.price=sc.nextFloat();
System.out.println();
//Taking details of
customer2 i.e pickup object
System.out.println("Customer
2");
System.out.println("Enter name:
");
pickup.name=sc.next();
System.out.println("Enter item
purchased: ");
pickup.item=sc.next();
System.out.println("Enter the
quantity: ");
pickup.qty=sc.nextInt();
System.out.println("Enter price of
the item: ");
pickup.price=sc.nextFloat();
System.out.println();
//calculating total using
calculateOrderTotal() method
float
total_delivery,total_pickup;
//calculating order total
for delivery object by passing delivery charge as an additional
parameter
total_delivery=calculateOrderTotal(delivery,10.00);
//calculating order total
for pickup object
total_pickup=calculateOrderTotal(pickup);
//setting total attribute
for each object
delivery.total=total_delivery;
pickup.total=total_pickup;
//displaying
info
System.out.println("Info of
customer 1: "+delivery.name);
//display info of delivery
object
displayOrderData(delivery,10.00);
System.out.println();
//display info of pickup
object
System.out.println("Info of
customer 2: "+pickup.name);
displayOrderData(pickup);
}
//method to display details of objects of
orderType "delivery"
private static void displayOrderData(Order delivery,
double d) {
// TODO Auto-generated method
stub
System.out.println("Order date:
"+delivery.orderDate);
System.out.println("Delivery
Location: "+delivery.address);
System.out.println("Order type:
"+delivery.orderType);
System.out.println("Item
description: "+delivery.item);
System.out.println("Price:
$"+delivery.price);
System.out.println("Quantity:
"+delivery.qty);
System.out.println("Order total:
$"+delivery.total);
System.out.println("Delivery
charge: $"+d);
}
//method to display details of objects of
orderType "pickup"
private static void displayOrderData(Order pickup)
{
// TODO Auto-generated method
stub
System.out.println("Order date:
"+pickup.orderDate);
System.out.println("Order type:
"+pickup.orderType);
System.out.println("Item
description: "+pickup.item);
System.out.println("Price:
$"+pickup.price);
System.out.println("Quantity:
"+pickup.qty);
System.out.println("Order total:
$"+pickup.total);
}
//method to calculate order total of
objects of orderType "pickup"
private static float calculateOrderTotal(Order pickup)
{
// TODO Auto-generated method
stub
float
total=pickup.price*pickup.qty;
return total;
}
//method to calculate order total of
objects of orderType "delivery"
private static float calculateOrderTotal(Order
delivery, double d) {
// TODO Auto-generated method
stub
float
total=delivery.price*delivery.qty+(float)d;
return total;
}
}
Executable code snippets:
Order.java
orderCart.java
Output: