You are required to implement the Make Order Request use case using an object-or
ID: 3871821 • Letter: Y
Question
You are required to implement the Make Order Request use case using an object-orient langauge or Java). The following depicts the communication diagrams for the Make Order Request use case followed by the classes for the use case. Implement the Make Order Request use case as specified using the objects instantiated from the classes. Your implementation of Make Order Request should follow the sequence of communication diagram using the methods defined in classes. State why you need to add new methods or change the methods specified in classes if you made changed (or added) to the methods in classes Main Sequence for Make Order Request use case MI: Orde Request Interthce M12: Cuslomer Outp MS: Authorize Crode Cand Request M3: Account MS: Croda Cad Awailale MEc Autherine Mila: Sead Ouder M9: Create Number Bank OrdeExplanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
class Bank{
//Dummy Cards and balances
private String cards[]={
"1234567890","2345678901","135792469"
};
double balances[]={300,100,50.50};
private static int authNumCounter=707707;
//A card is authorized if it exists, and has sufficient balance
boolean isCardAuthorized(String cardNo, double amount){
for(int i=0;i<cards.length;i++){
if(cards[i].equals(cardNo)){
if(balances[i]>=amount){
return true;
}
}
}
return false;
}
public String getAuthNum(String creditcard,double amount) {
if(!isCardAuthorized(creditcard,amount)){
return String.valueOf(authNumCounter++);
}
return null;
}
}
class Customer{
ArrayList<Order>orderHistory;
Order currentOrder;
static int customerIdCounter=1001;
int customerId;
public Customer(String order){
orderHistory=new ArrayList<>();
currentOrder=new Order(order);
customerId=customerIdCounter;
customerIdCounter++;
}
public Order getCurrentOrder(){
return currentOrder;
}
}
//The main controller
class SystemController{
//Delivery order
class DeliveryOrder{
Order order;
int CustomerId;
String creditCardAuthNumber;
public DeliveryOrder(Order order, int customerId, String creditCardAuthNumber) {
this.order = order;
CustomerId = customerId;
this.creditCardAuthNumber = creditCardAuthNumber;
}
}
//List of all customers who have an account and delivery orders
ArrayList<Customer> registeredCustomers;
ArrayList<DeliveryOrder> deliveryOrders;
Bank bank;
public SystemController(){
registeredCustomers=new ArrayList<>();
deliveryOrders=new ArrayList<>();
bank = new Bank();
}
//Check if customer already have an account
boolean isCustomerRegistered(Customer cust){
for(Customer c:registeredCustomers){
if(c==cust) return true;
}
return false;
}
//Main logic
public void placeOrder(Customer cust, String creditcard)
{
Scanner in = new Scanner(System.in);
//If customer has no account prompt him to make one,or cancel order
if(!isCustomerRegistered(cust)){
System.out.println("New User? Register(R) to continue.. Or Cancel(C) order: ");
String choice=in.nextLine().trim();
System.out.println(choice);
if(choice.equals("R")){
registerCustomer(cust);
System.out.println("You are now registerd with us!! Please Wait...");
}else{
System.out.println("Your order has been cancelled");
return;
}
}
//Prompt The right card number, until the Customer either cancels order or provides
//The correct card info
while(!bank.isCardAuthorized(creditcard, cust.getCurrentOrder().getTotalAmount())){
System.out.println("Please enter the correct card, or Cancel(C) the order: ");
String input = in.nextLine().trim();
if(input.equals("C")) return;
creditcard=input;
}
in.close();
Order currentOrder=cust.getCurrentOrder();
String authNum=bank.getAuthNum(creditcard,currentOrder.getTotalAmount());
//Creating a Delivery Order, and storing it for record
DeliveryOrder o = new DeliveryOrder(cust.getCurrentOrder(),cust.customerId,authNum);
deliveryOrders.add(o);
//Displaying Order Summary
System.out.println("Purchase Approved! Order Summary:");
currentOrder.displayOrder();
System.out.println("Total Amount: "+currentOrder.getTotalAmount());
}
public void registerCustomer(Customer cust){
registeredCustomers.add(cust);
}
}
class Order{
//Some fictional order items, and their associated prices
static final String items[] = {"A","B","C","D"};
static final double price[] ={10,15,11.50,170};
//Quantity chosen per item
int qty[]={0,0,0,0};
double totalCost;
//For simplicity order comes
//In the format of 4A-0B-3C-0D
public Order(String orders){
String[] data=orders.split("-");
for(int i=0;i<items.length;i++){
qty[i]=Character.getNumericValue(data[i].charAt(0));
}
//Calculating total Cost
totalCost=0;
for(int i=0;i<qty.length;i++){
totalCost+=(qty[i]*price[i]);
}
}
public double getTotalAmount(){
return totalCost;
}
public void displayOrder(){
System.out.println("Item Quantity PricePerUnit");
for(int i=0;i<qty.length;i++){
if(qty[i]!=0){
System.out.println(items[i]+" "+qty[i]+" "+price[i]);
}
}
}
}
public class PurchaseController {
public static void main(String[] args) {
SystemController sc = new SystemController();
Customer cust = new Customer("1A-9B-2C-0D");
sc.placeOrder(cust, "00000000");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.