import java.util.*;
import java.text.NumberFormat;
class WelcomeMessage
{ // start class WelcomeMessage
// the WelcomeMessage has two fields
private String line_1;
private String line_2;
// the WelcomeMessage has one constructor
public WelcomeMessage(String line1, String line2)
{
line_1 = new String(line1);
line_2 = new String(line2);
}
} // end class WelcomeMessage
class Item implements Comparable
{ // begin class Item
// class Item fields
private String title;
private int stockNum;
private int stockQty;
private double price;
private double calcvalue;
public Item( )
{
String title = " ";
int stockNum = 0;
int stockQty = 0;
double price = 0.00;
double calcvalue = 0.00;
}
// public Item(String cd_title, short stock_num, short stock_qty, double unit_price )
// { // begin Item class constructor
//
// title = new String(cd_title);
// stockNum = new Short(stock_num);
// stockQty = new Short(stock_qty);
// price = new Double(unit_price);
//
// } // end Item class constructor
// Item class constructor
public Item(String title, int stockNum, int stockQty, double price)
{
this.title = title;
this.stockNum = stockNum;
this.stockQty = stockQty;
this.price = price;
}
// class method to get the title
public String getTitle( )
{
return title;
}
// class method to set the title
public void setTitle(String title)
{
this.title = title;
}
// class method to get the stock number
public int getStockNum( )
{
return stockNum;
}
// class method to set the stock number
public void setStockNum(int stockNum)
{
this.stockNum = stockNum;
}
// class method to get the stock quantity
public int getStockQty( )
{
return stockQty;
}
// class method to set the stock quantity
public void setStockQty(int stockQty)
{
this.stockQty = stockQty;
}
// class method to get the price of each unit
public Double getPrice( )
{
return price;
}
// class method to set the price of each unit
public void setPrice(Double price)
{
this.price = price;
}
// class method to calculate the inventory value
public Double calcValue( )
{
return getStockQty( ) * getPrice( );
}
//
public int compareTo(Object o)
{
Item c = (Item) o;
return title.compareTo(c.getTitle( ) );
}
public String toString( )
{
return "Title: " + title + "\nStock Number: " + stockNum + "\nQty In Stock: "
+ stockQty + "\nUnit Price: $" + price + "\nIn Stock Value: $" + (stockQty * price);
}
} // end class Item
public class Inventory
{ // begin class Inventory
public static void main(String args [ ] )
{ // begin main
WelcomeMessage welcome; // create WelcomeMessage object welcome
// initilize fields of welcome
String line_1 = "\nWelcome to the CD Inventory Program!";
String line_2 = "\nThis program displays the CDs in stock alphabetically by Title\nand then calculates the value of the current CD
inventory in stock.\n";
// print welcome message
System.out.println( line_1);
System.out.println( line_2 );
Item[ ] cds = new Item[7]; // create array
// fill the elements
cds[0] = new Item( );
cds[0].setTitle("Disco Stomp");
cds[0].setStockNum(10001);
cds[0].setStockQty(10);
cds[0].setPrice(9.99);
cds[1] = new Item( );
cds[1].setTitle("Don't Stop Dancing");
cds[1].setStockNum(10002);
cds[1].setStockQty(8);
cds[1].setPrice(14.99);
cds[2] = new Item( );
cds[2].setTitle(" Get The Funk Out Of Ma Face");
cds[2].setStockNum(10003);
cds[2].setStockQty(5);
cds[2].setPrice(16.99);
cds[3] = new Item( );
cds[3].setTitle("Casanova");
cds[3].setStockNum(10004);
cds[3].setStockQty(13);
cds[3].setPrice(10.99);
cds[4] = new Item( );
cds[4].setTitle("When You've Been Blessed");
cds[4].setStockNum(10005);
cds[4].setStockQty(6);
cds[4].setPrice(12.99);
cds[5] = new Item( );
cds[5].setTitle("A House Is Not A Home");
cds[5].setStockNum(10006);
cds[5].setStockQty(12);
cds[5].setPrice(12.99);
cds[6] = new Item( );
cds[6].setTitle(" Fortunate");
cds[6].setStockNum(10007);
cds[6].setStockQty(8);
cds[6].setPrice(12.99);
// call the array sort utility for cds
Arrays.sort(cds);
// print the cd inventory by title from compare titles in Item class
for (Item c: cds)
{
System.out.println(c);
System.out.println( );
}
// format, calculate and print the inventory value
double value = 0.00;
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance( );
for(int a = 0; a < cds.length; a++)
{
value = value + cds[a].calcValue( );
}
String formattedValue = currencyFormat.format(value);
System.out.println("The current CD inventory value is: " + formattedValue);
// // initialize fields of item
// String title = "Disco Stomp";
// Short stockNum = 10001;
// Short stockQty = 10;
// Double price = 12.99;
//
// // store item information to item object of Item class
// item = new Item( title, stockNum, stockQty, price );
//
// // initialize value
// Double value = item.calcValue( );
//
// System.out.println( "\nCurrent CD Inventory:" );
// System.out.printf( "\nTitle: " + item.getTitle( ) );
// System.out.printf( "\nStock Number: " + item.getStockNum( ) );
// System.out.printf( "\nQuantity: " + item.getStockQty( ) );
// System.out.printf( "\nPrice: " + item.getPrice( ) );
// System.out.printf( "\n\nThe current CD inventory value is: $" + item.calcValue( ) );
// System.out.println();
} // end main
} // end class Invent