Matlab Code

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

MATLAB CODE

%Clear everything

clc

clear all

%Variables

bkno=1;

%predefined books

%Syntax: var(<array_position>).value_name = value;

book(1).name='Harry potter';

book(1).author='JK Rolling';

book(1).quantity=3;

book(1).rating=4.5;

book(2).name='50 Shades of grey';

book(2).author='E. L. James';

book(2).quantity=5;

book(2).rating=5;
book(3).name='The Great Gatsby';

book(3).author='F. Scott Fitzgerald';

book(3).quantity=3;

book(3).rating=4;

book(4).name='The Great Gatsby 2';

book(4).author='F. Scott Fitzgerald';

book(4).quantity=7;

book(4).rating=4;

book(5).name='Outbreak';

book(5).author='Robin Cook';

book(5).quantity=7;

book(5).rating=5;

book(6).name='The Dream Merchants';

book(6).author='Harold Robbins';

book(6).quantity=2;

book(6).rating=3;

book(7).name='The Pelican Brief';

book(7).author='John Grisham';
book(7).quantity=10;

book(7).rating=4;

book(8).name='Shall We Tell The President';

book(8).author='Jeffery Archer';

book(8).quantity=10;

book(8).rating=4.5;

book(9).name='The Bourne Identity';

book(9).author='Robert Ludlum';

book(9).quantity=6;

book(9).rating=4.5;

book(10).name='Windmills of the Gods';

book(10).author='Sidney Sheldon';

book(10).quantity=4;

book(10).rating=3;

book(11).name='Pride and Prejudice';

book(11).author='Jane Austen';

book(11).quantity=3;

book(11).rating=4;
book(12).name='Edge of Eternity';

book(12).author='Ken Follet';

book(12).quantity=7;

book(12).rating=4;

book(13).name='Death on the Nile';

book(13).author='Agatha Christie';

book(13).quantity=13;

book(13).rating=4.5;

book(14).name='David Copperfield';

book(14).author='Charles Dickens';

book(14).quantity=6;

book(14).rating=4;

book(15).name='Wuthering Heights';

book(15).author='Emily Bronte';

book(15).quantity=2;

book(15).rating=4;

book(16).name='War and Peace';


book(16).author='Leo Tolstoy';

book(16).quantity=8;

book(16).rating=4.5;

book(17).name='Congo';

book(17).author='Michael Crichton';

book(17).quantity=2;

book(17).rating=3;

book(18).name='Life on the Mississippi';

book(18).author='Mark Twain';

book(18).quantity=6;

book(18).rating=3.5;

book(19).name='The Happy Prince and Other Tales';

book(19).author='Oscar Wilde';

book(19).quantity=12;

book(19).rating=4;

book(20).name='The Emperor of all Maladies';

book(20).author='Siddhartha Mukerjee';

book(20).quantity=5;
book(20).rating=4;

book(21).name='A Child of the Century';

book(21).author='Ben Hecht';

book(21).quantity=2;

book(21).rating=4;

book(22).name='A Theory of Justice';

book(22).author='John Rawls';

book(22).quantity=4;

book(22).rating=4;

book(23).name='The Symposium';

book(23).author='Plato';

book(23).quantity=3;

book(23).rating=3.5;

book(24).name='The Diary of a Young Girl';

book(24).author='Anne Frank';

book(24).quantity=7;

book(24).rating=4;
book(25).name='Innocents Abroad';

book(25).author='Mark Twain';

book(25).quantity=9;

book(25).rating=4;

book(26).name='Confessions';

book(26).author='Jean Jacques Rousseau';

book(26).quantity=6;

book(26).rating=4;

book(27).name='The Selfish Gene';

book(27).author='Richard Dawkins';

book(27).quantity=5;

book(27).rating=4.5;

book(28).name='A Time of Gifts';

book(28).author='Patrick Leigh Fermor';

book(28).quantity=1;

book(28).rating=3;

book(29).name='Venice';

book(29).author='Jan Morris';
book(29).quantity=1;

book(29).rating=3.5;

book(30).name='A Brief History of Time';

book(30).author='Stephen Hawking';

book(30).quantity=5;

book(30).rating=4.5;

book(31).name='The Perks OF Being A Wallflower';

book(31).author='Stephen Chbosky';

book(31).quantity='3';

book(31).rating=4;

book(32).name='The Maz Runner';

book(32).author='James Dashner';

book(32).quantity='3';

book(32).rating=4;

book(33).name='The Hunger Games';

book(33).author='Suzanne Collins';

book(33).quantity='3';

book(33).rating=4;
book(34).name='Divergent';

book(34).author='Veronica Roth';

book(34).quantity='3';

book(34).rating=4;

book(3).name='The Maz Runner';

book(32).author='James Dashner';

book32).quantity='3';

book(32).rating=4;

book(33).name='The Hunger Games';

book(33).author='Suzanne Collins';

book(33).quantity='3';

book(33).rating=4;

book(34).name='Divergent';

book(34).author='Veronica Roth';

book(34).quantity='3';

book(34).rating=4;
book(35).name='The fault in our stars';

book(35).author='John Green';

book(35).quantity='3';

book(35).rating=4;

book(36).name='To kill a mocking bird';

book(36).author='Harper Lee';

book(36).quantity='3';

book(36).rating=4;

book(37).name='The book theif';

book(37).author='Marcus Zusak';

book(37).quantity='3';

book(37).rating=4;

book(38).name='Thirteen reasons why';

book(38).author='Jay Asher';

book(38).quantity='3';

book(38).rating=4;

book(39).name='If i stay';

book(39).author='Gayle Forman';
book(39).quantity='3';

book(39).rating=4;

book(40).name='Twilight';

book(40).author='Stephanie Meyer';

book(40).quantity='3';

book(40).rating=4;

book(41).name='The lord of the rings';

book(41).author='J R R Tolkin';

book(41).quantity='3';

book(41).rating=4;

book(42).name='American naturalist';

book(42).author='Journals';

book(42).quantity='3';

book(42).rating=4;

book(43).name='Biological reviews';

book(43).author='Journals';

book(43).quantity='3';

book(43).rating=4;
book(44).name='Nature';

book(44).author='Journals';

book(44).quantity='3';

book(44).rating=4;

book(45).name='Artificial intellegence';

book(45).author='Journals';

book(45).quantity='3';

book(45).rating=4;

book(46).name='The earth interactions';

book(46).author='Journals';

book(46).quantity='3';

book(46).rating=4;

book(47).name='A british medical journal';

book(47).author='Journals';

book(47).quantity='3';

book(47).rating=4;

book(48).name='Journal of genetics';
book(48).author='Journals';

book(48).quantity='3';

book(48).rating=4;

book(49).name='Journal of molecular biology';

book(49).author='Journals';

book(49).quantity='3';

book(49).rating=4;

book(50).name='Journal of animal sciences';

book(50).author='Journals';

book(50).quantity='3';

book(50).rating=4;

book(51).name='Journal of neuro chemistry';

book(51).author='Journals';

book(51).quantity='3';

book(51).rating=4;

book(52).name='Vogue';

book(52).author='Newspaper';

book(52).quantity='3';
book(52).rating=4;

book(53).name='Verve';

book(53).author='Newspaper';

book(53).quantity='3';

book(53).rating=4;

book(54).name='Open';

book(54).author='Newspaper';

book(54).quantity='3';

book(54).rating=4;

book(55).name='The week';

book(55).author='Newspaper';

book(55).quantity='3';

book(55).rating=4;

book(56).name=' The Hindu';

book(56).author='Newspaper';

book(56).quantity='3';

book(56).rating=4;
book(57).name='Time Out';

book(57).author='Newspaper';

book(57).quantity='3';

book(57).rating=4;

book(58).name='Time Asia';

book(58).author='Newspaper';

book(58).quantity='3';

book(58).rating=4;

book(59).name='Outlook';

book(59).author='Newspaper';

book(59).quantity='3';

book(59).rating=4;

book(60).name='Sports Star';

book(60).author='Newspaper';

book(60).quantity='3';

book(60).rating=4;

book(61).name='Business Today';

book(61).author='Newspaper';
book(61).quantity='3';

book(61).rating=4;

bkno=62; %New books will be added from 31 onwards.

%Main loop

exit_status='n'; %If exit status is true/y , the program will exit

while exit_status=='n'

clc

%menu

disp('1.Enter a new book.');

disp('2.View all books and issue.');

disp('3.Return a book.');

disp('4.Exit');

%choice

c=input('Enter your choice: ');

%switch case for the menu

switch c

case 1

%Adding a book

clc

addbk='y'; %exit status for next loop, used to add multiple book
while addbk=='y'

disp('----------------------------------------------')

fprintf('Book No:%d\n',bkno); %Displays book no.

%adding book info

book(bkno).name=input('Name:','s');

book(bkno).author=input('Author:','s');

book(bkno).quantity=input('Quantity:');

book(bkno).rating=input('Rating:');

bkno=bkno+1; %increaments bkno for next book.

addbk=input('Add another book(y/n):','s'); %if addbk=y, loop continues,


else breaks and returns to menu

save y

end

disp('Done! Returning...')

pause(2); %to pause, so that user can read the message

case 2

%veiwing book

clc

for i= 1:bkno-1 %variable which go through all value of bkno array . Last
value is bkno-1 since bkno is assinged for new book

disp('----------------------------------------------')
fprintf('Book no:%d\n',i); %displays book no.

book(i) %displays book information (name, author, quantity, rating)

end

%Menu to give user an option to return to menue or issue book

disp('1.Return to main manu.')

disp('2.Issue a book.')

c1=input('Choice: '); %choice of user

switch c1

case 1

%returns to main menu

disp('Returning...')

pause(1.5);

case 2

%issuing book

clc

disp('---------------------ISSUE--------------------')

ino=input('Enter the book number:'); %this gives reference to array


position of the book.

if ino<bkno %to check if book exist or not

disp('----------------------------------------------')

disp('You have selected:');

book(ino) %shows the book you have selected.


disp('----------------------------------------------')

q=book(ino).quantity;

if q>0 %checks quantity of the books

c3=input('Dou you want to issue this book? (y/n)','s'); %asks user if


the wanna issue this book

if c3=='y' %caase yes

book(ino).quantity = book(ino).quantity- 1; %the quantity of


book will decrease

disp('Book issued!') %message displayed

rem=book(ino).quantity; %reamaining quantity is displayed.

fprintf('Remaining quantity is: %d \n',rem);

else %case no

disp('Book issue cancel!'); %returns to main menu

end

else

disp('Sorry, This book is not available'); %if quantity is 0

end

else

disp('Enter a valid book number!'); %Case invalid book no.

end

pause(4) %pauses so user can see the messsage

otherwise %if user gives wrong choice


disp('Wrong choice, returning...')

pause(3)

end

case 3

%book return

clc

ino=input('Enter the book number as given on the book:'); %again acts as


an reference to the book

if ino<bkno %if valid bkno is entered,

book(ino).quantity=book(ino).quantity+1; %quantity of the specified


book is incresaed showing the return of book.

disp('Book returned!'); % message.

else %is the bkno is wrong,

disp('Please enter a valid book no.'); %message

end

pause(4); %pause so that user can see the message

case 4

%exits

disp('Exiting!!') %message

exit_status='y'; %sets exit statu to y, thus breaking the main loop

pause(1.5); %pauses so that user can see the message


otherwise

%wrong choice

disp('Wrong choice!'); %display message

pause(1.5); %pauses

% and finally restarts the loop

end %end of switch

end %end of main loop

You might also like