Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Designing a Software System from Scratch with OOAD & UML—A Restaurant System

DZone's Guide to

Designing a Software System from Scratch with OOAD & UML—A Restaurant System

Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

From the last few days i have been thinking to share my idea about how to design software system from scratch with the help of object oriented principles. The way the OOPS is taught to the students may enable them to write small programming assignments, but when it comes to design a whole system, many of them may falter. I would like to address that part with the  basic principle of OOAD and tell you how to traverse from the problem domain to the solution domain with the help of OOAD.

So, lets start. I have thought of an example to design a Restaurant system from scratch. I have used Java as the language, but one can use any OO language to achieve it.

To start designing of a software system using OOAD, one has to first identify the main entities participating in that system. So, in a Restaurant system who are the main participants? You guessed it right. A restaurant itself. Then the next entity is of course a customer. I think these are the main two entities. Now what does a restaurant consist of. It consists of some tables, a menu card, and food items. Another thing i have forgotten to mention. A restaurant should have a FIFO queue for its customers… right?

Now what kind of operation a Restaurant has to do? It should have some functionality to know if all tables are occupied, and if not which one is vacant; it should be able to book a vacant table when a new customer arrives. And when the customer leaves, it should be able to release that table and if anybody is waiting in its FIFO queue, it should be able to allocate that vacant table to that customer. It should also keep a track about who is the current customer it is serving and which customer has been allocated which table. Then it should also be able to generate a bill.

Now what does a Menu consist of? It has a list of some food items… right? So from Menu’s angle, it needs an entity called Item. Now what are the attributes of a food item vis-a-vis a restaurant? Each item should have an ID, a Name and its Price… right?

Now think that a restaurant will have a list of tables scattered systematically. So what kind of attributes a Table in a Restaurant may have? It should have an ID and it should know whether it is occupied or not. What kind of operation does a Table need? When a customer leaves the table, it should be able to tell the outside that it is occupied… right? On the other hand when a customer leaves the table it should let the outside world know that it is vacant and ready to accept new customer.

With these things in mind lets design the Restaurant, Table, Menu and Item classes.

Restaurant.pngitem.png



menu.pngtable.png

Now lets talk about the other main entity called the Customer. What are the attributes of a customer should have when he enters a restaurant. he must have an ID. i am not talking about his passport or SSN or Aadhar card ID. This ID is with respect to the restaurant by which the customer can be identified within the Restaurant premise. We will also add Name as another attribute to a customer. Although there is no absolute need for this from the OOAD designer’s perspective, this is needed only for decoration as you will see later. And what kind of operation a customer usually does. He looks at the menu, decides what he wants to have and how many plates… right? So the customer entity should have an operation called giveOrder. Now think about this. In order to give order customer needs an entity which will hold the customer’s choice, i.e., which item and how many plates for that item. Lets call this entity as ItemOrder, each ItemOrder object representing a specific Item and its number of plates ordered by the customer. So in the actual order by a customer, there will be a collection for all these ItemOrders objects created by the customer. Lets create a class as Order which will hold a list of such ItemOrder objects. This Order class can be used by the Resaturant entity for calculating the total amount to be billed to the customer… right? So in essence, we have got three major classes to design the customer and his flow of orders, namely Customer, ItemOrder, Order. Lets see how these classes will look like.

customer.pngorder.pngitemOrder.png

Remember, all setters and getters functions have been removed from these diagrams (except ItemOrder) to make it simple.

Okay, another helper class we need. That is the bill class. It should have the functionality to calculate the total amount to be billed for a particular customer.

bill.png

With all these classes ready, lets design the complete class diagram of the Restaurant System.

As you have probably guessed that a Restaurant HAS Tables and Restaurant HAS Menu. Menu HAS Item (rather items, a list of item). Restaurant has an ASSOCIATION with the Customer. The Bill stores a reference to a Customer for future use. Hence Bill has an AGGREGATION relationship with Customer. Each Customer creates an Order. Hence Customer HAS Order. And an Order holds a list of ItemOrders each representing and amount ordered for a particular Item. Hence Order HAS ItemOrder. Each ItemOrder keeps a reference of Item. Hence ItemOrder has an AGGREGATION relationship with Item. The Restaurant USEs Bill to calculate the total bill.

And with this explanation, the complete class diagram will look as the following.

Class Diagram.png

Please note that the complete signatures of the operations are not given in the UML diagrams to make it simple. You can refer the respective class from the source code attached to have an idea of each operation.

Let me  explain the UML in more detail. As you can see that Restaurant has a one-to-one HAS relationship with Menu and an one-to-many HAS relationship with the TABLE. This is quite obvious… right? Because a single Restaurant will have only one Menu list and it may have one to many number of TABLEs. The relationship of the Restaurant with the Customer is Association. Because a customer is an Independent entity. The Restaurant simply adds the new customer to its FIFO queue. As i have already mentioned, that a Customer HAS a Order (one-to-one relationship). Because whenever a customer plans to order, he needs to create an Order which he will populate with different ItemOrders(remember ItemOrder keeps a reference to an Item and how many plates the customer needs). As the ItemOrder keeps a reference to the Items, it obviously has an Aggregation relationship with the Item. A bill also has to keep a reference of the customer because it needs to identify for which customer the Bill has been generated. Hence the Bill also has an Aggregation relationship with Customer. The Restaurant has a USE relationship with the Bill because it creates the Bill object as a local variable in the generateBill function. Now probably you have acquired some sorts of idea about how this whole system has been designed using UML and OOAD.

The code for this whole Restaurant System has been given below. It is, of course in accordance to this class diagram given earlier.


The Restaurant Class:

package com.somitsolutions.training.java.restaurant;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;


public class Restaurant {
	private static final int MAX_NUMBER_OF_TABLES = 2;
	private List<Table> mTables;
	//whenever a new customer comes we add him to the front of this queue
	private Deque<Customer> mCustomerQueue; 
	private Menu mMenu;
	private List<Bill> billStore;
	private static Restaurant mResturantObject = null; 
	private Customer currentCustomer;
	private List<Customer> customerArray;
	
	
	private Restaurant(){
		mTables = new ArrayList<Table>();
		mMenu = new Menu();
		billStore = new ArrayList<Bill>();
		mCustomerQueue = new ArrayDeque<Customer>(5);
		customerArray = new ArrayList<Customer>();
		currentCustomer = null;
		for(int i = 0; i < MAX_NUMBER_OF_TABLES; i++){
			Table t = new Table();
			mTables.add(t);
		}
	}
	
	public List<Table> getTables(){
		return mTables;
	}
	
	public boolean isAnyTableOccupied(){
		for(int i = 0; i < MAX_NUMBER_OF_TABLES; i++){
			if (mTables.get(i).isOccupied() == true){
				return true;
			}	
		}
		return false;
	}
	
	private int findTheEmptyTable(){
		
		for(int i = 0; i < MAX_NUMBER_OF_TABLES; i++){
			if(mTables.get(i).isOccupied() == false){
				return i;
			}
		}
		return -1;
	}
	
	public void displayOccupiedTable(){
		for (int i = 0; i <MAX_NUMBER_OF_TABLES; i++){
			if(mTables.get(i).isOccupied() == true){
				System.out.println("Table No. " + i + " is occupied");
			}
		}
	}
	
	public boolean isAllTableOccupied(){
		int val = findTheEmptyTable();
		if (val == -1){
			return true;
		}
		else {
			return false;
		}
	}
	
	public void bookATable(){
		if(isAllTableOccupied() == false){
			int tableNumber = findTheEmptyTable();
			//get the customer from the list in the FIFO order
			//and remove him from the queue
			currentCustomer = mCustomerQueue.pollLast();
			//set the customer id same as table id, because a customer is identified
			//by the table which he occupies
			currentCustomer.setCustomerId(tableNumber);
			currentCustomer.getOrder().setOrderId(tableNumber);
			customerArray.add(tableNumber, currentCustomer);
			mTables.get(tableNumber).bookTable();
			System.out.println(currentCustomer.getCustomerName() + " has been given table number: "+ tableNumber);
			
		}
	}
	
	//input id is the customer id, which means it is nothing but the table id
	public void generateBill(int id){
		Customer customer = getCustomer(id);
		Bill newBill = new Bill(id,customer);
		billStore.add(newBill);
		System.out.println("Total Amount:" + newBill.calculateTotal());
		//return newBill.calculateTotal();
	}

	public Deque<Customer> getCustomerQueue() {
		return mCustomerQueue;
	}

	public void setCustomerQueue(Deque<Customer> customerQueue) {
		this.mCustomerQueue = customerQueue;
	}

	public Menu getMenu() {
		return mMenu;
	}

	public void setMenu(Menu mMenu) {
		this.mMenu = mMenu;
	}
	
	public void addNewCustomer(Customer customer){
		//Customer newCustomer =  new Customer();
		mCustomerQueue.offerFirst(customer);
	}
	
	public static Restaurant getRestaurant(){
		if(mResturantObject == null)
			mResturantObject = new Restaurant();
		return mResturantObject;
	}
	
	public Customer getCustomer(int id){
		return customerArray.get(id);
	}
	public void releaseTable(int tableId){
		if(mTables.get(tableId).isOccupied() == true){
			mTables.get(tableId).releaseTable();
			//mTables.remove(tableId);
			System.out.println("Table number" + tableId + "is available now...");
		}
		if(mCustomerQueue.isEmpty() == false){
			bookATable();
		}
		
		if ((mCustomerQueue.isEmpty() == true) && (isAnyTableOccupied() == true)){
			System.out.println("There are still some customers...");
		}
		
		if ((mCustomerQueue.isEmpty() == true) && (isAnyTableOccupied() == false)){
			System.out.println("All served...");
		}
		
	}

	
}


The Customer Class

package com.somitsolutions.training.java.restaurant;

public class Customer{

private int mCustomerId; //this is same as Table id. Each customer is identified

private Order mOrder;//by the table they occupied

private String mCustomerName;

public Customer(){

mCustomerId = 0;

mOrder = new Order(mCustomerId);

//addObserver(Restaurant.getRestaurant());

}

public Customer(int customerId){

mCustomerId = customerId;

mOrder = new Order(customerId);

}

public int getCustomerId() {

return mCustomerId;

}

public void setCustomerId(int mCustomerId) {

this.mCustomerId = mCustomerId;

}

public void giveOrder(Item item, int numberOfPlates){

//Order newOrder = new Order(mCustomerId);

ItemOrder newItemOrder = new ItemOrder(item,  numberOfPlates);

mOrder.addOrder(newItemOrder);

}

public void iAmDone(){

}

public Order getOrder() {

return mOrder;

}



public void setOrder(Order mOrder) {

this.mOrder = mOrder;

}



public String getCustomerName() {

return mCustomerName;

}



public void setCustomerName(String mCustomerName) {

this.mCustomerName = mCustomerName;

}

}


The Menu Class

package com.somitsolutions.training.java.restaurant;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;


public class Menu {

private List<Item> mMenu;

public Menu(){

mMenu = new ArrayList<Item>();

mMenu.add(new Item(11,"Tea", 5));

mMenu.add(new Item(22, "Coffee", 10));

mMenu.add(new Item(33, "Bread", 15));

}

public List<Item> getMenu(){

return mMenu;

}

public void display(){

int i = 0;

Iterator<Item> it = mMenu.iterator();

while(it.hasNext()){

Item currentItem = it.next();

System.out.println(i +":  " +currentItem.getItemId() + "  " + currentItem.getItemName() + "  " + currentItem.getItemPrice());

i++;

}

}

}

The Item Class

package com.somitsolutions.training.java.restaurant;

public class Item {

private int mItemId;

private String mName;

private float mPrice;

public Item(){

}

public Item(int id, String name, float price){

mItemId = id;

mName = name;

mPrice = price;

}

public int getItemId() {

return mItemId;

}

public void setItemId(int mItemId) {

this.mItemId = mItemId;

}

public String getItemName() {

return mName;

}

public void setItemName(String mName) {

this.mName = mName;

}

public float getItemPrice() {

return mPrice;

}

public void setItemPrice(float mPrice) {

this.mPrice = mPrice;

}

}

The ItemOrder Class

package com.somitsolutions.training.java.restaurant;

public class ItemOrder {

private Item mItem;

private int mNumberOfPlates;

public ItemOrder(){

mItem = null;

mNumberOfPlates = 0;

}

public ItemOrder(Item item, int numberOfPlates){

mItem = item;

mNumberOfPlates = numberOfPlates;

}

public Item getItem() {

return mItem;

}

public void setItem(Item mItem) {

this.mItem = mItem;

}

public int getNumberOfPlates() {

return mNumberOfPlates;

}

public void setNumberOfPlates(int mNumberOfPlates) {

this.mNumberOfPlates = mNumberOfPlates;

}

}

The Order Class

package com.somitsolutions.training.java.restaurant;

import java.util.ArrayList;

import java.util.List;

public class Order {

private int mOrderId;//this is the same as customerId imagining

//that one customer will create only one order

private List<ItemOrder> mItemOrder = new ArrayList<ItemOrder>();

public Order(){

mOrderId = 0;

//mItemOrder = null;

}

public Order(int orderId){

mOrderId = orderId;

}



public List<ItemOrder> getItemOrder() {

return mItemOrder;

}



public void setItemOrder(List<ItemOrder> mItemOrder) {

this.mItemOrder = mItemOrder;

}



public int getOrderId() {

return mOrderId;

}



public void setOrderId(int mOrderId) {

this.mOrderId = mOrderId;

}

public void addOrder(ItemOrder itemOrder){

mItemOrder.add(itemOrder);

}

}

The Bill Class

package com.somitsolutions.training.java.restaurant;

import java.util.Iterator;


public class Bill {

//private Order mOrder;

private int mBillId;//this is the same as orderId

private Customer mCustomer;

public Bill(int Id, Customer customer){

mBillId = Id;

mCustomer = customer;

}

/*public Order getOrder() {

return mOrder;

}

public void setOrder(Order mOrder) {

this.mOrder = mOrder;

}*/

public float calculateTotal(){

float retValue = 0;

Iterator<ItemOrder> it = mCustomer.getOrder().getItemOrder().iterator();

while (it.hasNext() == true){

ItemOrder element = it.next();

retValue+= (element.getItem().getItemPrice())* (element.getNumberOfPlates());

}

return retValue;

}

public int getBillId() {

return mBillId;

}

public void setBillId(int mBillId) {

this.mBillId = mBillId;

}

}

The Table Class

package com.somitsolutions.training.java.restaurant;

public class Table {

private boolean mOccupied;

//private int mCustomerId;

public Table(){

mOccupied = false;

}

public boolean isOccupied(){

return

mOccupied == true;

}

public void bookTable(){

mOccupied = true;

}

public void releaseTable(){

mOccupied = false;

}

}

The Main RestaurantSystem Class

package com.somitsolutions.training.java.restaurant;

import java.util.Scanner;

public class RestaurantSystem {

public static void main(String[] args) {

Scanner myScan = new Scanner(System.in);

String s = "Y";

Restaurant restaurant = Restaurant.getRestaurant();

do{

int tableId = 0;

int customerId = 0;

Customer customer = null;

System.out.println("Is there a new customer?");

String ans = myScan.nextLine();

if(ans.equalsIgnoreCase("Y")){

customer = new Customer();

System.out.println("Please enter customer name...");

String customerName = myScan.nextLine();

customer.setCustomerName(customerName);

restaurant.addNewCustomer(customer);

if(restaurant.isAllTableOccupied() == false){

restaurant.bookATable();

restaurant.getMenu().display();

System.out.println("Choose Menu from the above List");

System.out.println("How many items do you want to order?");

int number_of_items = myScan.nextInt();

for (int i = 0; i<number_of_items; i++){

System.out.println("Choose from the numbers at the leftmost position");

int item_position = myScan.nextInt();

System.out.println("How many plates of menu item " + item_position + " you want to order");

int number_of_plates = myScan.nextInt();

if(i == number_of_items - 1){

myScan.nextLine();

}

Item item = restaurant.getMenu().getMenu().get(item_position);

customer.giveOrder(item, number_of_plates);

}

}

else {

System.out.println("Sorry all tables are occupied... Please wait...");

System.out.println("Has anybody finished?");

System.out.println("Occupied tables are:");

Restaurant.getRestaurant().displayOccupiedTable();

System.out.println("Enter -9 if noone has finished...");

tableId = Integer.parseInt(myScan.nextLine());

//customerId = Integer.parseInt(myScan.nextLine());

//Customer customerDone = restaurant.getCustomer(customerId);

//customerDone.iAmDone();

if(tableId != -9 ){

restaurant.generateBill(tableId);

restaurant.releaseTable(tableId);

}

}

}

else {

if(restaurant.isAnyTableOccupied() == true){

//System.out.println("There are still some customers");

System.out.println("Has anybody finished?");

System.out.println("Enter -9 if noone has finished...");

System.out.println("Occupied tables are:");

Restaurant.getRestaurant().displayOccupiedTable();

int numberTable = myScan.nextInt();

myScan.nextLine();

if(numberTable != -9){

restaurant.generateBill(numberTable);

restaurant.releaseTable(numberTable);

}

}

}

System.out.println("Do you want to continue?");

s = myScan.nextLine();

if(s.equalsIgnoreCase("N")){

System.exit(0);

}

}while (s.equalsIgnoreCase("Y"));

}

}


The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}