package CalculatorImplementation;
public class Calculator {
private String brand;
private String[] pressButton;
private int pressButtonIndex;
public Calculator(){
pressButton = new String[100];
pressButtonIndex = 0;
}
public void setBrand(String brand){
this.brand = brand;
}
public String getBrand(){
return brand;
}
public void pressOne(){
pressButton[pressButtonIndex] = "1";
pressButtonIndex++;
System.out.println("按了数字1");
}
public void pressTwo(){
pressButton[pressButtonIndex] = "2";
pressButtonIndex++;
System.out.println("按了数字2");
}
public void pressThree(){
pressButton[pressButtonIndex] = "3";
pressButtonIndex++;
System.out.println("按了数字3");
}
public void pressFour(){
pressButton[pressButtonIndex] = "4";
pressButtonIndex++;
System.out.println("按了数字4");
}
public void pressFive(){
pressButton[pressButtonIndex] = "5";
pressButtonIndex++;
System.out.println("按了数字5");
}public void pressSix(){
pressButton[pressButtonIndex] = "6";
pressButtonIndex++;
System.out.println("按了数字6");
}public void pressSeven(){
pressButton[pressButtonIndex] = "7";
pressButtonIndex++;
System.out.println("按了数字7");
}public void pressEight(){
pressButton[pressButtonIndex] = "8";
pressButtonIndex++;
System.out.println("按了数字8");
}public void pressNine(){
pressButton[pressButtonIndex] = "9";
pressButtonIndex++;
System.out.println("按了数字9");
}
public void pressAdd(){
pressButton[pressButtonIndex] = "+";
pressButtonIndex++;
System.out.println("按了+");
}
public void pressSubtract(){
pressButton[pressButtonIndex] = "-";
pressButtonIndex++;
System.out.println("按了-");
}
public void pressMultiply(){
pressButton[pressButtonIndex] = "*";
pressButtonIndex++;
System.out.println("按了*");
}
public void pressDivide(){
pressButton[pressButtonIndex] = "/";
pressButtonIndex++;
System.out.println("按了/");
}
public int pressEquals(){
int result = 0;
String flag = null;
for(int i = 0;i < pressButtonIndex;i++){
if(!pressButton[i].equals("+") && !pressButton[i].equals("-") && !pressButton[i].equals("*") && !pressButton[i].equals("/")){
if(result == 0){
result = Integer.parseInt(pressButton[i]);
}else{
if(flag.equals("+")){
result = result + Integer.parseInt(pressButton[i]);
}else if(flag.equals("-")){
result = result - Integer.parseInt(pressButton[i]);
}else if(flag.equals("*")){
result = result * Integer.parseInt(pressButton[i]);
}else if(flag.equals("/")){
result = result / Integer.parseInt(pressButton[i]);
}
}
}else{
if(pressButton[i].equals("+")) {
flag = "+";
}else if(pressButton[i].equals("-")){
flag = "-";
}else if(pressButton[i].equals("*")){
flag = "*";
}else if(pressButton[i].equals("/")){
flag = "/";
}
}
}
return result;
}
}