package shop; public class Book extends Item { private String title; public Book(String title, double purchasePrice, double salePrice) { this(title, 0, purchasePrice, salePrice); } public Book(String title, int numberPens, double purchasePrice, double salePrice) { super(numberPens, purchasePrice, salePrice); this.title = title; } public void printDetails() { System.out.printf("%-20s ", title); super.printDetails(); } } ---------------------- package shop; public abstract class Item { private int numberPurchased; private double purchasePrice; private double salePrice; private int numberSold; public Item(int numberPurchased, double purchasePrice, double salePrice) { this.numberPurchased = numberPurchased; this.purchasePrice = purchasePrice; this.salePrice = salePrice; numberSold = 0; } public void purchase(int numItems) { numberPurchased += numItems; } public void sell(int numItems) { numberSold += numItems; } public double calcProfit() { return numberSold * (salePrice - purchasePrice); } public void printDetails() { System.out.printf("%3d @ %6.2f %3d @ %6.2f %5.2f\n", numberPurchased, purchasePrice, numberSold, salePrice, calcProfit()); } } --------------- package shop; public class Pen extends Item { private String inkColour; public Pen(String inkColour, double purchasePrice, double salePrice) { this(inkColour, 0, purchasePrice, salePrice); } public Pen(String inkColour, int numberPens, double purchasePrice, double salePrice) { super(numberPens, purchasePrice, salePrice); this.inkColour = inkColour; } public void printDetails() { System.out.printf("%-6s ", inkColour); super.printDetails(); } } ---------- public class Shop { public static void main(String args[]) { Book book1, book2, book3; Pen pen1, pen2; //create the objects book1 = new Book("Pigs may fly", 100, 120); book2 = new Book("Pigs can't fly", 75, 100); book3 = new Book("Pigs must not fly", 10, 75, 100); pen1 = new Pen("Black", 10, 10, 15); pen2 = new Pen("Blue", 10, 15); //manipulate the objects book1.purchase(10); book2.purchase(10); pen2.purchase(10); book1.sell(5); book2.sell(2); book3.sell(1); pen1.sell(10); pen2.sell(3); //print a report System.out.println("\nBooks"); System.out.printf("%-20s %12s %12s %6s\n", "Title", "Bought", "Sold", "Profit"); book1.printDetails(); book2.printDetails(); book3.printDetails(); System.out.println("\nPens"); System.out.printf("%-6s %12s %12s %6s\n", "Ink", "Bought", "Sold", "Profit"); pen1.printDetails(); pen2.printDetails(); System.out.print("\nTotal profit: "); double sum = book1.calcProfit(); sum += book2.calcProfit(); sum += book3.calcProfit(); sum += pen1.calcProfit(); sum += pen2.calcProfit(); System.out.println(sum + "p"); } }