I need help with a Java Program: Include the following Java classes to the above
ID: 3911624 • Letter: I
Question
I need help with a Java Program:
Include the following Java classes to the above created Java file. Define all the instance variables with private access modifier and constructors, instance methods with public access modifier.
Item class: This class has following instance variables, constructor and instance methods.
Order class: This class has following instance variables, static variable, constructor, instance methods and static method.
Part B:
Within the main method of the class, Customer creates three objects of class Order. Say ord1, ord2, and ord3.
Initialize the objects using the defined constructor.
Assign the following values to customer name and the three items in each of the Order objects.
?
- For each order ord1, ord2 and ord3, Display the details of three items. Display the total price of all the items. Display line of asterisk marks to separate.
System.out.println(“***********************************”);?
-Display the ordernumber of the next order.
Sample output of this program:
itemName String unitPrice double numberOfUnits: int Variables Constructor Default constructor Instance Methods nitPrice, and numberOfUnits Get and set methods for itemName, u variables. itemTotal : Find the total price (unitPrice numberOfUnits) for that particular item and return itExplanation / Answer
Item.java
public class Item {
//Declaring instance variables
private String itemName;
private Double unitPrice;
private int numberOfUnits;
//Zero argumented constructor
public Item() {
super();
}
//getters and setters
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(Double unitPrice) {
this.unitPrice = unitPrice;
}
public int getNumberOfUnits() {
return numberOfUnits;
}
public void setNumberOfUnits(int numberOfUnits) {
this.numberOfUnits = numberOfUnits;
}
//Method which calculates the ItemTotal
public double itemTotal()
{
return unitPrice*numberOfUnits;
}
}
___________________
Order.java
import java.util.Random;
public class Order {
//Declaring instance variables
private int orderId;
private String customerName;
private Item it1=new Item();
private Item it2=new Item();
private Item it3=new Item();
//Declaring static variable
static int nextOrderID=1000;
//Zero argumented constructor
Order()
{
//Creating a random Class object
Random r = new Random();
nextOrderID+=(r.nextInt(10) + 1);
this.orderId=nextOrderID;
}
//getters and setters
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public Item getIt1() {
return it1;
}
public Item getIt2() {
return it2;
}
public Item getIt3() {
return it3;
}
public void setIt1(String name,double unitPrice,int noOfUnits)
{
it1.setItemName(name);
it1.setUnitPrice(unitPrice);
it1.setNumberOfUnits(noOfUnits);
}
public void setIt2(String name,double unitPrice,int noOfUnits)
{
it2.setItemName(name);
it2.setUnitPrice(unitPrice);
it2.setNumberOfUnits(noOfUnits);
}
public void setIt3(String name,double unitPrice,int noOfUnits)
{
it3.setItemName(name);
it3.setUnitPrice(unitPrice);
it3.setNumberOfUnits(noOfUnits);
}
//Method which displays the Order Details
public void displayOrderDetails()
{
System.out.println("Order #: "+orderId);
System.out.println("Customer :"+customerName);
System.out.println();
System.out.println("Item Unit Price #units Item Total");
System.out.println(getIt1().getItemName()+" "+getIt1().getUnitPrice()+" "+getIt1().getNumberOfUnits()+" "+getIt1().itemTotal());
System.out.println(getIt2().getItemName()+" "+getIt2().getUnitPrice()+" "+getIt2().getNumberOfUnits()+" "+getIt2().itemTotal());
System.out.println(getIt3().getItemName()+" "+getIt3().getUnitPrice()+" "+getIt3().getNumberOfUnits()+" "+getIt3().itemTotal());
}
//Method which displays order Total
public double displayOrderTotal()
{
return getIt1().itemTotal()+getIt2().itemTotal()+getIt3().itemTotal();
}
//Static Method which displays next order number
public static void displayNextOrderNumber()
{
System.out.println("Next Order Number :"+new Order().nextOrderID);
}
}
_____________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating variables of type Order class
Order ord1,ord2,ord3;
//Creating Order class objects
ord1=new Order();
ord2=new Order();
ord3=new Order();
//Calling the setter methods on the Order#1 by passing the inputs
ord1.setCustomerName("Harry Potter");
ord1.setIt1("book", 3.5, 5);
ord1.setIt2("pen", 1.5, 3);
ord1.setIt3("ink", 7.5, 1);
//Calling the setter methods on the Order#2 by passing the inputs
ord2.setCustomerName("Hermione Granger");
ord2.setIt1("book", 4.5, 7);
ord2.setIt2("ink", 7.5, 1);
ord2.setIt3("paper", 0.02, 500);
//Calling the setter methods on the Order#3 by passing the inputs
ord3.setCustomerName("Ron Weasley");
ord3.setIt1("wand", 15.75, 1);
ord3.setIt2("book", 3.5, 3);
ord3.setIt3("pencil", 1.5, 4);
//Displaying the order details of Order#1
ord1.displayOrderDetails();
ord1.displayOrderTotal();
System.out.println("***************************");
//Displaying the order details of Order#2
ord2.displayOrderDetails();
ord2.displayOrderTotal();
System.out.println("***************************");
//Displaying the order details of Order#3
ord3.displayOrderDetails();
ord3.displayOrderTotal();
System.out.println("***************************");
Order.displayNextOrderNumber();
}
}
_______________________
Output:
Order #: 1008
Customer :Harry Potter
Item Unit Price #units Item Total
book 3.5 5 17.5
pen 1.5 3 4.5
ink 7.5 1 7.5
***************************
Order #: 1011
Customer :Hermione Granger
Item Unit Price #units Item Total
book 4.5 7 31.5
ink 7.5 1 7.5
paper 0.02 500 10.0
***************************
Order #: 1015
Customer :Ron Weasley
Item Unit Price #units Item Total
wand 15.75 1 15.75
book 3.5 3 10.5
pencil 1.5 4 6.0
***************************
Next Order Number :1021
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.