Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 7th, 2012, 08:27 PM
cgodfrey987 cgodfrey987 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 27 cgodfrey987 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 34 sec
Reputation Power: 0
Help with separating into multiple classes

Can someone help me figure out how I would break this program into multiple different classes? Like one for each button off of the mainframe window at the beginning of the application?? Thanks for any help. Here is my code so far. I know it's not complete yet:

Code:

package financialCalc;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.text.DecimalFormat;
import java.awt.*;
import java.awt.event.*;

public class FinancialCalc extends JFrame
{
	double balance, minimumPayment;
	String payment, fixedPayment;
	private JPanel welcomePanel, greetingPanel, buttonPanel, minPmtButtonPanel, minPmtPanel;
	private JLabel message, Amt, Term, Rate, minPmt, fixedPmt;
	//private JLabel autoAmtArray[], autoRateArray[], autoTermArray[];
	private JTextField AmtF, TermF, RateF, minPmtF, fixedPmtF;
	private JButton autoPmt, mortPmt, homeAfford, costOfMin, exit, minPmtButton, calcPmts;
	private JFrame mainFrame, autoPmtFrame, mortPmtFrame, minPmtFrame;
	private Dimension d = new Dimension(400,400);
	private DecimalFormat dlr = new DecimalFormat("#,##0.00");
	private DecimalFormat pct = new DecimalFormat("#0");
	//private JCheckBox add;
	
	public FinancialCalc()
	{
		
		mainFrame = new JFrame("Financial Calculator Application");		
		
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
		
		mainFrame.setPreferredSize(d);
		mainFrame.setLayout(new BorderLayout());
		mainFrame.setLocationRelativeTo(null);
		
		buildMenuBar();
		buildWelcomeMenu();
		
		mainFrame.add(greetingPanel, BorderLayout.NORTH);
		mainFrame.add(welcomePanel, BorderLayout.CENTER);
		mainFrame.add(buttonPanel, BorderLayout.SOUTH);		
		
		mainFrame.pack();
		mainFrame.setVisible(true);
	}
	
	
	public void buildMenuBar()
	{
		JMenuBar menuBar;
		JMenu fileMenu;
		JMenu editMenu;
		JMenuItem exit;
		JMenuItem cut;
		JMenuItem copy;
		JMenuItem paste;
		
		menuBar = new JMenuBar();
		
		fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		
		exit = new JMenuItem("Exit");
		exit.setMnemonic(KeyEvent.VK_E);
		exit.addActionListener(new ExitButtonListener());
	
		fileMenu.add(exit);
		
		
		editMenu = new JMenu("Edit");
		editMenu.setMnemonic(KeyEvent.VK_E);
		
		cut = new JMenuItem("Cut");
		cut.setMnemonic(KeyEvent.VK_X);
		
		copy = new JMenuItem("Copy");
		copy.setMnemonic(KeyEvent.VK_C);
		
		paste = new JMenuItem("Paste");
		paste.setMnemonic(KeyEvent.VK_P);
		
		editMenu.add(cut);
		editMenu.add(copy);
		editMenu.add(paste);
		
		
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		
		setJMenuBar(menuBar);
	}
		
	
	public void buildWelcomeMenu()
	{
		welcomePanel = new JPanel();
		welcomePanel.setLayout(new GridLayout(4,1));
		welcomePanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));	
		
		autoPmt = new JButton("Auto loan payment calculator");
		autoPmt.setMnemonic(KeyEvent.VK_A);
		autoPmt.addActionListener(new AutoButtonListener());
		
		mortPmt = new JButton("Mortgage payment calculator");
		mortPmt.setMnemonic(KeyEvent.VK_M);
		mortPmt.addActionListener(new MortButtonListener());
		
		homeAfford = new JButton("How much home can you afford?");
		homeAfford.setMnemonic(KeyEvent.VK_H);
		homeAfford.addActionListener(new HomeButtonListener());
		
		costOfMin = new JButton("True cost of paying the minimum payment");
		costOfMin.setMnemonic(KeyEvent.VK_C);
		costOfMin.addActionListener(new CostMinButtonListener());
		
		buttonPanel = new JPanel();
		exit = new JButton("Exit");
		exit.setMnemonic(KeyEvent.VK_E);
		exit.addActionListener(new ExitButtonListener());
		buttonPanel.add(exit);
		
		welcomePanel.add(autoPmt);
		welcomePanel.add(mortPmt);
		welcomePanel.add(homeAfford);
		welcomePanel.add(costOfMin);
	
		
		greetingPanel = new JPanel();
		message = new JLabel("Welcome to the Financial Calculations Application");
		greetingPanel.add(message);
		
		
	}
	
	
	private class AutoButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			JPanel autoPmtPanel, autoButtonPanel;
			autoPmtFrame = new JFrame();
			autoPmtFrame = new JFrame("Auto Payment Calculator");			
			autoPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			autoPmtFrame.setLayout(new GridLayout(2,1));
			autoPmtFrame.setPreferredSize(d);
			autoPmtFrame.setLocationRelativeTo(null);
			
			autoPmtPanel = new JPanel();
			autoPmtPanel.setLayout(new GridLayout(4,2));
			autoPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
			
			
			autoButtonPanel = new JPanel();
			//add = new JCheckBox("Compare another loan");
			//add.addActionListener(new AddButtonListener());
			
						
			JButton calculate = new JButton("Calculate");
			JButton exit = new JButton("Exit");
			calculate.addActionListener(new AutoCalcButtonListener());
			exit.addActionListener(new ExitButtonListener());
			
			Amt = new JLabel("Loan Amount");
			Term = new JLabel("Term in months");
			Rate = new JLabel("Interest Rate");
			
			
			AmtF = new JTextField(5);
			TermF = new JTextField(5);
			RateF = new JTextField(5);
			
			autoPmtPanel.add(Amt);
			autoPmtPanel.add(AmtF);
			autoPmtPanel.add(Term);
			autoPmtPanel.add(TermF);
			autoPmtPanel.add(Rate);
			autoPmtPanel.add(RateF);
			
			autoButtonPanel.add(calculate);
			autoButtonPanel.add(exit);

			autoPmtFrame.add(autoPmtPanel);
			autoPmtFrame.add(autoButtonPanel);
			
			autoPmtFrame.pack();
			autoPmtFrame.setVisible(true);
			
		}
	
	}
	
	private class AutoCalcButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
			String str;
			double amt, n, rate, denominator;
			double monthlyPmt, totalInt, totalAmtPaid;
			
			str = AmtF.getText();
			amt = Double.parseDouble(str);
			
			str = TermF.getText();
			n = Double.parseDouble(str);
			
			str = RateF.getText();
			rate = Double.parseDouble(str);
			
			rate = rate / 100 / 12;
			denominator = 1- Math.pow(1 + rate, -n);
			
			monthlyPmt = amt * (rate / denominator);
			
			totalAmtPaid = monthlyPmt * n;
			
			totalInt = totalAmtPaid - amt;
			
			JOptionPane.showMessageDialog(null, "Monthly Payment is $" + dlr.format(monthlyPmt) + "\n\n" + 
					"Total Amount Paid over " + pct.format(n) + " months: $" + dlr.format(totalAmtPaid) + "\n\n" +
					"Total Interest Paid: $" + dlr.format(totalInt), 
					"Total", JOptionPane.PLAIN_MESSAGE);
			
			
		}
	}
	
	private class MortButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			mortPmtFrame = new JFrame("Mortgage Payment Calculator");
			mortPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			mortPmtFrame.setLayout(new GridLayout(2,1));
			mortPmtFrame.setPreferredSize(d);
			mortPmtFrame.setLocationRelativeTo(null);
						
			JPanel mortPmtPanel, mortButtonPanel;
			
			mortPmtPanel = new JPanel();
			mortButtonPanel = new JPanel();
			
			mortPmtPanel.setLayout(new GridLayout(4,2));
			mortPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
			
			
			JButton calculate = new JButton("Calculate");
			JButton exit = new JButton("Exit");
			calculate.addActionListener(new MortCalcButtonListener());
			exit.addActionListener(new ExitButtonListener());
			
			Amt = new JLabel("Loan Amount");
			Term = new JLabel("Term in months");
			Rate = new JLabel("Interest Rate");
			
			
			AmtF = new JTextField(5);
			TermF = new JTextField(5);
			RateF = new JTextField(5);
			
			mortPmtPanel.add(Amt);
			mortPmtPanel.add(AmtF);
			mortPmtPanel.add(Term);
			mortPmtPanel.add(TermF);
			mortPmtPanel.add(Rate);
			mortPmtPanel.add(RateF);
			
			mortButtonPanel.add(calculate);
			mortButtonPanel.add(exit);

			mortPmtFrame.add(mortPmtPanel);
			mortPmtFrame.add(mortButtonPanel);
			
			mortPmtFrame.pack();
			mortPmtFrame.setVisible(true);
			
		}
	
	}
	
	
	private class MortCalcButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
			String str;
			double amt, n, rate, denominator;
			double monthlyPmt, totalInt, totalAmtPaid;
			
			str = AmtF.getText();
			amt = Double.parseDouble(str);
			
			str = TermF.getText();
			n = Double.parseDouble(str);
			
			str = RateF.getText();
			rate = Double.parseDouble(str);
			
			rate = rate / 100 / 12;
			denominator = 1- Math.pow(1 + rate, -n);
			
			monthlyPmt = amt * (rate / denominator);
			
			totalAmtPaid = monthlyPmt * n;
			
			totalInt = totalAmtPaid - amt;
			
			n = n / 12;
			
			JOptionPane.showMessageDialog(null, "Monthly Payment is $" + dlr.format(monthlyPmt) + "\n\n" + 
					"Total Amount Paid over " + pct.format(n) + " years: $" + dlr.format(totalAmtPaid) + "\n\n" +
					"Total Interest Paid: $" + dlr.format(totalInt), 
					"Total", JOptionPane.PLAIN_MESSAGE);
			
			
		}
	}
	
	
	
	private class CostMinButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			minPmtFrame = new JFrame("True Cost of Paying the Minimum Payment");
			minPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			minPmtFrame.setLayout(new GridLayout(2,1));
			minPmtFrame.setPreferredSize(d);
			minPmtFrame.setLocationRelativeTo(null);
			
			minPmtPanel = new JPanel();
			minPmtButtonPanel = new JPanel();
			
			minPmtPanel.setLayout(new GridLayout(4,2));
			minPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
			
			
			minPmtButton = new JButton("Get Minimum Payment");
		
			minPmtButton.addActionListener(new MinPmtButtonListener());
			
			
			Amt = new JLabel(" Balance ");
			Rate = new JLabel(" Interest Rate ");
			minPmt = new JLabel(" Minimum Payment ");
			fixedPmt = new JLabel(" Fixed Payment ");
			
			AmtF = new JTextField(10);
			RateF = new JTextField(10);
			minPmtF = new JTextField(null);
			fixedPmtF = new JTextField(null);
			
			minPmtPanel.add(Amt);
			minPmtPanel.add(AmtF);
			minPmtPanel.add(Rate);
			minPmtPanel.add(RateF);
			minPmtPanel.add(minPmt);
			minPmtPanel.add(minPmtButton);
			
			minPmtFrame.add(minPmtPanel);
			
			minPmtFrame.pack();
			minPmtFrame.setVisible(true);
			
		}
	
	}
	
	private class MinPmtButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			balance = Double.parseDouble(AmtF.getText());
			
			double fPayment;
			double pmtPct = .02;
			calcPmts = new JButton("Calculate");
			calcPmts.addActionListener(new MinPmtsCalcListener());
			
			minimumPayment = balance * pmtPct;
			fPayment = minimumPayment * 2;
			dlr.format(minimumPayment);
			dlr.format(fPayment);
						
			payment = Double.toString(minimumPayment);
			fixedPayment = Double.toString(fPayment);
			
			minPmtF.setText(payment);
			fixedPmtF.setText(fixedPayment);
			
			minPmtPanel.removeAll();			
			minPmtPanel.add(Amt);
			minPmtPanel.add(AmtF);
			minPmtPanel.add(Rate);
			minPmtPanel.add(RateF);
			minPmtPanel.add(minPmt);
			minPmtPanel.add(minPmtF);
			minPmtPanel.add(fixedPmt);
			minPmtPanel.add(fixedPmtF);			
			
			minPmtButtonPanel.add(calcPmts);
			minPmtFrame.add(minPmtPanel);
			minPmtFrame.add(minPmtButtonPanel);
			
			minPmtFrame.pack();
			minPmtFrame.setVisible(true);
			
		}
	
	}
	
	private class MinPmtsCalcListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
			
			
			
		}
	
	}
	
	
	
	private class HomeButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
			
			
		}
	
	}
	
	
	
	
	
	
	
	
	
	
	
	
	private class ExitButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);			
		}
	
	}
	
	public static void main(String[] args) 
	{
		new FinancialCalc();

	}

}


Reply With Quote
  #2  
Old December 8th, 2012, 07:40 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
The code currently has many classes. Why do you want to make more?

Reply With Quote
  #3  
Old December 8th, 2012, 08:38 AM
cgodfrey987 cgodfrey987 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 27 cgodfrey987 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
The code currently has many classes. Why do you want to make more?


Ok what I meant I guess was that I want to create a new class file so this one file doesn't have everything but that there are a few class files to make it sort of more organized

Reply With Quote
  #4  
Old December 8th, 2012, 09:05 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Have you tried cutting out the classes in the posted code and making them external classes in their own files?

Reply With Quote
  #5  
Old December 8th, 2012, 09:14 AM
cgodfrey987 cgodfrey987 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 27 cgodfrey987 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Have you tried cutting out the classes in the posted code and making them external classes in their own files?


No but I know that part. What I'm wondering is how to call the buttonListeners when they are in another class file?

Reply With Quote
  #6  
Old December 8th, 2012, 09:29 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
how to call the buttonListeners

You use the new statement the same as always to create ("call"???) another class.

Or are you asking how one class (the listener) can access values in another class? Usually listeners are inner classes so that they can have access to the contents of the class that they are in.

Reply With Quote
  #7  
Old December 8th, 2012, 10:15 AM
cgodfrey987 cgodfrey987 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 27 cgodfrey987 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
You use the new statement the same as always to create ("call"???) another class.

Or are you asking how one class (the listener) can access values in another class? Usually listeners are inner classes so that they can have access to the contents of the class that they are in.


Ya I just don't know how to do that call? How do I call it so that when the button is clicked, it calls the buttonListener method from the other class file?

Reply With Quote
  #8  
Old December 8th, 2012, 10:31 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
how to do that call

By "do call" do you mean create an instance of a class?
Or do you mean to call a method in a class?
Your code shouldn't call listener methods. That's done by the java program when an event happens that is being listened for.

Quote:
How do I call it so that when the button is clicked

Use the new statement to create an instance of the Listener class. Pass that reference to the addListener() method.

Reply With Quote
  #9  
Old December 8th, 2012, 11:04 AM
cgodfrey987 cgodfrey987 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 27 cgodfrey987 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
By "do call" do you mean create an instance of a class?
Or do you mean to call a method in a class?
Your code shouldn't call listener methods. That's done by the java program when an event happens that is being listened for.


Use the new statement to create an instance of the Listener class. Pass that reference to the addListener() method.


Any chance you could show me an example from one of my buttons to call one of my listeners pretending it's in a different class file?

Reply With Quote
  #10  
Old December 8th, 2012, 11:13 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
example from one of my buttons to call one of my listeners

You do NOT call a listener. The java program calls the listener when an event happens.

What have you tried so far? Do you get any errors?

There is the following statement in the code:
Code:
   costOfMin.addActionListener(new CostMinButtonListener());

That line should compile with CostMinButtonListener defined in its own class file. There could be a problem compiling that class if that class expects to be an inner class and have access to the enclosing class's values.

Last edited by NormR : December 8th, 2012 at 11:16 AM.

Reply With Quote
  #11  
Old December 8th, 2012, 11:45 AM
cgodfrey987 cgodfrey987 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 27 cgodfrey987 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
You do NOT call a listener. The java program calls the listener when an event happens.

What have you tried so far? Do you get any errors?

There is the following statement in the code:
Code:
   costOfMin.addActionListener(new CostMinButtonListener());

That line should compile with CostMinButtonListener defined in its own class file. There could be a problem compiling that class if that class expects to be an inner class and have access to the enclosing class's values.


Ok but if costMinButtonListener is in a different class file, will it still be able to access it?

Reply With Quote
  #12  
Old December 8th, 2012, 11:48 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Yes. That is how most classes are defined. Each class can be defined in its own file.
If the class file is on the classpath, the compiler will find it.

Try it and see what happens.
The problems will be with the inner class expectation of getting at the enclosing class's values.

Reply With Quote
  #13  
Old December 8th, 2012, 11:50 AM
cgodfrey987 cgodfrey987 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 27 cgodfrey987 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Yes. That is how most classes are defined. Each class can be defined in its own file.
If the class file is on the classpath, the compiler will find it.

Try it and see what happens.
The problems will be with the inner class expectation of getting at the enclosing class's values.


Okay thanks. I will let you know what happens! Thanks for all your help

Reply With Quote
  #14  
Old December 9th, 2012, 02:01 PM
cgodfrey987 cgodfrey987 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 27 cgodfrey987 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by cgodfrey987
Okay thanks. I will let you know what happens! Thanks for all your help


Ok, here is what I have but for example my "autoPmt" button in "buildWelcomeMenu" isn't linking to the "AutoButtonListener" in my other class file (AutoCalc). Do you know how I write the code to link them?

Code:
public class FinancialCalc extends JFrame
{
	double balance, minimumPayment;
	String payment, fixedPayment;
	private JPanel welcomePanel, greetingPanel, buttonPanel, minPmtButtonPanel, minPmtPanel;
	private JLabel message, Amt, Term, Rate, minPmt, fixedPmt;
	//private JLabel autoAmtArray[], autoRateArray[], autoTermArray[];
	private JTextField AmtF, TermF, RateF, minPmtF, fixedPmtF;
	private JButton autoPmt, mortPmt, homeAfford, costOfMin, exit, minPmtButton, calcPmts;
	private JFrame mainFrame, mortPmtFrame, minPmtFrame;//, autoPmtFrame;
	private Dimension d = new Dimension(400,400);
	private DecimalFormat dlr = new DecimalFormat("#0.00");
	private DecimalFormat pct = new DecimalFormat("#0");
	AutoCalc autoPanel = new AutoCalc();
	//private JCheckBox add;
	
	public FinancialCalc()
	{
		
		mainFrame = new JFrame("Financial Calculator Application");		
		
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
		
		mainFrame.setPreferredSize(d);
		mainFrame.setLayout(new BorderLayout());
		mainFrame.setLocationRelativeTo(null);
		
		//buildMenuBar();
		buildWelcomeMenu();
		
		mainFrame.add(greetingPanel, BorderLayout.NORTH);
		mainFrame.add(welcomePanel, BorderLayout.CENTER);
		mainFrame.add(buttonPanel, BorderLayout.SOUTH);		
		
		mainFrame.pack();
		mainFrame.setVisible(true);
	}
		
	
	public void buildWelcomeMenu()
	{
		welcomePanel = new JPanel();
		welcomePanel.setLayout(new GridLayout(4,1));
		welcomePanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));	
		
		autoPmt = new JButton("Auto loan payment calculator");
		autoPmt.setMnemonic(KeyEvent.VK_A);
		autoPmt.addActionListener(new AutoButtonListener());
		
		
		mortPmt = new JButton("Mortgage payment calculator");
		mortPmt.setMnemonic(KeyEvent.VK_M);
		mortPmt.addActionListener(new MortButtonListener());
		
		homeAfford = new JButton("How much home can you afford?");
		homeAfford.setMnemonic(KeyEvent.VK_H);
		homeAfford.addActionListener(new HomeButtonListener());
		
		costOfMin = new JButton("True cost of paying the minimum payment");
		costOfMin.setMnemonic(KeyEvent.VK_C);
		costOfMin.addActionListener(new CostMinButtonListener());
		
		buttonPanel = new JPanel();
		exit = new JButton("Exit");
		exit.setMnemonic(KeyEvent.VK_E);
		exit.addActionListener(new ExitButtonListener());
		buttonPanel.add(exit);
		
		welcomePanel.add(autoPmt);
		welcomePanel.add(mortPmt);
		welcomePanel.add(homeAfford);
		welcomePanel.add(costOfMin);
	
		
		greetingPanel = new JPanel();
		message = new JLabel("Welcome to the Financial Calculations Application");
		greetingPanel.add(message);
		
		
	}


// THE BEGINNING OF MY AUTOCALC CLASS FILE


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.border.EmptyBorder;


public class AutoCalc
{
	private JLabel Amt, Term, Rate;
	private JTextField AmtF, TermF, RateF;//, minPmtF, fixedPmtF;
	private JFrame autoPmtFrame;
	private DecimalFormat dlr = new DecimalFormat("#0.00");
	private DecimalFormat pct = new DecimalFormat("#0");
	private Dimension d = new Dimension(400,400);

	

	private class AutoButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			JPanel autoPmtPanel, autoButtonPanel;
			autoPmtFrame = new JFrame();
			autoPmtFrame = new JFrame("Auto Payment Calculator");			
			autoPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			autoPmtFrame.setLayout(new GridLayout(2,1));
			autoPmtFrame.setPreferredSize(d);
			autoPmtFrame.setLocationRelativeTo(null);
			
			autoPmtPanel = new JPanel();
			autoPmtPanel.setLayout(new GridLayout(4,2));
			autoPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
			
			
			autoButtonPanel = new JPanel();
			//add = new JCheckBox("Compare another loan");
			//add.addActionListener(new AddButtonListener());
			
						
			JButton calculate = new JButton("Calculate");
			JButton exit = new JButton("Exit");
			calculate.addActionListener(new AutoCalcButtonListener());
			exit.addActionListener(new ExitButtonListener());
			
			Amt = new JLabel("Loan Amount");
			Term = new JLabel("Term in months");
			Rate = new JLabel("Interest Rate");
			
			
			AmtF = new JTextField(5);
			TermF = new JTextField(5);
			RateF = new JTextField(5);
			
			autoPmtPanel.add(Amt);
			autoPmtPanel.add(AmtF);
			autoPmtPanel.add(Term);
			autoPmtPanel.add(TermF);
			autoPmtPanel.add(Rate);
			autoPmtPanel.add(RateF);
			
			autoButtonPanel.add(calculate);
			autoButtonPanel.add(exit);

			autoPmtFrame.add(autoPmtPanel);
			autoPmtFrame.add(autoButtonPanel);
			
			autoPmtFrame.pack();
			autoPmtFrame.setVisible(true);
			
		}
	
	}
	
	private class AutoCalcButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
			String str;
			double amt, n, rate, denominator;
			double monthlyPmt, totalInt, totalAmtPaid;
			
			str = AmtF.getText();
			amt = Double.parseDouble(str);
			
			str = TermF.getText();
			n = Double.parseDouble(str);
			
			str = RateF.getText();
			rate = Double.parseDouble(str);
			
			rate = rate / 100 / 12;
			denominator = 1- Math.pow(1 + rate, -n);
			
			monthlyPmt = amt * (rate / denominator);
			
			totalAmtPaid = monthlyPmt * n;
			
			totalInt = totalAmtPaid - amt;
			
			JOptionPane.showMessageDialog(null, "Monthly Payment is $" + dlr.format(monthlyPmt) + "\n\n" + 
					"Total Amount Paid over " + pct.format(n) + " months: $" + dlr.format(totalAmtPaid) + "\n\n" +
					"Total Interest Paid: $" + dlr.format(totalInt), 
					"Total", JOptionPane.PLAIN_MESSAGE);
			
			
		}
	}
	
	
	private class ExitButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);			
		}
	
	}
	
	
	
	
}


Reply With Quote
  #15  
Old December 9th, 2012, 02:26 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
linking to the "AutoButtonListener" in my other class file (AutoCalc).

Is the AutoButtonListener class public and external? If so the new statement should be create an instance of it.

The posted code shows it private and internal.
You could write a method that creates an instance of the class and returns it.


Please post the full text of the error messages.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Help with separating into multiple classes

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap