w3resource

Java: Restaurant menu, average rating

Java OOP: Exercise-18 with Solution

Write a Java program to create a class called "Restaurant" with attributes for menu items, prices, and ratings, and methods to add and remove items, and to calculate average rating.

Sample Solution:

Java Code:

 import java.util.ArrayList;

public class Restaurant {
  private ArrayList<String> menuItems;
  private ArrayList<Double> prices;
  private ArrayList<Integer> ratings;
  private ArrayList<Integer> itemCounts;

  public Restaurant() {
    this.menuItems = new ArrayList<String>();
    this.prices = new ArrayList<Double>();
    this.ratings = new ArrayList<Integer>();
    this.itemCounts = new ArrayList<Integer>();
  }

  public void addItem(String item, double price) {
    this.menuItems.add(item);
    this.prices.add(price);
    this.ratings.add(0);
    this.itemCounts.add(0);
  }

  public void removeItem(String item) {
    int index = this.menuItems.indexOf(item);
    if (index >= 0) {
      this.menuItems.remove(index);
      this.prices.remove(index);
      this.ratings.remove(index);
      this.itemCounts.remove(index);
    }
  }

  public void addRating(String item, int rating) {
    int index = this.menuItems.indexOf(item);
    if (index >= 0) {
      int currentRating = this.ratings.get(index);
      int totalCount = this.itemCounts.get(index);
      this.ratings.set(index, currentRating + rating);
      this.itemCounts.set(index, totalCount + 1);
    }
  }

  public double getAverageRating(String item) {
    int index = this.menuItems.indexOf(item);
    if (index >= 0) {
      int totalRating = this.ratings.get(index);
      int itemCount = this.itemCounts.get(index);
      return itemCount > 0 ? (double) totalRating / itemCount : 0.0;
    } else {
      return 0.0;
    }
  }

  public void displayMenu() {
    for (int i = 0; i < menuItems.size(); i++) {
      System.out.println(menuItems.get(i) + ": $" + prices.get(i));
    }
  }

  public double calculateAverageRating() {
    double totalRating = 0;
    int numRatings = 0;
    for (int i = 0; i < ratings.size(); i++) {
      totalRating += ratings.get(i);
      numRatings++;
    }
    return numRatings > 0 ? totalRating / numRatings : 0.0;
  }
}

The above Java class defines a restaurant with menu items, prices, and ratings. It has a constructor that initializes three ArrayLists for the menu items, prices, and ratings. It also has methods to add and remove items from the menu and add ratings for each item. The class also includes a method to calculate the average rating for a given menu item. It also includes a method to display the current menu.

//main.java
public class Main {
  public static void main(String[] args) {
    Restaurant restaurant = new Restaurant();
    restaurant.addItem("Burger", 8.99);
    restaurant.addItem("Pizza", 10.99);
    restaurant.addItem("Salad", 6.00);

    System.out.println("Menu:Item & Price");
    restaurant.displayMenu();

    restaurant.addRating("Burger", 4);
    restaurant.addRating("Burger", 5);
    restaurant.addRating("Pizza", 3);
	restaurant.addRating("Pizza", 4);
    restaurant.addRating("Salad", 2);
     
	double averageRating = restaurant.getAverageRating("Burger");
    System.out.println("\nAverage rating for Burger: " + averageRating); 	 
	averageRating = restaurant.getAverageRating("Pizza");
    System.out.println("Average rating for Pizza: " + averageRating);  	 
	averageRating = restaurant.getAverageRating("Salad");
    System.out.println("Average rating for Salad: " + averageRating); 	 	 
    System.out.println("Average rating: " + restaurant.calculateAverageRating());
    System.out.println("\nRemove 'Pizza' from the above menu.");
    restaurant.removeItem("Pizza");
    System.out.println("\nUpdated menu:");
    restaurant.displayMenu();
  }
}

The Main class contains the main function that creates an object of the Restaurant class and calls its methods to add, remove, and display menu items, as well as add ratings and calculate average ratings for those items.

Sample Output:

Menu:
Burger: $8.99
Pizza: $10.99
Salad: $6.0
Average rating: 4.666666666666667
Remove 'Pizza' from the above menu.

Updated menu:
Burger: $8.99
Salad: $6.0

Flowchart:

Flowchart: Java  OOP Exercises: Restaurant menu, average rating.
Flowchart: Java  OOP Exercises: Restaurant menu, average rating.
Flowchart: Java  OOP Exercises: Restaurant menu, average rating.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Movie and Review.
Java OOP Next: Search, book, cancel hotel and flight reservations.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.