Create a class named Order that performs order processing of a single item. The
ID: 3688474 • Letter: C
Question
Create a class named Order that performs order processing of a single item. The class has the following five fields:
Customer name
Customer number
Quantity ordered
Unit price
Total price
Include set and get methods for each field except the total price field. The set methods prompt the user for values for each field. This class also needs a method to compute the total price (quantity times unit price) and a method to display the field values.
Create a subclass named ShippedOrder that overrides computePrice() by adding a shipping and handling charge of $4.00. Write an application named UseOrder that instantiates an object of each of these classes. Prompt the user for data for the Order object, and display the results, then prompt the user for data for the ShippedOrder object, and display the results.
Explanation / Answer
public class OrderItem { String customername; int customernumber; int quantity; double unitprice; double totalprice; void setCustomerName(String cname) { customername = cname; } void setCustomerNumber(int cnum) { customernumber = cnum; } void setQuantity(int qtty) { quantity = qtty; } void setUnitPrice(double prce) { unitprice = prce; } String getCustomerName() { return customername; } int getCustomerNumber() { return customernumber; } int getQuantity() { return quantity; } double getUnitPrice() { return unitprice; } void Display() { System.out.println("Name:"+customername); System.out.println("Phone number:"+customernumber); System.out.println("Quantity:"+quantity); System.out.println("Unit price:RM"+unitprice); } void computeprice() { totalprice = quantity * unitprice; System.out.println("Total price is:RM"+totalprice); } } public class ShippedOrderclass extends OrderItem{ double newtotalpricecalc; void computeprice(int quantity, double price)// overriding method { newtotalprice = getUnitPrice() * getQuantity() +4; System.out.println("Total price is:RM"+newtotalpricecalc); } } import java.util.*; public class UseOrderTESTERCLASS { public static void main (String []args){ Scanner input = new Scanner(System.in); OrderItem Z = new Order(); ShippedOrderclass S = new ShippedOrderclass(); // OrderItem S = new ShippedOrder(); System.out.print("Enter Name:"); Z.setCustomerName(input.nextLine()); System.out.print("Enter Phone Number:"); Z.setCustomerNum(input.nextInt()); System.out.print("Enter Unit Price:RM"); Z.setUnitPrice(input.nextDouble()); System.out.print("Enter Quantity:"); Z.setQuantity(input.nextInt()); System.out.println("Please tell us if you Would your order to be shipped"); System.out.println("--Additional charges of RM4.00 will be added to total—f you want the shopping to be done"); System.out.print("Answer:"); String choicehere = input.next(); if ( choicehere.equalsIgnoreCase("Yes")) { S.computeprice(); } else Z.computeprice(); } Z.Display(); S.display(); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.