**question after 3 codes mport java.awt.Color; import java.awt.Font; import java
ID: 3833639 • Letter: #
Question
**question after 3 codes
mport java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
/**
*
*/
/**
* @author
*
*/
public class Instrument {
public NumberFormat nf = NumberFormat.getCurrencyInstance();
private String name;
private double cost;
private Picture picture;
private Sound sound;
/**
* no argument constructor
*/
public Instrument() {
}
/**
*
* @param name
*
* @param cost
*
* @param picture
*
* @param sound
*
*/
public Instrument(String name, double cost, Picture picture, Sound sound) {
this.name = name;
this.cost = cost;
this.picture = picture;
this.sound = sound;
}
/**
*
* @return name
*
*/
public String getName() {
return name;
}
/**
*
* @return cost
*
*/
public double getCost() {
return cost;
}
/**
*
* @return picture
*
*/
public Picture getPicture() {
return picture;
}
/**
*
* @return sound
*
*/
public Sound getSound() {
return sound;
}
/**
*
* @param name
*
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @param cost
*
*/
public void setCost(double cost) {
this.cost = cost;
if (cost < 0)
cost = 299.99;
JOptionPane.showMessageDialog(null, "Cost is less than zero, default cost set to $299.99");
}
/**
*
* @param picture
*
*/
public void setPicture(Picture picture) {
this.picture = picture;
}
/**
*
* @param sound
*
*/
public void setSound(Sound sound) {
this.sound = sound;
}
@Override
public String toString() {
return "Name: " + name + ", Cost: " + nf.format(cost) + ", Picture Url: " + picture + ", Sound: " + sound;
}
public Picture labelImage(Color c, int fontSize)
{
Picture temp = picture;
Graphics g = temp.getGraphics();
g.setColor(c);
g.setFont(new Font("Arial",Font.BOLD,fontSize));
g.drawString(name, 25, 75);
return temp;
}
}
/**
*
*/
/**
* @author
*
*/
public class Percussion extends Instrument {
private String drumType;
private boolean pitched;
/**
*no argument constructor
*/
public Percussion() {
}
/**
* @param name
*
* @param cost
*
* @param pictureUrl
*
* @param sound
*
* @param drumType
*
* @param pitched
*
*/
public Percussion(String name, double cost, Picture picture,
Sound sound, String drumType, boolean pitched) {
super(name, cost, picture, sound);
this.drumType = drumType;
this.pitched = pitched;
}
/**
*
* @return drumType
*
*/
public String getDrumType() {
return drumType;
}
/**
*
* @return pitched
*
*/
public boolean isPitched() {
return pitched;
}
/**
*
* @param drumType
*
*/
public void setDrumType(String drumType) {
this.drumType = drumType;
}
/**
*
* @param pitched
*
*/
public void setPitched(boolean pitched) {
this.pitched = pitched;
}
@Override
public String toString() {
return super.toString() + ", Drum Type" + drumType + ", Is pitched: " + pitched;
}
}
import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.NumberFormat;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
*/
/**
* @author
*
*/
public class TestMusicStore {
/**
* @param args
*/
public NumberFormat nf = NumberFormat.getCurrencyInstance();
/**
* Fills a passed Instrument array with the values of different Instrument and Percussion objects; some filled for you, some given
* a name by the user (original idea was to have the name be used to call the pic and sound file too, and while it worked just fine, it was
* a bit of a pain through testing, where I didn't want to type the full name of a valid item every time I needed to test)
* @param inventory - Passed Instrument array from calling method
*/
public void fillInventory (Instrument [] inventory) {
boolean doubleCheck = false;
inventory[0] = new Instrument("Whistle", 62.55, new Picture ("Whistle.PNG"), new Sound ("Whistle.wav"));
inventory[1] = new Instrument("Harp", 599.99, new Picture ("Harp.PNG"), new Sound ("Harp.wav"));
inventory[2] = new Percussion("Bongos", 49.99, new Picture ("Bongos.PNG"), new Sound ("Bongos.wav"), "macho", false);
inventory[3] = new Percussion("Drums", 200.99, new Picture ("drum.jpg"), new Sound ("Drums.wav"), "bass", true);
inventory[4] = new Instrument();
String tempName = JOptionPane.showInputDialog("Enter name of an instrument");
inventory[4].setName(tempName);
double tempCost = 0.0;
while (!doubleCheck) {
try {
tempCost = Double.parseDouble(JOptionPane.showInputDialog("How much does " + tempName + " cost?"));
doubleCheck = true;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter a valid number!");
}
}
inventory[4].setCost(tempCost);
inventory[4].setPicture(new Picture ("Bell.PNG"));
inventory[4].setSound(new Sound ("Bell.wav"));
inventory[5] = new Percussion();
tempName = JOptionPane.showInputDialog("Enter Name of a percussion instrument");
inventory[5].setName(tempName);
doubleCheck = false;
while (!doubleCheck) {
try {
tempCost = Double.parseDouble(JOptionPane.showInputDialog("How much does " + tempName + " cost?"));
doubleCheck = true;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter a valid number!");
}
}
inventory[5].setCost(tempCost);
inventory[5].setPicture(new Picture ("cymbals.jpg"));
inventory[5].setSound(new Sound ("Cymbals.wav"));
((Percussion)inventory[5]).isPitched();
((Percussion)inventory[5]).setDrumType("crash");
}
/**
* Method which sets up the GUI which is used to allow the user to dictate how the program works. A higher focus on method calls and separating some of this bloated
* method into smaller separate methods would have been wise, but things worked well enough!
* @param inventory - passed Instrument array
*/
public void displayMenu(Instrument [] inventory) {
int userSelection = 0;
String userInstrument = "";
boolean exit;
boolean properSelection;
int i = 0;
while (true) {
properSelection = false;
exit = false;
try {
userSelection = Integer.parseInt(JOptionPane.showInputDialog("Please choose from the following: 1) Select an instrument 2) Display Inventory 3) Listen to Inventory"
+ " 4) Display Total Inventory Values 5) Display Labeled Images 6) Exit"));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please only enter numbers!");
// Previous command iterates again after exception catch if user value isn't reset; I found it annoying
userSelection = 0;
}
switch (userSelection) {
case 1:
while (!properSelection) {
userInstrument = JOptionPane.showInputDialog("Which instrument would you like to select?");
for (i = 0; i < inventory.length; i++){
if (userInstrument.toLowerCase().compareTo(inventory[i].getName().toLowerCase()) == 0) {
properSelection = true;
break;
}
}
if (!properSelection) {
JOptionPane.showMessageDialog(null, "Item not found in inventory. Please try again");
}
}
properSelection = false;
while(!exit) {
try {
userSelection = Integer.parseInt(JOptionPane.showInputDialog("What would you like to do? 1) Display image of " + userInstrument
+ " 2) Play sound of " + userInstrument + " 3) Compare prices of " + userInstrument + " 4) Exit"));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please only enter numbers!");
// Same as above
userSelection = 0;
}
switch (userSelection) {
case 1:
(inventory[i].getPicture()).show();
break;
case 2:
(inventory[i].getSound()).play();
break;
case 3:
String fileName = "https://www.walmart.com/search/?query=" + inventory[i].getName().toLowerCase() + "&cat_id=4171_1015079";
String seq = "baseprice=";
String price = "";
String line = null;
double comparePrice = 0.0;
String bestPrice = "";
try {
URL url = new URL (fileName);
InputStream inStr = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStr));
while ((line = reader.readLine()) != null && line.indexOf(seq) < 0) {}
if (line != null) {
int seqIndex = line.indexOf(seq) + 9;
int stopIndex = line.indexOf(" dohideitemcontrols");
price = line.substring(seqIndex + 1, stopIndex);
}
comparePrice = Double.parseDouble(price);
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null,"Couldn't find file " + fileName);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,"Error during read or write");
ex.printStackTrace();
}
if (comparePrice == 0) {
break;
}
if (comparePrice > inventory[i].getCost()) {
bestPrice = "We have";
}
else if (comparePrice < inventory[i].getCost()) {
bestPrice = "Competitor has";
}
JOptionPane.showMessageDialog(null, "Competitor sells " + inventory[i].getName() + " for " + nf.format(comparePrice) +"; we sell " + inventory[i].getName()
+ " for " + nf.format(inventory[i].getCost()) + " " + bestPrice + " the best price");
break;
case 4:
exit = true;
}
}
break;
case 2:
String allValues = "";
for (Instrument j: inventory) {
allValues += j + " ";
}
JOptionPane.showMessageDialog(null, allValues);
break;
case 3:
i = 0;
while (i < inventory.length) {
inventory[i].getSound().blockingPlay();
i++;
}
break;
case 4:
double totalValue = 0;
for (i = 0; i < inventory.length; i++) {
totalValue += inventory[i].getCost();
}
JOptionPane.showMessageDialog(null, "The total cost of all the items in our inventory is " + nf.format(totalValue));
break;
case 5:
Picture label = new Picture ();
int imgSize = 36;
for (i = 0; i < inventory.length; i++) {
label = inventory[i].labelImage(randColor(), imgSize);
label.show();
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
// Makes it so the font size changes without having to input a different size each time, to meet the 3 different size requirement
imgSize = (imgSize < 72) ? imgSize + 12 : imgSize - 36;
}
break;
case 6:
updateObj2Cost(inventory);
modifyObj3Image(inventory);
modifyObj1Name(inventory);
copyDisplaySubclassObj(inventory);
return;
}
}
}
/**
* Works alongside the 5th option to label and print each image in inventory as to print different colors without having to manually call the method a bunch of times
* with different parameters, to meet the 3 color requirement (although the chance of only showing 2 of the 6 colors is still possible, even if the odds are low)
* @return - returns randomly selected color to calling method
*/
public Color randColor () {
Color [] randColor = {Color.RED, Color.BLUE, Color.GREEN, Color.PINK, Color.ORANGE, Color.BLACK};
Random rand = new Random ();
int randNum = rand.nextInt(6);
return randColor[randNum];
}
/**
* Changes the cost variable of the second object in the array to an invalid (0 or less) number, to invoke the default price
* @param inventory
*/
public void updateObj2Cost (Instrument [] inventory) {
JOptionPane.showMessageDialog(null, "The current cost of " + inventory[1].getName() + " is " + nf.format(inventory[1].getCost()));
inventory[1].setCost(0.0);
JOptionPane.showMessageDialog(null, "The updated cost of " + inventory[1].getName() + " is " + nf.format(inventory[1].getCost()));
}
/**
* Shows the image currently stored in the third object, negates the picture, and shows it once more.
* @param inventory - passed Instrument array
*/
public void modifyObj3Image (Instrument [] inventory) {
Picture pic = new Picture (inventory[2].getPicture());
JOptionPane.showMessageDialog(null, "Picture of " + inventory[2].getName());
pic.show();
negate(pic);
JOptionPane.showMessageDialog(null, "Picture of " + inventory[2].getName() + " negated");
pic.hide();
pic.show();
try {
Thread.sleep(1000);
pic.hide();
}
catch (InterruptedException ex){
Thread.currentThread().interrupt();
}
}
/**
* Changes the name value of the first object to the same name, but all uppercase
* @param inventory - passed instrument array
*/
public void modifyObj1Name(Instrument [] inventory) {
JOptionPane.showMessageDialog(null, "The current name of the first item is " + inventory[0].getName());
inventory[0].setName(inventory[0].getName().toUpperCase());
JOptionPane.showMessageDialog(null, "The name has been changed to " + inventory[0].getName());
}
/**
* Takes a percussion object in the passed array (predetermined), makes a copy, and prints only the percussion-unique values in it
* @param inventory - passed instrument array
*/
public void copyDisplaySubclassObj (Instrument [] inventory) {
Percussion obj = (Percussion)inventory[5];
String isPitched = (obj.isPitched()) ? "is pitched" : "is not pitched";
JOptionPane.showMessageDialog(null, "The type of drum is " + obj.getDrumType() + " and it " + isPitched);
}
/**
* Takes the passed picture, negates the colors, and passes it back to calling method
* @param pic - passed Picture object
*/
public void negate(Picture pic) {
Pixel[] pixelArray = pic.getPixels();
Pixel pixel = null;
int redValue, blueValue, greenValue = 0;
for (int i = 0; i < pixelArray.length; i++) {
pixel = pixelArray[i];
redValue = pixel.getRed();
greenValue = pixel.getGreen();
blueValue = pixel.getBlue();
pixel.setColor(new Color (255 - redValue, 255 - greenValue, 255 - blueValue));
}
}
public static void main(String[] args) {
Instrument [] inventory = new Instrument [6];
TestMusicStore store = new TestMusicStore();
store.fillInventory(inventory);
store.displayMenu(inventory);
}
}
To test your TestMusicStore do the following:
Modify the class TestMusicStore to do the following
Create one array that will hold 3 Instrument objects and 3 Percussion objects
Call a method named fillInventory which does the following
Instantiate 4 array objects (2 of each type) using the constructor with parameters
Instantiate one of each type of object using the no parameter constructor
Use input dialog boxes to input the name and the cost for the 2 ‘default’ objects, and then use set methods to store data for each default object
Call a method named displayMenu with the following choices
1.Select an Instrument
Use a linear search to look for that instrument by name
If the instrument is found display another menu with these choices
Image – displays the object’s image
Sound – plays the object’s sound
Compare Price – finds the first occurrence of this object’s price on Amazon.com using search sequence “suggested retail price”. If found, compare to stored price and determine best deal, else display message.
(http://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords= )
Exit
If the instrument is not found display a message saying so
2.Display Inventory
Use a for-each loop to loop through the array and uses a toString( ) method print to ALL of the information about each object
3.Listen to Inventory
Use a while loop to play all the sounds of the objects in your array (use blockingPlay)
4.Display Total Inventory Value
Compute and display the total cost of all instruments in your array
5.Display Labeled Images
Use a for loop to label (use the method you wrote in your base class) and display all the images of your Instrument objects array. Use 3 different colors and 3 different font sizes.
6.Execute final 4 methods (described below)
7.Exit
Call a method named updateObj2Cost
Display the cost of the second object
Set the cost of the second object in your array to 0
Display the cost of the second object again
Call a method named modifyObj3Image
Explore the picture for the third object in your array
Negate the picture for the third object in your array and explore it again
Call a method named modifyObj1Name
Display the name of the first object
Use a predefined method to modify/store the name of the first object to upper case
Display the name of the first object again
Call a method named copyDisplaySubclassObj
Create a new Percussion object and store one of your array objects in it. Display the subclass information only (no base class information) for that object. (hint: typecast
Explanation / Answer
mport java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
/**
*
*/
/**
* @author
*
*/
public class Instrument {
public NumberFormat nf = NumberFormat.getCurrencyInstance();
private String name;
private double cost;
private Picture picture;
private Sound sound;
/**
* no argument constructor
*/
public Instrument() {
}
/**
*
* @param name
*
* @param cost
*
* @param picture
*
* @param sound
*
*/
public Instrument(String name, double cost, Picture picture, Sound sound) {
this.name = name;
this.cost = cost;
this.picture = picture;
this.sound = sound;
}
/**
*
* @return name
*
*/
public String getName() {
return name;
}
/**
*
* @return cost
*
*/
public double getCost() {
return cost;
}
/**
*
* @return picture
*
*/
public Picture getPicture() {
return picture;
}
/**
*
* @return sound
*
*/
public Sound getSound() {
return sound;
}
/**
*
* @param name
*
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @param cost
*
*/
public void setCost(double cost) {
this.cost = cost;
if (cost < 0)
cost = 299.99;
JOptionPane.showMessageDialog(null, "Cost is less than zero, default cost set to $299.99");
}
/**
*
* @param picture
*
*/
public void setPicture(Picture picture) {
this.picture = picture;
}
/**
*
* @param sound
*
*/
public void setSound(Sound sound) {
this.sound = sound;
}
@Override
public String toString() {
return "Name: " + name + ", Cost: " + nf.format(cost) + ", Picture Url: " + picture + ", Sound: " + sound;
}
public Picture labelImage(Color c, int fontSize)
{
Picture temp = picture;
Graphics g = temp.getGraphics();
g.setColor(c);
g.setFont(new Font("Arial",Font.BOLD,fontSize));
g.drawString(name, 25, 75);
return temp;
}
}
/**
*
*/
/**
* @author
*
*/
public class Percussion extends Instrument {
private String drumType;
private boolean pitched;
/**
*no argument constructor
*/
public Percussion() {
}
/**
* @param name
*
* @param cost
*
* @param pictureUrl
*
* @param sound
*
* @param drumType
*
* @param pitched
*
*/
public Percussion(String name, double cost, Picture picture,
Sound sound, String drumType, boolean pitched) {
super(name, cost, picture, sound);
this.drumType = drumType;
this.pitched = pitched;
}
/**
*
* @return drumType
*
*/
public String getDrumType() {
return drumType;
}
/**
*
* @return pitched
*
*/
public boolean isPitched() {
return pitched;
}
/**
*
* @param drumType
*
*/
public void setDrumType(String drumType) {
this.drumType = drumType;
}
/**
*
* @param pitched
*
*/
public void setPitched(boolean pitched) {
this.pitched = pitched;
}
@Override
public String toString() {
return super.toString() + ", Drum Type" + drumType + ", Is pitched: " + pitched;
}
}
import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.NumberFormat;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
*/
/**
* @author
*
*/
public class TestMusicStore {
/**
* @param args
*/
public NumberFormat nf = NumberFormat.getCurrencyInstance();
/**
* Fills a passed Instrument array with the values of different Instrument and Percussion objects; some filled for you, some given
* a name by the user (original idea was to have the name be used to call the pic and sound file too, and while it worked just fine, it was
* a bit of a pain through testing, where I didn't want to type the full name of a valid item every time I needed to test)
* @param inventory - Passed Instrument array from calling method
*/
public void fillInventory (Instrument [] inventory) {
boolean doubleCheck = false;
inventory[0] = new Instrument("Whistle", 62.55, new Picture ("Whistle.PNG"), new Sound ("Whistle.wav"));
inventory[1] = new Instrument("Harp", 599.99, new Picture ("Harp.PNG"), new Sound ("Harp.wav"));
inventory[2] = new Percussion("Bongos", 49.99, new Picture ("Bongos.PNG"), new Sound ("Bongos.wav"), "macho", false);
inventory[3] = new Percussion("Drums", 200.99, new Picture ("drum.jpg"), new Sound ("Drums.wav"), "bass", true);
inventory[4] = new Instrument();
String tempName = JOptionPane.showInputDialog("Enter name of an instrument");
inventory[4].setName(tempName);
double tempCost = 0.0;
while (!doubleCheck) {
try {
tempCost = Double.parseDouble(JOptionPane.showInputDialog("How much does " + tempName + " cost?"));
doubleCheck = true;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter a valid number!");
}
}
inventory[4].setCost(tempCost);
inventory[4].setPicture(new Picture ("Bell.PNG"));
inventory[4].setSound(new Sound ("Bell.wav"));
inventory[5] = new Percussion();
tempName = JOptionPane.showInputDialog("Enter Name of a percussion instrument");
inventory[5].setName(tempName);
doubleCheck = false;
while (!doubleCheck) {
try {
tempCost = Double.parseDouble(JOptionPane.showInputDialog("How much does " + tempName + " cost?"));
doubleCheck = true;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter a valid number!");
}
}
inventory[5].setCost(tempCost);
inventory[5].setPicture(new Picture ("cymbals.jpg"));
inventory[5].setSound(new Sound ("Cymbals.wav"));
((Percussion)inventory[5]).isPitched();
((Percussion)inventory[5]).setDrumType("crash");
}
/**
* Method which sets up the GUI which is used to allow the user to dictate how the program works. A higher focus on method calls and separating some of this bloated
* method into smaller separate methods would have been wise, but things worked well enough!
* @param inventory - passed Instrument array
*/
public void displayMenu(Instrument [] inventory) {
int userSelection = 0;
String userInstrument = "";
boolean exit;
boolean properSelection;
int i = 0;
while (true) {
properSelection = false;
exit = false;
try {
userSelection = Integer.parseInt(JOptionPane.showInputDialog("Please choose from the following: 1) Select an instrument 2) Display Inventory 3) Listen to Inventory"
+ " 4) Display Total Inventory Values 5) Display Labeled Images 6) Exit"));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please only enter numbers!");
// Previous command iterates again after exception catch if user value isn't reset; I found it annoying
userSelection = 0;
}
switch (userSelection) {
case 1:
while (!properSelection) {
userInstrument = JOptionPane.showInputDialog("Which instrument would you like to select?");
for (i = 0; i < inventory.length; i++){
if (userInstrument.toLowerCase().compareTo(inventory[i].getName().toLowerCase()) == 0) {
properSelection = true;
break;
}
}
if (!properSelection) {
JOptionPane.showMessageDialog(null, "Item not found in inventory. Please try again");
}
}
properSelection = false;
while(!exit) {
try {
userSelection = Integer.parseInt(JOptionPane.showInputDialog("What would you like to do? 1) Display image of " + userInstrument
+ " 2) Play sound of " + userInstrument + " 3) Compare prices of " + userInstrument + " 4) Exit"));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please only enter numbers!");
// Same as above
userSelection = 0;
}
switch (userSelection) {
case 1:
(inventory[i].getPicture()).show();
break;
case 2:
(inventory[i].getSound()).play();
break;
case 3:
String fileName = "https://www.walmart.com/search/?query=" + inventory[i].getName().toLowerCase() + "&cat_id=4171_1015079";
String seq = "baseprice=";
String price = "";
String line = null;
double comparePrice = 0.0;
String bestPrice = "";
try {
URL url = new URL (fileName);
InputStream inStr = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStr));
while ((line = reader.readLine()) != null && line.indexOf(seq) < 0) {}
if (line != null) {
int seqIndex = line.indexOf(seq) + 9;
int stopIndex = line.indexOf(" dohideitemcontrols");
price = line.substring(seqIndex + 1, stopIndex);
}
comparePrice = Double.parseDouble(price);
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null,"Couldn't find file " + fileName);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,"Error during read or write");
ex.printStackTrace();
}
if (comparePrice == 0) {
break;
}
if (comparePrice > inventory[i].getCost()) {
bestPrice = "We have";
}
else if (comparePrice < inventory[i].getCost()) {
bestPrice = "Competitor has";
}
JOptionPane.showMessageDialog(null, "Competitor sells " + inventory[i].getName() + " for " + nf.format(comparePrice) +"; we sell " + inventory[i].getName()
+ " for " + nf.format(inventory[i].getCost()) + " " + bestPrice + " the best price");
break;
case 4:
exit = true;
}
}
break;
case 2:
String allValues = "";
for (Instrument j: inventory) {
allValues += j + " ";
}
JOptionPane.showMessageDialog(null, allValues);
break;
case 3:
i = 0;
while (i < inventory.length) {
inventory[i].getSound().blockingPlay();
i++;
}
break;
case 4:
double totalValue = 0;
for (i = 0; i < inventory.length; i++) {
totalValue += inventory[i].getCost();
}
JOptionPane.showMessageDialog(null, "The total cost of all the items in our inventory is " + nf.format(totalValue));
break;
case 5:
Picture label = new Picture ();
int imgSize = 36;
for (i = 0; i < inventory.length; i++) {
label = inventory[i].labelImage(randColor(), imgSize);
label.show();
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
// Makes it so the font size changes without having to input a different size each time, to meet the 3 different size requirement
imgSize = (imgSize < 72) ? imgSize + 12 : imgSize - 36;
}
break;
case 6:
updateObj2Cost(inventory);
modifyObj3Image(inventory);
modifyObj1Name(inventory);
copyDisplaySubclassObj(inventory);
return;
}
}
}
/**
* Works alongside the 5th option to label and print each image in inventory as to print different colors without having to manually call the method a bunch of times
* with different parameters, to meet the 3 color requirement (although the chance of only showing 2 of the 6 colors is still possible, even if the odds are low)
* @return - returns randomly selected color to calling method
*/
public Color randColor () {
Color [] randColor = {Color.RED, Color.BLUE, Color.GREEN, Color.PINK, Color.ORANGE, Color.BLACK};
Random rand = new Random ();
int randNum = rand.nextInt(6);
return randColor[randNum];
}
/**
* Changes the cost variable of the second object in the array to an invalid (0 or less) number, to invoke the default price
* @param inventory
*/
public void updateObj2Cost (Instrument [] inventory) {
JOptionPane.showMessageDialog(null, "The current cost of " + inventory[1].getName() + " is " + nf.format(inventory[1].getCost()));
inventory[1].setCost(0.0);
JOptionPane.showMessageDialog(null, "The updated cost of " + inventory[1].getName() + " is " + nf.format(inventory[1].getCost()));
}
/**
* Shows the image currently stored in the third object, negates the picture, and shows it once more.
* @param inventory - passed Instrument array
*/
public void modifyObj3Image (Instrument [] inventory) {
Picture pic = new Picture (inventory[2].getPicture());
JOptionPane.showMessageDialog(null, "Picture of " + inventory[2].getName());
pic.show();
negate(pic);
JOptionPane.showMessageDialog(null, "Picture of " + inventory[2].getName() + " negated");
pic.hide();
pic.show();
try {
Thread.sleep(1000);
pic.hide();
}
catch (InterruptedException ex){
Thread.currentThread().interrupt();
}
}
/**
* Changes the name value of the first object to the same name, but all uppercase
* @param inventory - passed instrument array
*/
public void modifyObj1Name(Instrument [] inventory) {
JOptionPane.showMessageDialog(null, "The current name of the first item is " + inventory[0].getName());
inventory[0].setName(inventory[0].getName().toUpperCase());
JOptionPane.showMessageDialog(null, "The name has been changed to " + inventory[0].getName());
}
/**
* Takes a percussion object in the passed array (predetermined), makes a copy, and prints only the percussion-unique values in it
* @param inventory - passed instrument array
*/
public void copyDisplaySubclassObj (Instrument [] inventory) {
Percussion obj = (Percussion)inventory[5];
String isPitched = (obj.isPitched()) ? "is pitched" : "is not pitched";
JOptionPane.showMessageDialog(null, "The type of drum is " + obj.getDrumType() + " and it " + isPitched);
}
/**
* Takes the passed picture, negates the colors, and passes it back to calling method
* @param pic - passed Picture object
*/
public void negate(Picture pic) {
Pixel[] pixelArray = pic.getPixels();
Pixel pixel = null;
int redValue, blueValue, greenValue = 0;
for (int i = 0; i < pixelArray.length; i++) {
pixel = pixelArray[i];
redValue = pixel.getRed();
greenValue = pixel.getGreen();
blueValue = pixel.getBlue();
pixel.setColor(new Color (255 - redValue, 255 - greenValue, 255 - blueValue));
}
}
public static void main(String[] args) {
Instrument [] inventory = new Instrument [6];
TestMusicStore store = new TestMusicStore();
store.fillInventory(inventory);
store.displayMenu(inventory);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.