[go: up one dir, main page]

0% found this document useful (0 votes)
17 views3 pages

Q PVQJDzy

Oop 2 employee code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Q PVQJDzy

Oop 2 employee code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/*----------------------------------SIB42_Abhishek_Nimbalkar--------------------------

---- Identify commonalities and differences between Publication, Book and


Magazine classes. Title,Price, Copies are common instance variables and
saleCopy is common method. The differences are, Bookclass has author
and orderCopies(). Magazine Class has methods orderQty, Current issue
receiveissue().Write a program to find how many copies of the given books are
ordered and display total sale of publication.*/
package P1;
import java.util.Scanner;
class Publication { String title; double price; int copies;
public int setCopies(int c) { this.copies -= c; return this.copies; }
public void setPrice(double p){ this.price = p; }
public void saleCopy(double totalPrice){ System.out.println(”Total Publication
sell: ” + totalPrice); } }
class Book extends Publication { String author; int bookNo; static int total-
BooksSold = 0; static int totalMagazinesSold = 0;
Book() { title = null; price = 0.0; copies = 0; author = null; }
Book(int index, String T, String a, double p, int c) { bookNo = index; title =
T; price = p; author = a; copies = c; }
public double orderCopies(Book[] b) { double totalPrice = 0.0; int flag = 0, ch;
Scanner scr = new Scanner(System.in);
do { System.out.println(”Books available with us:”); for (Book book : b) {
System.out.println(book.bookNo + ” \t ” + book.title + ” \t ” + book.author
+ ” \t ” + book.price + ” \t ” + book.copies); }
System.out.println(”Enter the Book Number You want to Order:”); int bNo =
scr.nextInt(); int qty;
for (Book book : b) { if (book.bookNo == bNo) { System.out.println(”Enter
number of copies You want to Order:”); qty = scr.nextInt();
if (book.copies >= qty) { System.out.println(”Copies Ordered Successfully”);
totalPrice += (book.price * qty); System.out.println(”Total amount for ” +
book.title + ” is: ” + totalPrice); System.out.println(”Number of Copies of
” + book.title + ” left are ” + book.setCopies(qty)); totalBooksSold += qty;
flag = 1; break; } else { System.out.println(”Sorry, not sufficient copies”); } }
else { flag = 0; } } if (flag == 0) { System.out.println(”Book not found”); }
System.out.println(”Do you want to order more books? \n1: Yes 2: No ”); ch
= scr.nextInt(); } while (ch == 1);
return totalPrice; }

1
public void saleCopy(double totalPrice) { System.out.println(”Total Books sell:
” + totalPrice); } }
class Magazine extends Publication { public static String totalMagazinesSold;
int mno; int currentIssued = 0;
Magazine() { title = null; price = 0.0; copies = 0; }
Magazine(int index, String T, double p, int c) { mno = index; title = T; price
= p; copies = c; }
public double orderQty(Magazine[] m) { double totalPrice = 0.0; int flag = 0;
Scanner scr = new Scanner(System.in); System.out.println(”Magazines avail-
able with us:”); for (Magazine mag : m) { System.out.println(mag.mno +
” \t ” + mag.title + ” \t ” + mag.price + ” \t ” + mag.copies); } Sys-
tem.out.println(”Enter the Magazine number You want to Order:”); int index1
= scr.nextInt(); int qty; for (Magazine mag : m) { if (mag.mno == index1)
{ System.out.println(”Enter number of copies You want to Order:”); qty =
scr.nextInt(); if (mag.copies >= qty) { System.out.println(”Copies Ordered Suc-
cessfully”); currentIssued = qty; System.out.println(”Copies currently issued
are: ” + currentIssued); System.out.println(”Number of Copies of ” + mag.title
+ ” left are ” + mag.setCopies(qty)); totalPrice += (mag.price * qty); flag = 1;
break; } else { System.out.println(”Sorry, not sufficient copies”); } } else { flag
= 0; } } if (flag == 0) { System.out.println(”Magazine not found”); } return
totalPrice; }
@Override public void saleCopy(double totalPrice) { System.out.println(”Total
Magazine sell: ” + totalPrice); } }
public class MainClass { public static void main(String[] args) { double p = 0,
mp = 0; Book[] bookList = new Book[5]; bookList[0] = new Book(1, ”Effective
Java”, ”Joshua Bloch”, 1200, 10); bookList[1] = new Book(2, ”Clean Code”,
”Robert C. Martin”, 1500, 10); bookList[2] = new Book(3, ”Design Patterns”,
”Erich Gamma”, 1650, 10); bookList[3] = new Book(4, ”Introduction to Algo-
rithms”, ”Thomas H. Cormen”, 1100, 15); bookList[4] = new Book(5, ”Java
Concurrency in Practice”, ”Brian Goetz”, 1400, 20);
Magazine[] m1 = new Magazine[5]; m1[0] = new Magazine(1, ”Data Structure”,
300, 10); m1[1] = new Magazine(2, ”Computer Network”, 400, 10); m1[2] = new
Magazine(3, ”Code Harry”, 550, 12); m1[3] = new Magazine(4, ”Tech Monthly”,
250, 5); m1[4] = new Magazine(5, ”Science Digest”, 350, 8);
Scanner scr = new Scanner(System.in); System.out.println(”Welcome to Book
& Magazine Store”); int ch, ch1; do { System.out.println(”Enter the correct
choice to order \n1: Book \n2: Magazine”); ch = scr.nextInt();
if (ch == 1) { Book bookObj = new Book(); p = bookObj.orderCopies(bookList);
bookObj.saleCopy(p); } else { Magazine magObj = new Magazine(); mp =
magObj.orderQty(m1); magObj.saleCopy(mp); } System.out.println(”\nDo

2
you want to order something more? \n1: Yes \n2: No”); ch1 = scr.nextInt(); }
while (ch1 == 1);
Publication pubObj = new Publication(); pubObj.saleCopy(p + mp); Sys-
tem.out.println(”Total number of books sold: ” + Book.totalBooksSold); Sys-
tem.out.println(”Total number of magazines sold: ” + Magazine.totalMagazinesSold);
}}

You might also like