In: Computer Science
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class described below. Each class is worth 50 points. When you are finished, zip the project folder and submit it.
Example 1 (output format for the headphoneToString method of the Headphone class.)
Bose SoundLink
- You have 6 headphones left.
- This headphone has been restocked 2 times.
Example 2 (output format for the summary method of the HeadphoneInventory class.)
Headphone Summary
-------------------------------------------------------------
Bose SoundLink
- You have 6 headphones left.
- This headphone has been restocked 2 times.
======================================
Sennheiser RS 195
- You have 8 headphones left.
- This headphone has been restocked 0 times.
======================================
Koss BT540i
- You have 5 headphones left.
- This headphone has been restocked 1 times.
======================================
Headphone.java:
package Exam1; import java.util.Random; public class Headphone { // members private String manufacturer; private String name; private int quantity; private int timesRestocked; // constructor public Headphone(String manufacturer, String name) { this.manufacturer = manufacturer; this.name = name; // generate a number between 0 and 9 then add 3 to make the number // between 3 and 12 quantity = new Random().nextInt(10) + 3; timesRestocked = 0; } // purchase method public void purchase() { --quantity; } // restock method public void restock() { quantity += 3; timesRestocked++; } // accessor for quantity public int getQuantity() { return quantity; } // headphoneEquals method public boolean headphoneEquals(String manufacturer, String name) { if (this.manufacturer.equalsIgnoreCase(manufacturer) && this.name.equalsIgnoreCase(name)) return true; else return false; } // display headphone details public String headphoneToString() { String temp = ""; temp += manufacturer + " " + name + "\n"; temp += "- You have " + quantity + " headphones left.\n"; temp += "- This heaphone has been restocked " + timesRestocked + " times.\n"; return temp; } }
HeadphoneInventory.java:
package Exam1; public class HeadphoneInventory { private Headphone[] headphones = new Headphone[3]; public HeadphoneInventory() { headphones[0] = new Headphone("Bose","SoundLink"); headphones[1] = new Headphone("Sennheiser","RS 195"); headphones[2] = new Headphone("Koss","BT540i"); } public void sellHeadphone(String manufacturer, String name) { int headphoneQuantity; boolean flag = false; for (int i = 0; i < 3; i++) { if (headphones[i].headphoneEquals(manufacturer,name)) { flag = true; headphones[i].purchase(); headphoneQuantity = headphones[i].getQuantity(); if (headphoneQuantity <= 3) { System.out.println("You have " + headphoneQuantity + " " + manufacturer + " " + name + " headphones left in stock. You are running low. Maybe it is time to restock?" ); headphones[i].restock(); } else System.out.println("You have sold a " + manufacturer + " " + name); } } if(!flag) System.out.println("Sorry, we do not seem to have the " + manufacturer + " " + name + " headphones in stock."); } public void summary() { System.out.println("\n\nHeadphone Summary"); System.out.println("------------------------------------------------------------"); for (Headphone h: headphones) { System.out.println(h.headphoneToString()); System.out.println("========================================================="); } } public static void main(String[] args) { HeadphoneInventory hi = new HeadphoneInventory(); hi.sellHeadphone("bose","soundlink"); hi.sellHeadphone("kia","alto"); hi.sellHeadphone("bose","soundlink"); hi.sellHeadphone("bose","soundlink"); hi.sellHeadphone("Koss","BT540i"); hi.sellHeadphone("Koss","BT540i"); hi.summary(); } }
OUTPUT: