Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Problem with JFile Chooser input and output

  1. #1
    prabhdsun is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Problem with JFile Chooser input and output

    Here is a Gui program that our teacher told us to design. I will Paste the Entire code below.
    Java Code:
    /**
    * 
    * @author Prabhdeep Singh
    * Extends Employee and takes in all the data
    */
    public class CommissionWorker extends Employee
    {
    public final static double RATE=0.057; // Rate
    public final static double INITIAL=250; // Initial
    private double weeklySales; // Weekly Sales
    
    /**
    * Calls superclass, puts in id and weeklysales
    * @param Id
    * @param WeeklySales
    */
    
    public CommissionWorker(int id, double weeklySales) // Constructor for calling superclass and putting in data
    {
    super(id);
    this.weeklySales=weeklySales;
    super.weeklyPay=this.calculateWeeklyPay();
    
    }
    /**
    * @return a String Comission Worker
    */
    public String toString()	 // return as a string
    {
    return "Commission worker "+super.toString();
    }
    /**
    * @return a Double of the calculation
    */
    public double calculateWeeklyPay() // calculates weeklypay
    {
    return INITIAL+weeklySales*RATE;
    }
    
    }
    Java Code:
    /**
    * Abstract Class Employee
    * @author Prabhdeep Singh
    *
    */
    public abstract class Employee {
    protected int id; // id
    protected double weeklyPay; // weeklyPay
    
    abstract double calculateWeeklyPay(); // abstract method
    /**
    * Constructor of the public abstract class Employee
    * @param id
    */
    public Employee(int id) // takes an int as a argument
    {
    this.id=id;
    }
    /**
    * Void Method
    * @param id
    * 
    */
    public void setId(int id) // takes int as an arguement
    {
    this.id=id;
    }
    /**
    * Returns an int of arguement ID
    * @return Id
    */
    public int getId() // returns the id arguement
    {
    return id;
    }
    /**
    * Converts to string and returns a string
    */
    
    public String toString() // toString method
    {
    return id+"has a weekly pay of "+weeklyPay;
    }
    /**
    * Returns the weeklypay
    * @return weeklyPay
    */
    
    public double getWeeklyPay() // getter method
    {
    return weeklyPay;
    }
    Java Code:
    }
    import java.util.ArrayList;
    import java.text.NumberFormat;
    /**
    * This EmployeesManager implements EmployeesManagerInterface provided by the teacher
    * @author Prabhdeep Singh
    *
    */
    
    public class EmployeesManager implements EmployeesManagerInterface 
    {
    
    private ArrayList<Employee> Moneyroll;	// ArrayLists 
    private NumberFormat form=NumberFormat.getCurrencyInstance(); // Formats the number
    private int NumberOfHourly; // hourly works
    private int NumberOfCommission; // commission works
    private int NumberOfPiece; // piece works
    private int NumberOfManagers; // manager works
    
    /**
    * EmployeesManager Constructor
    */
    public EmployeesManager() // constructor which initializes all values
    {
    NumberOfHourly=0;
    NumberOfManagers=0;
    Moneyroll=new ArrayList<Employee>();
    NumberOfCommission=0;
    NumberOfPiece=0;
    
    }
    
    /**
    * @param paycode
    * @param firstParam
    * @param SecondParam
    * @param empNum
    */
    public void addEmployee(int paycode, double firstParam, int secondParam, int empNum) // Gets the different paycodes then implements them
    {
    if (paycode==1)
    {
    NumberOfManagers++;
    Moneyroll.add(new Manager(empNum, firstParam));
    }
    else if(paycode==2)
    {
    NumberOfHourly++;
    Moneyroll.add(new HourlyWorker(empNum, firstParam, secondParam));
    }
    else if (paycode==3)
    {
    NumberOfCommission++;
    Moneyroll.add(new CommissionWorker(empNum, firstParam));
    }
    else if(paycode==4)
    {
    NumberOfPiece++;
    Moneyroll.add(new PieceWorker(empNum, firstParam, secondParam));
    }
    }
    /**
    * @return NumberOfHourly
    */
    public int getNumHourlyWorker() // getter
    {
    return NumberOfHourly;
    }
    /**
    * @return NumberOfManagers
    */
    public int getNumManager() // getter
    {
    return NumberOfManagers;
    }
    /**
    * @return NumberOfComission
    */
    public int getNumCommissionWorker() // getter
    {
    return NumberOfCommission;
    }
    /**
    * @return NumberOfPiece
    */
    public int getNumPieceWorker() // getter
    {
    return NumberOfPiece;
    }
    /**
    * @return FinalReport - A huge Report of what happened in the program.
    */
    public String generateWeeklyReport() // Weekly Report of the entire thing
    {
    String FinalReport="WEEKLY PAY REPORT FOR WIDGET COMPANY\n\nEMPLOYEE WEEKLYPAY\n ";
    for (Employee y: Moneyroll)
    {
    FinalReport+=y.getId()+" "+form.format(y.getWeeklyPay())+"\n ";
    }
    FinalReport+="\n";
    FinalReport+="Total payroll: "+form.format(this.calculateTotalWeeklyPay())+"\n" ;
    FinalReport+="Total number of Managers paid:" +NumberOfManagers+"\n";
    FinalReport+="Total number of Hourly Workers paid:" +NumberOfHourly+"\n";
    FinalReport+="Total number of Commission Workers paid:" +NumberOfCommission+"\n";
    FinalReport+="Total number of Piece Workers paid:" +NumberOfPiece+"\n";
    
    return FinalReport;
    
    }
    /**
    * @return Final Total, which is the totalWeeklyPay
    */
    public double calculateTotalWeeklyPay() // calculates the total weeklypay
    {
    double FinalTotal=0;
    for (Employee x: Moneyroll)
    {
    FinalTotal+=x.getWeeklyPay();
    }
    return FinalTotal;
    }
    Java Code:
    }
    /**
    * Interface for the Employees class for CS103, Assignment 7, Fall 2012
    * Contains an ArrayList of objects that inherit from the Abstract class Employee
    * Keeps track of the number of Mangers, Hourly Workers, Commission Workers 
    * and Piece Workers employees
    * @author Professor Myers
    *
    */
    public interface EmployeesManagerInterface {
    /**
    * Add an employee to the ArrayList.
    * @param paycode Type of employee: 1-Manager, 2-Hourly Worker, 3-Commission Worker, 4-Piece Worker
    * @param firstParam 1-salary, 2-hourly rate, 3-weekly sales, 4-rate per piece
    * @param secondParam 1-not needed (0), 2-number of hours, 3-not needed (0), 4-number of pieces
    * @param empNum employee number
    */
    public void addEmployee(int paycode, double firstParam, int secondParam, int empNum);
    
    /**
    * Returns the number of Managers in the ArrayList
    * @return number of Managers
    */
    public int getNumManager();
    
    /**
    * Returns the number of Hourly Workers in the ArrayList
    * @return number of Hourly Workers
    */
    public int getNumHourlyWorker();
    
    /**
    * Returns the number of Commission Worker in the ArrayList
    * @return number of Commission Worker
    */
    public int getNumCommissionWorker();
    
    /**
    * Returns the number of Piece Worker in the ArrayList
    * @return number of Piece Worker
    */
    public int getNumPieceWorker();
    
    /**
    * Generates a weekly report of employee weekly pay
    * @return String that contains the Weekly Report
    */
    public String generateWeeklyReport();
    
    /**
    * Calculate the total weekly pay for all employees in the ArrayList
    * @return the total weekly pay for all employees
    */
    public double calculateTotalWeeklyPay();
    
    /**
    * Creates a string representation of the object
    * @return String that represents the objects in the ArrayList
    */
    public String toString();
    
    
    }
    Java Code:
    /**
    * HourlyWorker extends the Employee class
    * @author Prabhdeep Singh
    *
    */
    public class HourlyWorker extends Employee
    {
    private static final int INITIAL_HOURS=40; // initial hours
    private static final double OVERTIME_RATE=1.5; // overtime 
    private double payrate; // payrate
    private int hours; // hours
    
    /**
    * Constructor for HourlyWorker
    * @param id
    * @param payrate
    * @param hours
    */
    public HourlyWorker(int id, double payrate, int hours) // constructor which calls superclass and sets parameters
    {
    super(id);
    this.hours=hours;
    this.payrate=payrate;
    super.weeklyPay=this.calculateWeeklyPay();
    }
    /**
    * @return Hourly Worker a string arguement
    */
    public String toString() // toString method
    {
    return "Hourly Worker " +super.toString();
    }
    /**
    * @return WeeklyPay
    */
    public double calculateWeeklyPay() // Calculates weekly Pay
    {
    if (hours<= INITIAL_HOURS)
    return payrate*hours;
    else
    return INITIAL_HOURS*payrate+(hours-INITIAL_HOURS)*OVERTIME_RATE*payrate;
    }
    
    
    }
    Java Code:
    /**
    * Manager extends Employee
    * @author Prabhdeep Singh
    *
    */
    public class Manager extends Employee
    {
    
    /**
    * Manager Constructor takes in arguements and calls superclass
    * @param id
    * @param weeklyPay
    */
    public Manager(int id, double weeklyPay) // Constructor
    {
    super(id);
    super.weeklyPay=weeklyPay;
    }
    
    public String toString() // returns a String
    {
    return "Manager " + super.toString();
    }
    
    public double calculateWeeklyPay() // calculates weeklypay
    {
    return super.weeklyPay;
    }
    
    }
    Java Code:
    /**
    * PieceWorker extends Employee, takes in all the arguements
    * @author Prabhdeep Singh
    *
    */
    public class PieceWorker extends Employee
    {
    
    private int sales; // sales
    private double price; // price
    
    /**
    * PieceWorker Consstructor takes in arguements and calls superclass
    * @param id
    * @param price
    * @param sales
    */
    public PieceWorker(int id, double price, int sales) // constructor
    {
    super(id);
    this.price=price;
    this.sales=sales;
    super.weeklyPay=this.calculateWeeklyPay();
    }
    /**
    * @return a String
    */
    public String toString() // returns a String
    {
    return "Piece Worker "+ super.toString();
    }
    /**
    * @return calculateWeeklyPay
    */
    public double calculateWeeklyPay() // Calculates Weeklypay
    {
    
    return sales*price;
    }
    
    }
    Java Code:
    /**
    * Prabhdeep Singh
    * CS 103
    * 
    */
    import javax.swing.*;
    /**
    * This WindowFrame class uses the WindowPanel class in order to create a new EmployeeBook.
    * 
    *
    */
    public class WindowFrame extends JFrame
    {
    private WindowPanel Panel = new WindowPanel(); // Creates a WIndowPanel object
    
    public WindowFrame() // WindowFrame Constructor
    {
    
    JFrame WindowBox = new JFrame(); // Creates a new JFrame
    
    WindowBox.setTitle("Employee Management"); // sets JFrame title
    WindowBox.setSize(500, 500); // sets JFrame size
    WindowBox.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE); // sets the operation as to what happens when you close the gui
    WindowBox.add(Panel); // adds the Panel to the JFrame
    WindowBox.pack(); // Packs for size refitting
    WindowBox.setVisible(true); // sets visibility of the Frame
    
    }
    public static void main(String[] args)
    {
    new WindowFrame(); // creates an instance of the WindowFrame class
    }
    
    }
    Java Code:
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    /**
    * @author Prabhdeep Singh- CS 103 
    * Driver class 
    * Gets information from the user as to what sort of employee
    */
    
    public class WindowPanel extends JPanel 
    {
    private JButton B1, B2, B3, B4, B5; // buttons 
    private JRadioButton RB1, RB2, RB3, RB4; // radiobuttons
    private JTextField TF1, TF2,TF3; // textfields
    private JLabel L1, L2, L3; // labels
    private EmployeesManager Moneyroll; // Class EmployeesManager
    
    /**
    * Provides all the buttons, radio buttons, text fields, and bordered titles which are used in the gui
    */
    public WindowPanel() // Constructor
    {
    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    JPanel Panel1 = new JPanel();
    TitledBorder Titled1 = BorderFactory.createTitledBorder("Type of employee");
    RB1 = new JRadioButton("Commission Worker", true);
    RB2= new JRadioButton("Piece Worker");
    RB3 = new JRadioButton("Manager");
    RB4 = new JRadioButton("Hourly Worker");
    ButtonGroup BigGroup = new ButtonGroup();
    BigGroup.add(RB1);
    BigGroup.add(RB2);
    BigGroup.add(RB3);
    BigGroup.add(RB4);
    RB1.addActionListener(new RadioButtonListener());
    RB2.addActionListener(new RadioButtonListener());
    RB3.addActionListener(new RadioButtonListener());
    RB4.addActionListener(new RadioButtonListener());
    Panel1.setBorder(Titled1);
    Panel1.add(RB1);
    Panel1.add(RB2);
    Panel1.add(RB3);
    Panel1.add(RB4);
    add(Panel1);
    
    JPanel Panel2 = new JPanel();
    L1 = new JLabel("Employee ID: ");
    L2 = new JLabel("Weekly Sales");
    L3 = new JLabel();
    TF1 = new JTextField(15);
    TF2 = new JTextField(10);
    TF3 = new JTextField(5);
    L3.setVisible(false);
    TF3.setVisible(false);
    Panel2.add(L1);
    Panel2.add(TF1);
    Panel2.add(L2);
    Panel2.add(TF2);
    Panel2.add(L3);
    Panel2.add(TF3);
    
    add(Panel2);
    
    JPanel Panel3 = new JPanel();
    B1 = new JButton("Add Employee");
    B2 = new JButton("Write Report to File");
    B3 = new JButton("Display Summary Pay Report");
    B4 = new JButton("Read From File");
    B5 = new JButton("Exit");
    B1.addActionListener(new JButtonListener());
    B2.addActionListener(new JButtonListener());
    B3.addActionListener(new JButtonListener());
    B4.addActionListener(new JButtonListener());
    B5.addActionListener(new JButtonListener());
    Panel3.add(B1);
    Panel3.add(B2);
    Panel3.add(B3);
    Panel3.add(B4);
    Panel3.add(B5);
    
    add(Panel3);
    
    Moneyroll=new EmployeesManager();
    
    
    
    }
    private class RadioButtonListener implements ActionListener // RadioButton ActionListener Class
    {
    public void actionPerformed(ActionEvent e)
    {
    Object Clicker = e.getSource();
    if (Clicker == RB1 )
    {
    L2.setText("Weekly Sales");
    TF3.setVisible(false);
    L3.setVisible(false);
    
    }
    else if (Clicker == RB4)
    {
    L2.setText("PayRate");
    TF3.setVisible(true);
    L3.setVisible(true);
    L3.setText("Hours");
    }
    else if (Clicker == RB3)
    {
    L2.setText("Salary");
    TF3.setVisible(false);
    L3.setVisible(false);
    
    }
    else if (Clicker == RB2)
    {
    L2.setText("Piece Rate");
    L3.setText("# pieces");
    TF3.setVisible(true);
    L3.setVisible(true);
    }
    }
    
    }
    private class JButtonListener implements ActionListener // ButtonListener Class
    {
    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource()==B1)
    {
    
    if (RB1.isSelected())
    {
    Moneyroll.addEmployee(3, Double.parseDouble(TF2.getText()), 0, Integer.parseInt(TF1.getText()));
    }
    else if(RB3.isSelected())
    {
    Moneyroll.addEmployee(1, Double.parseDouble(TF2.getText()), 0, Integer.parseInt(TF1.getText()));
    }
    else if (RB4.isSelected())
    {
    Moneyroll.addEmployee(2, Double.parseDouble(TF2.getText()), Integer.parseInt(TF3.getText()), Integer.parseInt(TF1.getText()));
    }
    else if (RB2.isSelected())
    {
    Moneyroll.addEmployee(4, Double.parseDouble(TF2.getText()), Integer.parseInt(TF3.getText()), Integer.parseInt(TF1.getText()));
    }
    }
    else if (e.getSource() == B2)
    {
    
    }
    else if(e.getSource()==B3)
    {
    JOptionPane.showMessageDialog(null, Moneyroll.generateWeeklyReport());
    }
    else if(e.getSource()==B4)
    {
    
    JFileChooser fileChooser = new JFileChooser();
    int status = fileChooser.showOpenDialog(null);
    if(status == JFileChooser.APPROVE_OPTION)
    {
    File selectedFile = fileChooser.getSelectedFile();
    try {
    Scanner scan = new Scanner(selectedFile);
    int EmployeeID, Position, Ticker;	 //Creates variable that will hold the employee id, hours, or number of pieces
    double Payment;	 //Creates a variable that will hold the salary, weekly sales, payrate, or piece rate
    
    while(scan.hasNextLine())
    {
    Position = scan.nextInt();	 //Gets the position choice from the file
    if(Position==4 || Position==2)
    {
    Payment = scan.nextDouble();	 //Gets the payrate or the piece rate from the file
    Ticker = scan.nextInt();	 //Gets the hours or num of pieces
    EmployeeID = scan.nextInt();	 //Get the employee ID
    }
    else
    {
    Payment = scan.nextDouble();	 //Gets the weekly sales or salary from the file
    Ticker = 0;
    EmployeeID = scan.nextInt();	 //Get the employee ID
    }
    
    Moneyroll.addEmployee(Position, Payment, Ticker, EmployeeID);	 //Adds the data from the file to the array
    }	
    scan.close();	
    String filename = selectedFile.getPath();
    JOptionPane.showMessageDialog(null, "You selected " + filename);
    } catch (FileNotFoundException e1) {
    
    e1.printStackTrace();
    }
    //Creates a new scanner that will read from a file
    
    }
    }
    else if (e.getSource()==B5)
    {
    System.exit(0);
    }
    }
    }
    }
    Ok I know this is a VERY long code, but i will explain to you my problem. When you look at the code

    else if(e.getSource() == B3)
    {
    JOptionPane.showMessageDialog(null, Moneyroll.generateWeeklyReport());
    }
    Our teacher wants the WeeklyReport that is generated by clicking on B3, - The summary report button, to be outputted/written to a text file using JFileChooser - Something i have no clue what to do about so need dire help for it.

    As you can see under the code

    else if(e.getSource() == B2)
    {
    }
    I have no code. This is the button for Write Report to file. What this button is suppose to do is that when a file is read into the program, and lets say you add another employee using the gui, when you click the Write report to File button, it adds the data that you added from the GUI into the file, and when you click on Display Summary Pay report, it shows the data that you put in from the GUI in the JOptionPane.showMessageDialog(null, Moneyroll.generateWeeklyReport()); that shows up.

    Would be very grateful for any help i could get. i would put up the java assignment as well but the Manage attachments on this site isnt seem to be working.
    Last edited by prabhdsun; 12-04-2012 at 06:35 AM.

  2. #2
    prabhdsun is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Problem with JFile Chooser input and output

    Any Help would be great.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with JFile Chooser input and output

    Quote Originally Posted by prabhdsun View Post
    Ok I know this is a VERY long code, but i will explain to you my problem. When you look at the code ...
    No, that is a h3ll of a lot of code, and all of it unformatted(!), probably more than most volunteers will be willing to sift through. One part of asking volunteers for free help is your putting in effort to make it easier for them to help you.

  4. #4
    prabhdsun is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Problem with JFile Chooser input and output

    Ok so how should i format it so its easier to read?
    Quote Originally Posted by Fubarable View Post
    No, that is a h3ll of a lot of code, and all of it unformatted(!), probably more than most volunteers will be willing to sift through. One part of asking volunteers for free help is your putting in effort to make it easier for them to help you.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Problem with JFile Chooser input and output

    Quote Originally Posted by prabhdsun View Post
    Ok so how should i format it so its easier to read?
    This will tell you: Code Conventions for the Java Programming Language: Contents

    Another great link: SSCCE (Short, Self Contained, Correct (Compilable), Example)

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Trouble with J File Chooser for input and output
    By prabhdsun in forum Advanced Java
    Replies: 1
    Last Post: 12-04-2012, 06:03 AM
  2. Replies: 4
    Last Post: 11-27-2012, 11:07 AM
  3. Problem with input/output code
    By Outsider418 in forum New To Java
    Replies: 2
    Last Post: 05-19-2012, 05:02 PM
  4. JFile Chooser
    By karno in forum NetBeans
    Replies: 4
    Last Post: 03-17-2010, 12:47 PM
  5. File chooser internationalization problem
    By Astghik in forum AWT / Swing
    Replies: 10
    Last Post: 01-14-2010, 06:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •