3052, Swayam Java Ass2
3052, Swayam Java Ass2
Moradiya
Roll No: 3052
Div: A
Java Assignment-2
Q1.
package com.cafe;
import java.util.ArrayList;
import java.util.Scanner;
interface CoffeeOrder {
void selectCoffee(String coffeeType);
void selectSize(String size);
void displayOrder();
int getPrice();
}
class CafeOrder implements CoffeeOrder {
String coffeeType;
double price;
ArrayList<ArrayList<Object>> Orders = new ArrayList<ArrayList<Object>>();
if(size=="small")
ind.add(this.price/2);
if(size=="medium")
ind.add(this.price/1.5);
if(size=="large")
ind.add(this.price);
Orders.add(ind);
}
class Que1 {
static CafeOrder C = new CafeOrder();
static Scanner S = new Scanner(System.in);
switch(choice)
{
case 1:
C.selectCoffee("Latte");
chooseSize();
break;
case 2:
C.selectCoffee("Cappuccino");
chooseSize();
break;
case 3:
C.selectCoffee("Caffè mocha");
chooseSize();
break;
case 4:
C.selectCoffee("Espresso");
chooseSize();
break;
case 5:
C.selectCoffee("Frappe");
chooseSize();
break;
case 6:
C.selectCoffee("Americano");
chooseSize();
break;
default:
return;
}
}
}
switch(choice)
{
case 1:
chooseType();
break;
case 2:
try {
C.Orders.get(0);
C.displayOrder();
return;
}
catch(Exception e) {
System.out.println("***Please! Place Order First***");
}
break;
default:
return;
}
}
}
}
Output:
Q2.
package com.kiosk;
import java.util.Scanner;
interface DenominationHandler
{
void FiveHundredHandler(long amount);
void OneHundredHandler(long amount);
void TenHandler(long amount);
void FiveHandler(long amount);
}
int fiveHundred = 0;
int oneHundred = 0;
int ten = 0;
int five = 0;
Kiosk() {
}
K.FiveHundredHandler(payAmount);
}
public void FiveHundredHandler(long amount) {
while(amount>=500)
{
amount -= 500;
fiveHundred++;
}
K.OneHundredHandler(amount);
}
public void OneHundredHandler(long amount) {
while(amount>=100)
{
amount -= 100;
oneHundred++;
}
K.TenHandler(amount);
}
public void TenHandler(long amount) {
while(amount>=10)
{
amount -= 10;
ten++;
}
K.FiveHandler(amount);
}
public void FiveHandler(long amount) {
while(amount>=5)
{
amount -= 5;
five++;
}
this.rem = (int)amount;
K.displayDetails();
}
public void displayDetails() {
System.out.println("\n\nYour Bill Details...\n------------------------------------");
System.out.println("Customer Number: "+custNo);
System.out.printf("\n500 * %d : Rs.%d",fiveHundred,fiveHundred*500);
System.out.printf("\n100 * %d : Rs.%d",oneHundred,oneHundred*100);
System.out.printf("\n10 * %d : Rs.%d",ten,ten*10);
System.out.printf("\n5 * %d : Rs.%d",five,five*5);
System.out.println("\nRemaining Amount: Rs."+rem);
System.out.println("\nTotal Payable Amount: Rs."+payAmount+"\n");
}
}
class Que2 {
public static void main(String[] args)
{
Scanner S = new Scanner(System.in);
System.out.print("\nEnter Customer Number: ");
long custNo = S.nextLong();
System.out.print("Enter Bill Amount: ");
long amount = S.nextLong();
Output:
Q3.
package com.bank;
import java.util.Scanner;
class BankingException extends Exception {
public BankingException(String str) {
super(str);
}
}
interface BankingOperations {
double getBalance();
}
long getAccNo() {
return this.accNo;
}
@Override
public double getBalance() {
return this.balance;
}
@Override
public void deposit(double amount) throws Exception {
if(amount > 0) {
this.balance += amount;
System.out.printf("\n***Rs.%.2f Deposited Successfully***\n",amount);
System.out.println("Balance: Rs."+this.balance);
}
else {
throw new BankingException("***Deposit Amount is invalid***");
}
}
@Override
public void withdraw(double amount) throws Exception {
if(amount <= 0) {
throw new BankingException("***Withdrawal Amount must be greater
than 0***");
}
if(amount>this.balance) {
throw new BankingException("***Insufficient Balance***");
}
else {
this.balance -= amount;
System.out.printf("\n***Rs.%.2f Withdraw Successfully***\n",amount);
System.out.println("Balance: Rs."+this.balance);
}
}
}
while(true) {
System.out.println("\nChoices...");
System.out.println("1) Create Account");
System.out.println("2) Deposit Money");
System.out.println("3) Withdraw Money");
System.out.println("4) Retrieve Information");
System.out.println("Other to Exit");
switch(choice) {
case 1:
if(index<15) {
accNo = (int)(Math.random()*111111111);
for(int i=0; i<index; i++) {
if(B[i].getAccNo() == accNo) {
accNo = (int)(Math.random()*111111111);
}
}
B[index] = new BankingSystem(accNo, 0);
index++;
}
else {
System.out.println("***You exceeded the maximum limit of
account creation. So, You can't Create the Account***");
}
break;
case 2:
System.out.print("\nEnter the Account Number: ");
accNo = S.nextLong();
try {
B[i].deposit(amount);
}
catch(Exception e)
{
System.out.println("\n"+e.getMessage());
}
checkAcc = true;
}
}
if(!checkAcc) {
System.out.println("***Acount Number: "+accNo+" is not
exist***");
}
checkAcc = false;
break;
case 3:
System.out.print("\nEnter the Account Number: ");
accNo = S.nextLong();
try {
B[i].withdraw(amount);
}
catch(Exception e)
{
System.out.println("\n"+e.getMessage());
}
checkAcc = true;
}
}
if(!checkAcc) {
System.out.println("***Acount Number: "+accNo+" is not
exist***");
}
checkAcc = false;
break;
case 4:
System.out.print("\nEnter the Account Number: ");
accNo = S.nextLong();
default:
return;
}
}
}
}
Output:
Q4.
package hospital;
import hospital.Doctor;
abstract class Doctor {
public String name, specialization;
class Appointment {
private Doctor DS;
Appointment(Doctor DS) {
this.DS = DS;
}
void schedule() {
System.out.println("\nName: "+DS.getName());
System.out.println("Specialization: "+DS.getSpecialization());
DS.bookAppointment();
}
}
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
class BankAccount {
private String accountHolder;
private double balance;
List<String> transactions = new ArrayList<String>();
BankAccount(String accountHolder) {
this.accountHolder = accountHolder;
this.balance = 0;
}
void deposit(double amount) {
if(amount > 0) {
this.balance += amount;
System.out.printf("***Rs.%.2f is Deposited Successfully***\n",amount);
transactions.add("Deposited Rs."+amount+"\tBalance:
Rs."+this.balance);
}
else {
System.out.println("***Deposit amount must be greater than 0***\n");
}
}
void printMiniStatement() {
System.out.println("\n\nAccount Holder Name: "+this.accountHolder);
try {
transactions.get(0);
System.out.println("\n\tTransaction History");
System.out.println("-------------------------------------------");
for(int i=0; i<transactions.size(); i++) {
System.out.printf(transactions.get(i)+"\n");
}
}
catch(Exception e) {
System.out.println("\n***No Transaction History***");
}
}
double getBalance() {
return this.balance;
}
}
class Que5 {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.print("\nEnter Account Holder Name: ");
String name = S.nextLine();
double amount;
while(true) {
System.out.println("\nChoices...");
System.out.println("1) Deposit Money");
System.out.println("2) Withdraw Money");
System.out.println("3) Check Balance");
System.out.println("4) Print Mini Statement");
System.out.println("-- Other to Exit");
System.out.print("\nEnter your Choice: ");
int choice = S.nextInt();
switch(choice) {
case 1:
System.out.print("\nEnter the Amount to Deposit: ");
amount = S.nextDouble();
B.deposit(amount);
break;
case 2:
System.out.print("\nEnter the Amount to Withdraw: ");
amount = S.nextDouble();
B.withdraw(amount);
break;
case 3:
System.out.println("\nBalance: Rs."+B.getBalance()+"\n");
break;
case 4:
B.printMiniStatement();
break;
default:
return;
}
}
}
}
Output:
Q6.
package propertymanagement;
interface Property {
void buy();
void sell();
}
// package propertymanagement;
class Apartments implements Property {
String location;
double price;
class Que6 {
public static void main(String[] args) {
Property P[] = new Property[6];
P[0] = new Apartments("River View, Surat",400000);
P[1] = new Bungalow("Green Park, Surat", 2500000);
P[2] = new Tenaments("New Kanal Heights, Chandkheda", 4090000);
class Flour {
private double weight, price;
Flour(double ...args) {
this.weight = args[0];
this.price = args[1];
}
double getWeight() {
return this.weight;
}
double getPrice() {
return this.price;
}
double calcPrice() {
return weight*price;
}
}
class FlourItem {
Flour defaultFlour() {
return new Flour(1, 50);
}
}
interface FlourItemInterface {
Flour getQuintal();
Flour get10kg();
Flour get1kg();
}
class Que7 {
public static void main(String[] args) {
FlourItemInterface FS = new FlourStore();
package library;
import java.util.Scanner;
import java.util.ArrayList;
AbstractBook(String ...a) {
this.title = a[0];
this.author = a[1];
this.bookType = a[2];
this.isLent = false;
}
String getTitle() {
return this.title;
}
String getAuthor() {
return this.author;
}
abstract String getBookType();
String getBookType() {
return this.bookType;
}
}
class Library {
ArrayList<Book> B = new ArrayList<Book>();
Library() {
B.add(new Book("Core Java","Cay S. Horstman","Learning"));
B.add(new Book("Clean Code","Robert Cecil Martin","Learning"));
B.add(new Book("Let us C","Yashvant Kanetar","Learning"));
B.add(new Book("The C++","Bjarne Stroustrup","Learning"));
B.add(new Book("Eloquent JavaScript","Marijn Haverbeke","Learning"));
B.add(new Book("HTML & CSS: Design and Build Web Sites","Jon
Duckett","Learning"));
B.add(new Book("React and React Native","Adam Boduch","Learning"));
}
void addBook(String ...a) {
B.add(new Book(a[0], a[1], a[2]));
System.out.println("\n***Book added Succefully***");
System.out.println(B.get(B.size()-1).toString());
}
void printAvBook() {
int count = 0;
for(int i=0; i<B.size(); i++) {
if(!B.get(i).isLent) {
count++;
break;
}
}
if(count > 0) {
System.out.println("\nAvailable Books");
System.out.println("__________________________________________
_____________");
for(int i=0; i<B.size(); i++) {
if(!B.get(i).isLent) {
System.out.println((i+1)+") "+B.get(i).toString());
System.out.println("-----------------------------------------------");
count++;
}
}
}
else {
System.out.println("***All Books are Lended Now***");
}
}
void printNotAvBook() {
int count = 0;
class Que8 {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
while(true) {
System.out.println("\n\nChoices....");
System.out.println("1) Add Book in Library");
System.out.println("2) Lend Book");
System.out.println("3) Return Book");
System.out.println("4) List All Available Books");
System.out.println("5) List All lended Books");
System.out.println("-- Other to Exit");
switch(choice) {
case 1:
Scanner S1 = new Scanner(System.in);
System.out.print("Enter Title of the Book: ");
title = S1.nextLine();
case 2:
L.printAvBook();
System.out.print("\nEnter the Number of the Book, Which you want
to Lend: ");
num = S.nextInt();
L.lendBook(num);
break;
case 3:
L.printNotAvBook();
System.out.print("\nEnter the Number of the Book, Which you want
to Return: ");
num = S.nextInt();
L.returnBook(num);
break;
case 4:
L.printAvBook();
break;
case 5:
L.printNotAvBook();
break;
default:
return;
}
}
}
}
Output: