Hi I am trying to create a generic class form existing code I have tried to figure it out but with no avail if anyone could help it would be much appreciated I have tried to do it and this is what I have so far
Here is the Class
public class OpAdd<S> implements Operation<S>{
public OpAdd() {}
public String getOpsName() {
return " plus ";
}
public S operate(S original) {
char previousChar ;
String or = original.toString();
String org = or.replaceAll("\\s+","");;
or = or.replaceAll("\\s+","");
for(int i =0 ; i < or.length() ; i++ ){
previousChar = org.charAt(i);
or = replaceCharAt(or, i, or.charAt(or.length()-1));
or = replaceCharAt(or, or.length()-1, previousChar);
}
return (S) or;
}
public static String replaceCharAt(String s, int pos, char c) {
return s.substring(0, pos) + c + s.substring(pos + 1);
}
}
Here is the Interface
public interface Operation<S> {
S operate(S original);
String getOpsName();
}
Here is the main program
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFrame;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CalculateSpring {
private Operation ops;
private ResultWriter wtr;
private static JPanel p;
private JButton Run;
public static JTextField org,encrpty;
public JLabel blank;
public void setOps(Operation ops) {
this.ops = ops;
}
public void setWriter(ResultWriter wtr) {
this.wtr = wtr;
}
public static void main(String[] args) {
String[] args2 = {"3001", "3"};
ApplicationContext context =
new ClassPathXmlApplicationContext(
"beans.xml");
BeanFactory factory = (BeanFactory) context;
CalculateSpring calc =
(CalculateSpring) factory.getBean("opsbean");
calc.execute(args2);
}
public void execute(String [] args) {
//String[] args2 = {"3001", "3"};
JFrame frame = new JFrame();
p = new JPanel();
Run = new JButton("ENCRYPT");
org = new JTextField(30);
encrpty = new JTextField(30);
blank = new JLabel(" ");
Run.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("Click!");
String op1 = org.getText();
String op2 = encrpty.getText();
wtr.showResult(ops.operate(op1) + "!");
}
});
p.add(org);
p.add(encrpty);
p.add(blank);
p.add(Run);
frame.add(p);
frame.pack();
frame.setSize(500, 600);
frame.show();
}
}
Any Help would be great thanks a million