For some reason, whenever I input one of the set colours in my code, it gives me an error. Can anyone see what the problem is here
This is my app class:
import javax.swing.JOptionPane;
public class binCollectionAPP{
public static void main(String[] args){
//Declare Variables
int apartmentNum, flag;
String collect, binColour;
//Objects
binCollection bin = new binCollection();
//Inputs
apartmentNum=Integer.parseInt(JOptionPane.showInputDialog("Enter your apartment number"));
binColour=JOptionPane.showInputDialog("Enter the colour bin you are checking");
//set
bin.setApartNum(apartmentNum);
bin.setBinColour(binColour);
//compute
bin.compute();
//get
collect=bin.getCollectDay();
flag=bin.getFlag();
//output
if(flag == 1){
JOptionPane.showMessageDialog(null,"Error, incorrect bin colour");
}
else if(flag == 2){
JOptionPane.showMessageDialog(null,"Error, incorrect apartment number");
}
else{
JOptionPane.showMessageDialog(null,"Your collection day is "+collect);
}
}
}
And my instantiable class is:
public class binCollection{
//Declare Vars
private int apartmentNum, flag;
private String collect, binColour;
//Constructor
public binCollection(){
apartmentNum = 0;
flag = 0;
collect = "";
binColour = "";
}
//set methods
public void setApartNum(int apartmentNum){
this.apartmentNum = apartmentNum;
}
public void setBinColour(String binColour){
this.binColour = binColour;
}
//compute
public void compute(){
if(apartmentNum % 2 == 0){
if(binColour == "brown" || binColour == "Brown" ){
collect = "Monday";
}
else if(binColour == "black"){
collect = "Tuesday";
}
else if(binColour == "green"){
collect = "Wednesday";
}
else{
flag = 1;
}
}
else if(apartmentNum % 2 == 1){
if(binColour == "brown"){
collect = "Tuesday";
}
else if(binColour == "black"){
collect = "Wednesday";
}
else if(binColour == "green"){
collect = "Thursday";
}
else{
flag = 1;
}
}
else{
flag = 2;
}
}
//get methods
public String getCollectDay(){
return collect;
}
public int getFlag(){
return flag;
}
}
Can anyone see where I am going wrong?
Aucun commentaire:
Enregistrer un commentaire