Homework3 CS3365-Fall2017 You are required to implement the Make Order Request u
ID: 3596915 • Letter: H
Question
Homework3 CS3365-Fall2017 You are required to implement the Make Order Request use case using JaveServer Page (JSP), MySql database, and Java. The following depicts the communication diagrams for the Make Order Request use case, followed by the classes. The Customer Account and Delivery Order tables are given along with their intial data. You might need to change the table schema to add a key or foreighn key Main Sequence for Make Order Request use case MI: Order Request M12: Customer Output M2: Order MII: Order M5: Authorize Credit Card Request M3: Account Request business logo M&: Credit Card Approved M4 [Available: Account Ino M6: Authorize | | M7: [Approval) MIla: Send Order Confirmation Email M9: Create Order MI0: Order Bank CS3365 12Explanation / 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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.