0% found this document useful (0 votes)
2 views

Assignment Report (1)

The document outlines two tasks: a Restaurant Ordering System and a Savings Account Calculator, both implemented in Java. The Restaurant Ordering System allows users to view a menu, select dishes, and generate an invoice based on their orders, while the Savings Account Calculator enables users to input their annual interest rate, starting balance, and monthly transactions to calculate their final balance and interest earned. Each task includes detailed code snippets and step-by-step explanations of the user interactions and functionalities.

Uploaded by

rkadhikari4341
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment Report (1)

The document outlines two tasks: a Restaurant Ordering System and a Savings Account Calculator, both implemented in Java. The Restaurant Ordering System allows users to view a menu, select dishes, and generate an invoice based on their orders, while the Savings Account Calculator enables users to input their annual interest rate, starting balance, and monthly transactions to calculate their final balance and interest earned. Each task includes detailed code snippets and step-by-step explanations of the user interactions and functionalities.

Uploaded by

rkadhikari4341
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Assignment Report

Task 1: Take-Away Menu


1. Java Code

import java.util.InputMismatchException;

import java.util.Scanner;

public class RestaurantOrderingSystem {

// Menu data as arrays (Category, Dish, Description, Price)

static String[][] menu = {

{"Appetisers", "Spring Rolls", "Crispy rolls filled with vegetables and


sweet chili sauce.", "6.00"},

{"Appetisers", "Thai Dumplings", "Steamed dumplings with chicken


and vegetable filling.", "7.50"},

{"Main Dishes", "Pad Thai", "Stir-fried rice noodles with shrimp, tofu,
peanuts, and bean sprouts.", "12.00"},

{"Main Dishes", "Basil Chicken", "Stir-fried chicken with Thai basil,


chili, and vegetables, served with jasmine rice.", "12.00"},

{"Fried Rice", "Thai Fried Rice", "Fried rice with chicken, shrimp, or
vegetables, seasoned with Thai spices.", "11.00"},

{"Fried Rice", "Pineapple Fried Rice", "Fried rice with pineapple,


cashews, and choice of meat or tofu.", "12.50"}

};

static double totalBill = 0; // Store the total bill


static StringBuilder invoice = new StringBuilder(); // To generate invoice

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

boolean ordering = true; // Control the ordering process

while (ordering) {

// Prompt the user with options

int option = getValidIntegerInput(scanner, "Given Option:\n1. Show


Menu\n2. Exit\nchoose an option: ");

if (option == 2) {

break; // Exit the program if user chooses option 2

} else if (option == 1) {

// Display categories

boolean continueOrdering = true;

while (continueOrdering) {

System.out.println("Categories:");

System.out.println("1. Appetisers");

System.out.println("2. Main Dishes");

System.out.println("3. Fried Rice");

int categoryChoice = getValidIntegerInput(scanner, "Choose a


category: ");
String selectedCategory = "";

switch (categoryChoice) {

case 1: selectedCategory = "Appetisers"; break;

case 2: selectedCategory = "Main Dishes"; break;

case 3: selectedCategory = "Fried Rice"; break;

default:

System.out.println("Invalid category.");

continue;

// Display dishes in the selected category in a simple table


format

System.out.println("Dishes in " + selectedCategory + ":");

System.out.printf("%-4s %-15s %-70s %-6s\n", "No.", "Dish


Name", "Description", "Price");

System.out.println("..........................................................................................
.........................................");

int dishNumber = 1;

for (int i = 0; i < menu.length; i++) {

if (menu[i][0].equals(selectedCategory)) {

System.out.printf("%-4d %-15s %-70s $%-5s\n",


dishNumber, menu[i][1], menu[i][2], menu[i][3]);
dishNumber++;

// Ask the user to choose a dish by number

int dishChoice = getValidIntegerInput(scanner, "Enter the


number of the dish you want to order: ");

int selectedDishIndex = -1;

dishNumber = 1;

// Find the index of the selected dish in the menu

for (int i = 0; i < menu.length; i++) {

if (menu[i][0].equals(selectedCategory)) {

if (dishNumber == dishChoice) {

selectedDishIndex = i;

break;

dishNumber++;

if (selectedDishIndex == -1) {
System.out.println("Invalid dish selection.");

continue; // Skip to the next iteration if dish is invalid

// Get the price of the selected dish

double dishPrice =
Double.parseDouble(menu[selectedDishIndex][3]);

// Ask for the quantity and validate it

int quantity = getValidIntegerInput(scanner, "Enter the quantity:


");

while (quantity <= 0) {

System.out.println("Invalid quantity. Please enter a positive


number.");

quantity = getValidIntegerInput(scanner, "Enter the quantity:


");

// Calculate subtotal and update the total bill

double subtotal = dishPrice * quantity;

totalBill += subtotal;

// Add to the invoice in a simple table format


invoice.append(String.format("%-20s %-10d $%-10.2f\n",
menu[selectedDishIndex][1], quantity, subtotal));

// Ask if the user wants to continue ordering

System.out.println("Would you like to continue ordering?


(yes/no)");

scanner.nextLine(); // Consume newline

String continueResponse = scanner.nextLine();

if (continueResponse.equalsIgnoreCase("no")) {

continueOrdering = false;

ordering = false; // End the overall ordering process

// Display the final invoice in a simple table format

if (totalBill > 0) {

System.out.println("\nINVOICE:");

System.out.printf("%-20s %-10s %-10s\n", "Dish", "Quantity",


"Subtotal");

System.out.println(".....................................................");

System.out.println(invoice.toString());
System.out.printf("%-20s %-10s $%-10.2f\n", "Total Bill", "", totalBill);

} else {

System.out.println("No items were ordered.");

// Thank the user and close the scanner

System.out.println("Thank you for your order!");

scanner.close();

// Method to handle integer input validation

public static int getValidIntegerInput(Scanner scanner, String prompt) {

int input = -1;

boolean valid = false;

while (!valid) {

System.out.print(prompt);

if (scanner.hasNextInt()) {

input = scanner.nextInt();

valid = true;

} else {

System.out.println("Invalid input. Please enter a valid number.");

scanner.next(); // Consume the invalid input


}

return input;

}
2. Screenshots
a) Giving two options to user when system is open which is Show Menu and Exit.

b) If user choose option 2 which is exit then output will be shown as below
c) If user choose option 1 which is Show Menu
d) If user choose one of the category suppose user choose appetisers then output shows like
below

e) Then user will choose one dish from the categories.


f) User will get options to continue order or not, if user choose no then invoices will be
displayed as below

g) If user choose order again then category menu will be displayed


h) User will choose a dish and quantity from that category and proceed to create a bill for
order

3. Step-by-Step Explanation
User Prompt:
Initially, the program will display the option of either showing the menu or exiting the program.

Menu Categories:
When the user selects "Show Menu," the program displays the available categories: Appetisers,
Main dishes and Fried rice.

Dish Selection:
Within the selected category, the dishes are numbered. There are meal options for the user to
choose from, and the user selects a dish by choosing the number of the dish.
Order Processing:
If the user chooses a particular dish, they are asked to input the number of portions they want for
that specific dish. The program checks such an input to guarantee that it is positive.

Invoice Generation:
This means that after the user is through with ordering, the program provides an invoice for all
the ordered items, the quantity of each item, and the subtotal. The net amount of the bill is also
shown.
Task 2: Calculator
1. Java Code

//Calculator.java

Import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input for annual interest rate, starting balance, and number of months

System.out.print("Enter the annual interest rate (in %): ");

double annualInterestRate = scanner.nextDouble();

System.out.print("Enter the starting balance: ");

double startingBalance = scanner.nextDouble();

System.out.print("Enter the number of months: ");

int numberOfMonths = scanner.nextInt();

// Create a SavingsAccount object

SavingsAccount account = new SavingsAccount(startingBalance, annualInterestRate);


// Process each month's transactions

for (int month = 1; month <= numberOfMonths; month++) {

System.out.print("Enter the amount deposited in month " + month + ": ");

double depositAmount = scanner.nextDouble();

account.deposit(depositAmount);

System.out.print("Enter the amount withdrawn in month " + month + ": ");

double withdrawalAmount = scanner.nextDouble();

account.withdraw(withdrawalAmount);

account.addMonthlyInterest();

// Display the results

System.out.printf("Ending balance: $%.2f%n", account.getBalance());

System.out.printf("Total deposits: $%.2f%n", account.getTotalDeposits());

System.out.printf("Total withdrawals: $%.2f%n", account.getTotalWithdrawals());

System.out.printf("Total interest earned: $%.2f%n", account.getTotalInterestEarned());

scanner.close();

}
// File: SavingsAccount.java

public class SavingsAccount {

private double annualInterestRate;

private double balance;

private double totalDeposits;

private double totalWithdrawals;

private double totalInterestEarned;

// Constructor to initialize the savings account with the starting balance

public SavingsAccount(double startingBalance, double annualInterestRate) {

this.balance = startingBalance;

this.annualInterestRate = annualInterestRate;

this.totalDeposits = 0;

this.totalWithdrawals = 0;

this.totalInterestEarned = 0;

// Method to deposit amount into the account

public void deposit(double amount) {

balance += amount;

totalDeposits += amount;

}
// Method to withdraw amount from the account

public void withdraw(double amount) {

balance -= amount;

totalWithdrawals += amount;

// Method to add monthly interest to the balance

public void addMonthlyInterest() {

double monthlyInterestRate = annualInterestRate / 12 / 100;

double monthlyInterest = balance * monthlyInterestRate;

balance += monthlyInterest;

totalInterestEarned += monthlyInterest;

// Getter methods

public double getBalance() {

return balance;

public double getTotalDeposits() {

return totalDeposits;

}
public double getTotalWithdrawals() {

return totalWithdrawals;

public double getTotalInterestEarned() {

return totalInterestEarned;

}
2. ScreenShots
Sample output

3. Step-by-step Explanation
User Input:
This involves the user inputting the annual interest rate, the starting balance, and the number of
months.

Monthly Transactions:
For each month, the user enters the deposit/withdrawal amount. With the present program, the
account balance may be updated, and the monthly interest may be applied.

Interest Calculation:
Monthly interest is calculated using the formula: Monthly interest is calculated using the
formula:

Monthly Interest = Current Balance × (Annual Interest Rate /12 ×100)


Results Display:
The last three figures are available to the user as the final balance, the total deposit, the total
withdrawal and the total interest earned.

4.
Task 2: Calculator

1. User Input:
o The program collects the annual interest rate, starting balance, and number of
months from the user.
2. Monthly Transactions:
o For each month, the user inputs deposit and withdrawal amounts. The program
updates the account balance and applies monthly interest.
3. Interest Calculation:
o Monthly interest is calculated using the formula:
Monthly Interest=Current Balance×(Annual Interest Rate12×100)\text{Monthly
Interest} = \text{Current Balance} \times \left(\frac{\text{Annual Interest Rate}}
{12 \times 100}\
right)Monthly Interest=Current Balance×(12×100Annual Interest Rate)
4. Results Display:
o The final balance, total deposits, total withdrawals, and total interest earned are
displayed to the user.

You might also like