This parts is really making me confused. I can't get values from memType as it is always in null and the arrayIndexOutOfBounds when I click on the calculate button twice. It also empty the column when i clicked the second time. Anybody can help me with this please ? Have been coding for 2 days almost non-stop.
UI:
//Calculate
else if(e.getSource() == jbtCalculate)
{ memControl = new MemberControl();
Member member = new Member();
JPanel confirmPanel = new JPanel();
JPanel cp1 = new JPanel();
JPanel cp2 = new JPanel();
int RowCount = table.getRowCount();
double discount = 0;
double transaction;
double discountPromo = 0;
double totaltrans = 0 ;
double totaltranswithDisc =0;
String pID = jTF3.getText();
ArrayList<Purchase> purchaseList = purcControl.selectRecord(pID);
String memTypeCheck = jTF4.getText();
memControl.selectRecord(memTypeCheck);
String memType = String.valueOf(member.getMemType());
System.out.println(memType);
if(memType.equals("Gold"))
{
discount = 0.8;
}
else if(memType.equals("Silver"))
{
discount = 0.9;
}
else if(memType.equals("Normal"))
{
discount = 1;
}
try{
tableModel.setColumnCount(4);
tableModel.addColumn("Total");
for (Purchase purchase : purchaseList) {
transaction = purchase.getQuantity()*purchase.getPrice();
tableModel.setValueAt(transaction,purchaseCount , 4);
purchaseCount += 1;
totaltrans += transaction;
}
JOptionPane.showMessageDialog(null, "Discount on member :" +discount +"0\nDiscount on promotion :"+ discountPromo+"0\nCost before discount :"+totaltrans +"0\nTotal transaction:" + totaltranswithDisc+"0");
}
catch(ArrayIndexOutOfBoundsException a){
JOptionPane.showMessageDialog(null, "Index Out Of Bound");
}
Control :
public Member selectRecord(String id) {
return memDA.getRecord(id);
}
Domain :
public Member(MembershipType memType){
this.memType = memType;
}
public Member(String memID,String memName, MembershipType memType, String IC, char gender, String address, String ContactNo, String eMail){
this.memID = memID;
this.memName = memName;
this.memType = memType;
this.IC = IC;
this.gender = gender;
this.address = address;
this.ContactNo = ContactNo;
this.eMail = eMail;
counterID++;
}
public String getMemID(){
return memID;
}
public MembershipType getMemType(){
return memType;
}
public String getMemName(){
return memName;
}
public String getIC(){
return IC;
}
public char getGender(){
return gender;
}
public String getAddress(){
return address;
}
public String getContactNo(){
return ContactNo;
}
public String getEMail(){
return eMail;
}
public String getMemberID(){
return memID;
}
public int getCounterID(){
return counterID;
}
public double getTotalMemberFee(){
return totalMemberFee;
}
public void setTotalMemberFee(double totalMemberFee){
this.totalMemberFee = totalMemberFee;
}
public void setMemID(String memID){
this.memID = memID;
}
public void setMemType(MembershipType memType){
this.memType = memType;
}
public void setMemName(String memName){
this.memName = memName;
}
public void setIC(String IC){
this.IC = IC;
}
public void setGender(char gender){
this.gender = gender;
}
public void setAddress(String address){
this.address = address;
}
public void setContactNo(String ContactNo){
this.ContactNo = ContactNo;
}
public void setEMail(String eMail){
this.eMail = eMail;
}
public void setMemberID(String memberID){
this.memID = memID;
}
public void setCounterID(int counterID){
this.counterID = counterID;
}
}
DA:
public Member getRecord(String id) {
String queryStr = "SELECT * FROM " + tableName + " WHERE memberID = ?";
Member member = null;
try {
stmt = conn.prepareStatement(queryStr);
stmt.setString(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
MembershipType membershipType = memTypeDA.getRecord(rs.getString("memberType"));
member = new Member(id,rs.getString("memberName"),membershipType,rs.getString("memberIC"),rs.getString("gender").charAt(0),rs.getString("memberAddress"), rs.getString("ContactNo"),rs.getString("email"));
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
return member;
}
Edit: This is the part where I got my error of arrayOutOfBound. (from UI)
try{
tableModel.setColumnCount(4);
tableModel.addColumn("Total");
for (Purchase purchase : purchaseList) {
transaction = purchase.getQuantity()*purchase.getPrice();
tableModel.setValueAt(transaction,purchaseCount , 4);
purchaseCount += 1;
totaltrans += transaction;
}
JOptionPane.showMessageDialog(null, "Discount on member :" +discount +"0\nDiscount on promotion :"+ discountPromo+"0\nCost before discount :"+totaltrans +"0\nTotal transaction:" + totaltranswithDisc+"0");
}
catch(ArrayIndexOutOfBoundsException a){
JOptionPane.showMessageDialog(null, "Index Out Of Bound");
}
jTF5.setText(String.valueOf(totaltrans) + "0"); //EDIT
jTF5.setEditable(false); //EDIT
Null value has been solved. I added two statement.
Member mem = memControl.selectRecord(memTypeCheck);
String memType = mem.getMemType().getMemberType();
So actually is my silly mistake for null value.