Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners restrictedorder
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


arvind
restrictedorder
0 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners


<p>//Three classes: OrderController, OrderUI, Order</p>

<p>import java.util.regex.*; //Use of ArrayList
import java.util.*; //Use of ArrayList
import java.io.*; //Use of InputStreamReader and BufferedReader</p>

<p>//Controller class:
//Methods: initiateOrderDetailsAddition():void ; initiateOrderDetailsView():void ; initiateViewBills():void</p>

<p>class OrderController
{</p>

<p>   //Contract: initiateOrderDetailsAddition():void
   //Controls the addition of order details
   public void initiateOrderDetailsAddition() //UC2(a): Add order details
   {
    OrderUI orderUI=new OrderUI();
    double totalPrice;
    Order order;
    boolean purchase=false;
    String temp="";
    ArrayList&lt;Object> orderDetails=new ArrayList&lt;Object>();
    orderDetails=orderUI.getOrderDetails();
    if(orderDetails!=null) //If no system error occurs
      {
         temp=CDAManufacturers.validateOrderDetails(orderDetails); //Checks if the customer, product id are in their respective lists
         if(temp.equals("")) //If the inputs are valid
         {           </p>

<pre class="prettyprint">          order=CDAManufacturers.addOrderDetails(orderDetails); //Add order details to the order list
          if(order!=null) //If no system error occurs
            orderUI.notifyOrderAdditionSuccess(order); //Prompt user successful addition of order details
        else
        {
          orderUI.displayErrorMessage("Order addition aborted!!!"); //Prompt user that his order process has been aborted
          orderUI.displayMenu(); //Display clerk's menu
        }
</code></pre>

<p> }
         else
         {
           orderUI.displayErrorMessage(temp); //Prompt user the invalid ids
           orderUI.displayMenu(); //Display clerk's menu
         }
      }
      else
       {
  orderUI.notifyOrderFailure(); //Prompt user failure of order details addition due to system error
 } <br />
     }</p>

<p>  //Contract: initiateOrderDetailsView():void
  //Controls the viewing of order details
  public void initiateOrderDetailsView()  //UC2(b): View order details
  {</p>

<pre class="prettyprint">BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
OrderUI orderUI=new OrderUI();
Order orderOb=null;
ArrayList&lt;Object&gt; orderDetails=new ArrayList&lt;Object&gt;();
int orderId=orderUI.getOrderId();
if(orderId!=-1) //If system error occurs
{
  orderOb=CDAManufacturers.searchOrderList(orderId); //Check order id is in the order list
  if(orderOb!=null) //If order id is in the order list
  {
    orderDetails=CDAManufacturers.returnOrderDetails(orderOb); //Fetch order details from the order list
    orderUI.displayOrderDetails(orderDetails); //Display order details
    orderUI.displayMenu(); //clerk's menu is displayed
  }
  else
  {
    orderUI.displayErrorMessage("Invalid order ID!!!!");
    orderUI.displayMenu(); //clerk's menu is displayed
  }
}
else
{
  orderUI.displayErrorMessage("Sorry!!! unable to display the order details");
  orderUI.displayMenu(); //clerk's menu is displayed
}
</code></pre>

<p>  }
 //Contract: initiateViewBills():void
  //Controls the viewing of bill details
 public void initiateViewBills()
 {</p>

<pre class="prettyprint">BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
OrderUI orderUI=new OrderUI();
Order orderOb=null;
double bill=0;
ArrayList&lt;Object&gt; orderDetails=new ArrayList&lt;Object&gt;();
int orderId=orderUI.getOrderId();
if(orderId!=-1) //If system error occurs
{
  orderOb=CDAManufacturers.searchOrderList(orderId); //Check order id is in the order list
  if(orderOb!=null) //If order id is in the order list
  {
    orderDetails=CDAManufacturers.returnOrderDetailsForBill(orderOb); //Fetch order details from the order list
    bill=CDAManufacturers.calculateBill(orderDetails);
    bill=bill*1.4;
    System.out.println("Rs."+bill);
    orderUI.displayMenu(); //clerk's menu is displayed
  }
  else
  {
    orderUI.displayErrorMessage("Invalid order ID!!!!");
    orderUI.displayMenu(); //clerk's menu is displayed
  }
}
else
{
  orderUI.displayErrorMessage("Sorry!!! unable to display the order details");
  orderUI.displayMenu(); //clerk's menu is displayed
}
</code></pre>

<p>  }
}</p>

<p>//---------------------------------------------------------------------------------------------------------------</p>

<p>//Class OrderUI:
//Methods: getOrderDetails():List ; showOrderDetailsAdditionScreen():list ; diplayProductList(list):void
//         requestOrderAdditionConfirmation():boolean ; displayErrorMessage(String):void ;notifyAddProduct():String
//         displayErrorMessage(String):void ; notifyOrderAdditionSuccess(Object):void ; displayOrderId(int):void
//         notifyOrderFailure():void ; displayMenu():void ; getOrderId():int ; showOrderIdScreen():int; displayOrderDetails(List):void ; </p>

<p>class OrderUI
{
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));</p>

<p>  //Contract: getOrderDetails():List
  //It reads the order details and returns the order details list
  public ArrayList&lt;Object> getOrderDetails()
  {
    ArrayList&lt;Object> orderDetails=showOrderDetailsAdditionScreen(); //Read order details from user
    return(orderDetails);
  }</p>

<p>  //Contract: showOrderDetailsAdditionScreen():list
  //Takes order details and validates the inputs
  private ArrayList&lt;Object> showOrderDetailsAdditionScreen()
  {
    String addProductFlag="";
    ArrayList&lt;Object> orderDetails=new ArrayList&lt;Object>();
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    boolean flag=false;
    String temp;
    try
    {
      System.out.println();
      while(flag==false) //Repeatedly asks for customer id until a feasible id is entered
      {
        System.out.println("");
        System.out.println("Enter customer id: ");
        temp=br.readLine();
        Pattern p=Pattern.compile("[0-9][0-9]*"); //Input must be a positive integer
        Matcher m=p.matcher(temp);
        if(m.matches())
        {
          orderDetails.add(temp);
          flag=true;
        }
        else
          System.out.println("A positive integer expected..."); //Prompt user if an incorrect input is entered
      }
      diplayProductList(CDAManufacturers.returnProductList());
      addProductFlag=notifyAddProduct();
      while(addProductFlag.equalsIgnoreCase("y")) //Checks if the user wants to buy an product
      {
      flag=false;
      while(flag==false) //Repeatedly asks for product id until a feasible id is entered
      {
        System.out.println("");
        System.out.println("Enter the Product Id ");
        temp=br.readLine();
        Pattern p=Pattern.compile("[0-9][0-9]*"); //Input must be a positive integer
        Matcher m=p.matcher(temp);
        if(m.matches())
        {
          orderDetails.add(temp);
          flag=true;
        }
        else
          System.out.println("A positive integer expected..."); //Prompt user if an incorrect input is entered
     }
        flag=false;
      while(flag==false) //Repeatedly asks for product id until a feasible id is entered
      {
        System.out.println("");
        System.out.println("Enter the Product quantity(in numbers): ");
        temp=br.readLine();
        Pattern p=Pattern.compile("[0-9][0-9]*"); //Input must be a positive integer
        Matcher m=p.matcher(temp);
        if(m.matches())
        {
          orderDetails.add(temp);
          flag=true;
        }
        else
          System.out.println("A positive integer expected..."); //Prompt user if an incorrect input is entered
          } 
        addProductFlag=notifyAddProduct();
        }
      return orderDetails;
    }
    catch(Exception e)
    {
      return null;
    }
  }</p>

<p> //Contract: diplayProductlList(list):void
  //Displays product list as catalogue
  private void diplayProductList(ArrayList&lt;Product> productList)
  {
    System.out.println("The details of the product available:");
    System.out.println("");
    System.out.println("Product Id \t\tProduct Name"); 
    System.out.println("");
    for(Product productOb:productList)
      System.out.println(productOb.getProductId()+"\t\t"+productOb.getProductName()); //Display product id and name
  }</p>

<p>  //Contract: notifyAddProduct():String
  //Ask user if he wants to buy an additional product
  public String notifyAddProduct()
  {
   String temp="";
   boolean flag=false;
    try
    {
      while(flag==false) //Repeatedly ask for input until a valid value is entered
      {
        System.out.println("Want to add Product to order? (Y/N)");
        temp=br.readLine();
        if(temp.equalsIgnoreCase("y")||temp.equalsIgnoreCase("n")) //Input can only be y or n
          flag=true;
        else
          System.out.println("Please enter Y or N..."); //Prompt user of an invalid input
      }
      return(temp);
    }
    catch(Exception e)
    {
      //System.err.println("Exception: "+e);
      return("n");
    } 
  } </p>

<p>  //Contract: requestOrderAdditionConfirmation():boolean
  //Request user for order confirmation
  private boolean requestOrderAdditionConfirmation()
  {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    boolean flag=false;
    String cont="";
    try
    {
      while(flag==false) //Repeatedly ask for input until a valid value is entered
      {
        System.out.println("Are you sure you want to purchase? (Y/N)");
        cont=br.readLine();
        if(cont.equalsIgnoreCase("y")||cont.equalsIgnoreCase("n")) //Input can only be y or n
          flag=true;
        else
          System.out.println("Please enter Y or N..."); //Prompt user of an invalid input
      }
      if(cont.equalsIgnoreCase("n"))
        return(false);
      else
        return(true);
    }
    catch(Exception e)
    {
      return false;
    }
  }</p>

<p>  //Contract: displayErrorMessage(String):void
  //Displays the error message passed
  public void displayErrorMessage(String error)
  {
    System.out.println();
    System.err.println(error); //Diplay the error encountered
  }</p>

<p>  //Contract: notifyOrderAdditionSuccess(Object):void
  //Notify user of successful order and display order id
  public void notifyOrderAdditionSuccess(Order order)
  {
      displayOrderId(order.getOrderId()); //Display order id generated
      displayMenu(); //Display clerk's menu
  }</p>

<p>  //Contract: displayOrderId(int):void
  //Displays the order id
  private void displayOrderId(int orderId)
  {
   System.out.println();
   System.out.println("The customer with order Id: "+orderId+" has ordered the products successfully");
   displayMenu(); //Display clerk's menu
  }</p>

<p>  //Contract: notifyOrderFailure():void
  //Notify user of the order failure
  public void notifyOrderFailure()
  {
    System.out.println();
    System.out.println("The order process failed");
    displayMenu(); //Display clerk's menu
  }</p>

<p>  //Contract: displayMenu():void
  //Display clerk's menu
  public void displayMenu()
  {
    try
    {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.println();
      System.out.println("Press any key to continue");
      br.readLine();
    }
    catch(Exception e)
    {
      System.err.println("Exception: "+e);
    }
    MainClerkSystem mainclerkSystemOb=new MainClerkSystem();
    mainclerkSystemOb.initiateClerkView();
  }</p>

<p>  //Contract: getOrderId():int
  //Takes order id and returns it
  public int getOrderId()
  {
      int orderId;
      orderId=showOrderIdScreen(); //Read order id from user
      return(orderId);
  }</p>

<p>  //Contract: showOrderIdScreen():int
  //Take a valid order id from user
  private int showOrderIdScreen()
  {
    String temp;
    boolean flag=false;
    int orderId=-1;
    try
    {
      while(flag==false) //Repeatedly ask user to enter a value until a feasible input is entered
      {
        System.out.println("");
        System.out.println("Enter the order Id:");
        temp=br.readLine();
        Pattern p=Pattern.compile("[0-9][0-9]*"); //Input must be a positive integer
        Matcher m=p.matcher(temp);
        if(m.matches())
        {
          orderId=Integer.parseInt(temp);
          flag=true;
        }
        else
          System.out.println("A positive integer expected..."); //Prompt user of an invalid input
      }
      return(orderId);
    }
    catch(Exception e)
    {
      System.err.println("Exception: "+e);
      return(-1);
    }
  }</p>

<p>  //Contract:displayOrderDetails(List):void
  //Displays the order details which is passed as a list
  public void displayOrderDetails(ArrayList&lt;Object> orderDetails)
  {
    System.out.println("");
    System.out.println("Order id :\t\t"+orderDetails.get(0));
    System.out.println("Customer name :\t"+orderDetails.get(1));
    System.out.println("Order status :\t"+orderDetails.get(2));
    System.out.println("Date Of Order :\t"+orderDetails.get(3));
    System.out.println("Date Of Delivery :\t"+orderDetails.get(4));
    int x=((orderDetails.size()-4)/2);
    System.out.println("");
    System.out.println("Quantity ordered \t Products ordered");
    for(int i=5;i<orderDetails.size()-x;i++)
     {
 System.out.println("\t"+orderDetails.get(i+x)+"\t"+orderDetails.get(i));
     }
  }                                </p>

<p>  }</p>

<p>//----------------------------------------------------------------------------------------------------------</p>

<p>//Class Order:
//Attributes: int id ; String status ; String dateOfOrder ; String dateOfDelivery
//            int customerId ; Integer[] productIdList ; Integer[] productQuantityList
//Methods:    setOrderId(int):void ; getOrderId():int ; setCustomerId(int):void ; getCustomerId():int
//            setProductId(List):void ; getProductId():list ; setProductQuantity(List):void ; getProductQuantity():list 
//            setStatus(String):void ; getStatus():String ; setDateOfOrder(String):void ; getDateOfOrder():String
//            setDateOfDelivery(String):void ; getDateOfDelivery():String ; </p>

<p>class Order
{
  private int id;
  private String status;
  private String dateOfOrder;
  private String dateOfDelivery;
  private int customerId;
  private ArrayList&lt;Integer> productIdList=new ArrayList&lt;Integer>();
  private ArrayList&lt;Integer> productQuantityList=new ArrayList&lt;Integer>();</p>

<p>  //Contract: setOrderId(int):void
  //It modifies order id
  public void setOrderId(int id)
  {
    this.id=id;
  }</p>

<p>  //Contract: getOrderId():int
  //It returns order id
  public int getOrderId()
  {
    return id;
  }</p>

<p>  //Contract: setCustomerId(int):void
  //It modifies customer id
  public void setCustomerId(int customerId)
  {
    this.customerId=customerId;
  }</p>

<p>  //Contract: getCustomerId():int
  //It returns customer id
  public int getCustomerId()
  {
    return customerId;
  }</p>

<p>  //Contract: setProductId(List):void
  //It modifies Product id list
  public void setProductId(ArrayList&lt;Integer> tempProductId)
  {
    productIdList.clear();
    for(Integer productId:tempProductId)
      productIdList.add(productId);
  }</p>

<p>  //Contract: getProductId():list
  //It returns Product id list
  public ArrayList&lt;Integer> getProductId()
  {
    return productIdList;
  }</p>

<p>//Contract: setProductQuantity(List):void
  //It modifies Product Quantity list
  public void setProductQuantity(ArrayList&lt;Integer> tempProductQuantity)
  {
    productQuantityList.clear();
    for(Integer productQuantity:tempProductQuantity)
      productQuantityList.add(productQuantity);
  }</p>

<p>  //Contract: getProductQuantity():list
  //It returns Product Quantity list
  public ArrayList&lt;Integer> getProductQuantity()
  {
    return productQuantityList;
  }</p>

<p>  //Contract: setStatus(String):void
  // It modifies order status
   public void setStatus(String status)
  {
    this.status=status;
  }</p>

<p>  //Contract: getStatus():String
  //It returns order status
  public String getStatus()
  {
    return status;
  }</p>

<p>  //Contract:setDateOfOrder(String):void
  // It modifies date of order
   public void setDateOfOrder(String dateOfOrder)
  {
    this.dateOfOrder=dateOfOrder;
  }</p>

<p>  //Contract: getDateOfOrder():String
  //It returns date of order
  public String getDateOfOrder()
  {
    return dateOfOrder;
  }</p>

<p>  //Contract:setDateOfDelivery(String):void
  // It modifies date of delivery
  public void setDateOfDelivery(String dateOfDelivery)
  {
    this.dateOfDelivery=dateOfDelivery;
  }</p>

<p>  //Contract: getDateOfDelivery():String
  //It returns date of delivery
  public String getDateOfDelivery()
  {
    return dateOfDelivery;
  }</p>

<p>}</p>

Advertisement
View Answers

Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com