package inventory;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.lang.*;
public class CDs extends JFrame{
private static final long serialVersionUID = 1L; // serial version
//// this area controls the open, add, and closing of the file
private Scanner x;
public void openFile(){
try{
x = new Scanner(new File("test.txt"));
}catch(Exception e){
System.out.println("An error occured.");
}
}// end openFile
public void readFile(){
while(x.hasNext()){
String a = x.next();
String b = x.next();
String c = x.next();
String d = x.next();
String e = x.next();
System.out.printf("%s %s %s %s %s\n", a, b, c, d, e);
}
}
public void closeFile(){
x.close();
}
/// end file open, add, and close
// declares var for currentIndex for button functionality
public int currentIndex = 0;
private JLabel labelName;
private JLabel labelID;
private JLabel labelStock;
private JLabel labelGenre;
private JLabel labelPrice;
private JLabel labelReStock;
private JLabel labelFval;
private JLabel labelTval;
private InvMore[] CD;
private JLabel imageLabel;
public CDs(){// sets the GUI
CD = new InvMore[5]; // creates the variable CD for the array
/// CD inventory objects
CD[0] = new InvMore("Red Hot Chili Peppers", "Alternative", 1, 35, 16.99);
CD[1] = new InvMore("Nirvana", "Classic Rock", 2, 25, 16.99);
CD[2] = new InvMore("Styx", "Classic Rock", 3, 28, 16.99);
CD[3] = new InvMore("Jay Z", "Hip Hop/Rap", 4, 37, 16.99);
CD[4] = new InvMore("Nelly", "Hip Hop/Rap", 5, 32, 16.99);
// sets the values for the window
setTitle("Your Inventory Program");
setSize(600,550);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
getContentPane().add(panel1);
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
// creates image logo
imageLabel = new JLabel();// creates the label for the logo
imageLabel.setIcon(new ImageIcon("D:\\UOP111\\JavaWork\\inventory\\src\\inventory\\TMDlogo.png")); // found out that you need to have the \\ when calling the directory
panel1.add(imageLabel);
//creates the panel and layout
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.NORTH);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
/// fields numbered 1-8 for easy matching with other functions and adding
labelName = new JLabel ("CD Name: ");//1
labelName.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(labelName);//1
labelID = new JLabel ("CD ID: ");//2
labelID.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(labelID);//2
labelGenre = new JLabel ("CD genre: ");//3
labelGenre.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(labelGenre);//3
labelStock = new JLabel ("Stock amount: ");//4
labelStock.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(labelStock);//4
labelPrice = new JLabel ("CD Price: ");//5
labelPrice.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(labelPrice);//5
labelReStock = new JLabel ("ReStock value: ");//6
labelReStock.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(labelReStock);//6
labelFval = new JLabel ("CD stock value: ");//7
labelFval.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(labelFval);//7
/// creates the buttons
JButton previous = new JButton("Previous");
panel.add(previous);
JButton next = new JButton("Next");
panel.add(next);
//**** working on implementing this ****
labelTval = new JLabel ("Total CD inventory value: ");//8
labelTval.setFont(new Font("Serif", Font.ITALIC, 14));
panel.add(labelTval);//8
///// these are the active listeners for the button functions
previous.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showPrevious();
}
});
next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showNext();
}
});
/// calls the GUI display for object results
updateGUI();
InvMore item = CD[currentIndex];
labelTval.setText(String.format("The Total CD Inventory Value: $%.2f", item.gettotValue(CD)));//8
}//end GUI
public static void main(String[] args){ // start main
new CDs().setVisible(true); // this calls the GUI
// calls the file open, add, and close
CDs g = new CDs();
g.openFile();
g.readFile();
g.closeFile();
// end the file calls
}// end main
public void updateGUI() {
InvMore item = CD[currentIndex];
labelName.setText(String.format("Name: %s",item.getName()));//1
labelID.setText(String.format("ID: %s",item.getID()));//2
labelGenre.setText(String.format("Genre: %s",item.getGenre()));//3
labelStock.setText(String.format("In-Stock: %s",item.getStock()));//4
labelPrice.setText(String.format("Price: $%.2f",item.getPrice()));//5
labelReStock.setText(String.format("ReStock Value: $%.2f",item.getReStock()));//6
labelFval.setText(String.format("Total Stock Value: $%.2f",item.getValStock()));//7
}// ends updateGUI
/// methods for the buttons
private void showNext() { // method for the next button
if (currentIndex < CD.length - 1) {
currentIndex++;
updateGUI();
}
}
private void showPrevious() { // method for the previous button
if (currentIndex > 0) {
currentIndex--;
updateGUI();
}
}
}// end class