Q PVQJDzy
Q PVQJDzy
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);
}}