Instructions:
Now it's time to code. You will be creating a program that implements a stack of Songs.
Step 1: Create a class called Song that has 3 attributes-title, artist, and price. It should contain 2 constructor methods (a no-arg and full parameter), all the necessary set and get methods, and a "toString" method that prints the values of the 3 attributes. A starter file has been provided below- download it and finish the code for it.
Step 2: Create a class called "SimpleSongStack" which will be used for creating/manipulating a stack of Song objects (use the file "SimpleStack" as a guide).
Step 3: Create a class called "UseSimpleSongStack" which will utilize the "SimpleSongStack" class to create a stack of songs. This program should initialize an "ITunesGiftCard" variable to $15.00. Then it should set up a while loop to request a Song object's data from the user (title, artist, and price), create a Song object with the data, and push the newly created Song object onto the stack. The loop should track the money spent on each song ordered (assume song prices are either 0.99, 1.29, or 1.49) by subtracting it from the gift card total. When no more songs can be purchased (ITunesGiftCard < 0.99), the loop should terminate. After the loop finishes, the program should report the "Songs Recently Purchased:". To do this, pop each song object from the song stack and call it's "toString" method.
File 1: Song.java
public class Song
{
private String title;
private String artist;
private double price;
public Song(String Title, String Artist, double Price)
{
this.title = Title;
this.artist = Artist;
this.price = Price;
}
public Song()
{
title = null;
artist = null;
price = 0;
}
public String getTitle()
{
return this.title;
}
public void setTitle(String newTitle)
{
this.title = newTitle;
}
public String getArtist()
{
return this.artist;
}
public void setArtist(String newArtist)
{
this.artist = newArtist;
}
public Double getPrice()
{
return this.price;
}
public void setPrice(Double newPrice)
{
this.price = newPrice;
}
public String toString()
{
return title + " by " + artist + "\n Price: " + price + "\n";
}
}
File 2: SimpleSongStack.java
public class SimpleSongStack
{
private Song[] stack;
private int top;
public SimpleSongStack(int capacity)
{
stack = new Song[capacity];
top = 0;
}
public boolean isEmpty()
{
return (top == 0);
}
public boolean isFull()
{
return (top == stack.length);
}
public void push(Song item) throws Exception
{
if (isFull())
{
throw new Exception("stack overflow");
}
else
{
stack[top++] = item;
}
}
public Song pop() throws Exception
{
if (isEmpty())
{
throw new Exception("stack underflow");
}
else
{
return (stack[--top]);
}
}
}
File 3: UsesSimpleSongStack.java
import java.util.Scanner;
public class UseSimpleSongStack
{
public static void main (String args[]) throws Exception
{
SimpleStack myStack = new SimpleStack(16);
int numberOfSongItems;
Song item;
String itemName;
String itemArtist;
double itemPrice;
double itunesGiftCard = 15.00;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of song items you would like to purchase: ");
numberOfSongItems = keyboard.nextInt();
keyboard.nextLine();
for (int i=0; i< numberOfSongItems; i++)
{
System.out.println("\nEnter the name of the song: ");
itemName = keyboard.nextLine();
System.out.println("\nEnter the artist of the song:");
itemArtist = keyboard.nextLine();
System.out.println("Enter the price for this item: ");
itemPrice = keyboard.nextInt();
keyboard.nextLine();
item = new Song(itemName, itemArtist, itemPrice);
System.out.println("...pushing the " + item.getDescription() + " song on the stack...");
myStack.push(item) ;
}
System.out.println("\nPopping Song Items off the stack:");
for (int i=0; i< numberOfSongItems; i++)
{
item = myStack.pop();
System.out.println("...the item " + item.getDescription() + " was popped off the stack...");
}
}
}
HELP:
I am getting an error message in the File 3 UseSimpleSongStack.java and I dont know why or how. I have bolded the troubled parts.
PLEASE HELP. IM DESPERATE