0% found this document useful (0 votes)
22 views16 pages

A2 Oop

The document contains Java source code for a restaurant management system that defines classes like MenuItem, FoodItem, DrinkItem and a Restaurant class to manage the menu and orders. It demonstrates adding items to the menu, displaying the menu to take orders and calculating the total price.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views16 pages

A2 Oop

The document contains Java source code for a restaurant management system that defines classes like MenuItem, FoodItem, DrinkItem and a Restaurant class to manage the menu and orders. It demonstrates adding items to the menu, displaying the menu to take orders and calculating the total price.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Resturent.

java

Source code.

import java.util.ArrayList;
import java.util.Scanner;

// Base class for menu items


class MenuItem {
protected String name;
protected double price;

public MenuItem(String name, double price) {


this.name = name;
this.price = price;
}

public String getName() {


return name;
}

public double getPrice() {


return price;
}
}

// Subclass representing food items


class FoodItem extends MenuItem {
public FoodItem(String name, double price) {
super(name, price);
}
}

// Subclass representing drink items


class DrinkItem extends MenuItem {
public DrinkItem(String name, double price) {
super(name, price);
}
}

// Restaurant class to manage the menu and order


public class Restaurant {
private ArrayList<MenuItem> menu;

public Restaurant() {
menu = new ArrayList<>();
}

// Add a menu item to the menu


public void addItem(MenuItem item) {
menu.add(item);
}

// Display the menu


public void displayMenu() {
System.out.println("Menu:");
for (MenuItem item : menu) {
System.out.println(item.getName() + " - $" + item.getPrice());
}
}
// Take order from the user
public double takeOrder() {
Scanner scanner = new Scanner(System.in);
double totalPrice = 0.0;
String choice;

System.out.println("Enter item name to add to your order (type 'done' to finish):");


while (true) {
choice = scanner.nextLine();
if (choice.equalsIgnoreCase("done")) {
break;
}
boolean found = false;
for (MenuItem item : menu) {
if (item.getName().equalsIgnoreCase(choice)) {
totalPrice += item.getPrice();
found = true;
break;
}
}
if (!found) {
System.out.println("Item not found in the menu. Please choose again.");
}
}
return totalPrice;
}

public static void main(String[] args) {


// Create restaurant object
Restaurant restaurant = new Restaurant();

// Add some menu items


restaurant.addItem(new FoodItem("Burger", 5.99));
restaurant.addItem(new FoodItem("Pizza", 8.99));
restaurant.addItem(new DrinkItem("Coke", 1.99));
restaurant.addItem(new DrinkItem("Coffee", 2.49));

// Display the menu


restaurant.displayMenu();

// Take order and calculate total price


double totalPrice = restaurant.takeOrder();
System.out.println("Total Price: $" + totalPrice);
}
}

Output
UITS.java

Source code.
import java.util.Scanner;

class UITS {
Scanner scanner = new Scanner(System.in);

void displayWelcomeMessage() {
System.out.println("Welcome to University of Information Technology and Sciences
Portal!!!");
}

void displayMenu() {
System.out.println("Select one of the following streams in which you want to study");
System.out.println("1. Computer Science");
System.out.println("2. Information Technology");
System.out.println("3. Electronics and Communication");
System.out.println("4. Electrical and Electronics");
System.out.println("5. Mechanical");
System.out.println("10. Exit");
System.out.println("Enter the choice number from above");
}

void displayCourseOutline(String stream) {


System.out.println("You have chosen " + stream + ".");
System.out.println("You will get the following subjects:");

switch (stream) {
case "Computer Science":
System.out.println("DSA");
System.out.println("DBMS");
System.out.println("Computer Networks (CN)");
break;
case "Information Technology":
System.out.println("MPMC (Multi processor and Multi Controller)");
System.out.println("DBMS");
System.out.println("DSA");
System.out.println("Information Security");
break;
case "Electronics and Communication":
System.out.println("Wireless Communication");
System.out.println("Switching Theory and Logic Design (STLD)");
System.out.println("Mobile Systems Communication");
break;
case "Electrical and Electronics":
System.out.println("Switching Theory and Logic Design (STLD)");
System.out.println("Control Systems");
System.out.println("Electrical Circuits and Designs");
break;
case "Mechanical":
System.out.println("Force and Friction");
System.out.println("Motors and its Types");
break;
default:
System.out.println("Invalid choice!");
}
}

void lockStream(String stream) {


System.out.println("Are you sure you want to lock this stream? Choose either A or B.");
System.out.println("A. Yes, I'm sure");
System.out.println("B. No, I'm not sure");
String choice = scanner.nextLine();

if (choice.equalsIgnoreCase("A")) {
System.out.println("Congratulations!! You are now a " + stream + " student at UITS");
} else if (choice.equalsIgnoreCase("B")) {
System.out.println("Stream selection canceled.");
} else {
System.out.println("Invalid choice! Stream selection canceled.");
}
}

void start() {
displayWelcomeMessage();
while (true) {
displayMenu();
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline

if (choice == 10) {
System.out.println("Exiting...");
break;
}

switch (choice) {
case 1:
displayCourseOutline("Computer Science");
lockStream("CSE");
break;
case 2:
displayCourseOutline("Information Technology");
lockStream("IT");
break;
case 3:
displayCourseOutline("Electronics and Communication");
lockStream("ECE");
break;
case 4:
displayCourseOutline("Electrical and Electronics");
lockStream("EE");
break;
case 5:
displayCourseOutline("Mechanical");
lockStream("Mechanical");
break;
default:
System.out.println("Invalid choice!");
}
}
}
}

public class Main {


public static void main(String[] args) {
UITS uits = new UITS();
uits.start();
}
}

Output
BankManagement.java
Source code.

import java.util.Scanner;

// Account class (parent class)


class Account {
protected String customerName;
protected int accountNumber;
protected String accountType;
protected double balance;

public Account(String customerName, int accountNumber, String accountType) {


this.customerName = customerName;
this.accountNumber = accountNumber;
this.accountType = accountType;
this.balance = 50000.0;
}

public void deposit(double amount) {


balance += amount;
System.out.println("Deposit successful.");
}

public void withdraw(double amount) {


if (amount > balance) {
System.out.println("Insufficient funds.");
} else {
balance -= amount;
System.out.println("Withdrawal successful.");
}
}

public void displayBalance() {


System.out.println("Balance: " + balance);
}

public void displayDetails() {


System.out.println("Customer Name: " + customerName);
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Type: " + accountType);
System.out.println("Balance: " + balance);
}
}

// SavingAccount class (child class)


class SavingAccount extends Account {
public SavingAccount(String customerName, int accountNumber) {
super(customerName, accountNumber, "Saving");
}

@Override
public void deposit(double amount) {
super.deposit(amount);
// Adding 2% interest on deposit
balance += (amount * 0.02);
}
}

// CurrentAccount class (child class)


class CurrentAccount extends Account {
public CurrentAccount(String customerName, int accountNumber) {
super(customerName, accountNumber, "Current");
}
}

public class BankManagementSystem {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter S for saving customer and C for current a/c customer: ");
String accountTypeInput = scanner.nextLine().toUpperCase();

if (accountTypeInput.equals("S")) {
System.out.print("Enter Customer Name: ");
String customerName = scanner.nextLine();

System.out.print("Enter Account Number: ");


int accountNumber = scanner.nextInt();

SavingAccount savingAccount = new SavingAccount(customerName, accountNumber);


handleAccountOperations(savingAccount, scanner);
} else if (accountTypeInput.equals("C")) {
System.out.print("Enter Customer Name: ");
String customerName = scanner.nextLine();

System.out.print("Enter Account Number: ");


int accountNumber = scanner.nextInt();

CurrentAccount currentAccount = new CurrentAccount(customerName,


accountNumber);
handleAccountOperations(currentAccount, scanner);
} else {
System.out.println("Invalid input. Please enter 'S' for saving account or 'C' for current
account.");
}
System.out.println("Thank You for Banking with us..");
}

public static void handleAccountOperations(Account account, Scanner scanner) {


while (true) {
System.out.println("Choose Your Choice");
System.out.println("1) Deposit");
System.out.println("2) Withdraw");
System.out.println("3) Display Balance");
System.out.println("4) Display with Full Details");
System.out.println("5) Exit");

System.out.print("Enter Your choice: ");


int choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.print("Enter amount to Deposit: ");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
break;
case 2:
System.out.print("Enter amount to Withdraw: ");
double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
break;
case 3:
account.displayBalance();
break;
case 4:
account.displayDetails();
break;
case 5:
return;
default:
System.out.println("Invalid choice.");
}
}
}
}

Output

You might also like