Skip to content

Commit

Permalink
🔀 Merge pull request #13 from AntoineJT/develop
Browse files Browse the repository at this point in the history
🔖 Release v0.6.0
  • Loading branch information
AntoineJT authored Nov 24, 2019
2 parents 5e3d352 + fbdf44a commit 5709748
Show file tree
Hide file tree
Showing 22 changed files with 376 additions and 342 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Gradle stuff
.gradle/
build/
!gradle/wrapper/gradle-wrapper.jar

# A folder I use to keep a copy of each build I provide
versions/
36 changes: 22 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# Just Another Stack Calculator
*Un projet de SLAM2 de fin de 1ère année de SIO, coeff 3*
# 🧮 Just Another Stack Calculator

*This is a school project.*

It's a calculator which uses a Stack to work. To use it properly, just use the help command.
🏫 *This is a school project ("SLAM2" end of the year project from first year of "BTS SIO").*

You can get updates on [my repository](https://github.com/AntoineJT/jasc/).

## Purpose
## 🥅 Purpose

The purpose is to create a calculator using a stack to make calculation and provide a command line interface (CLI), and a graphic one (GUI) (not implemented yet).

To use it properly, just use the help command.

Create a calculator using a stack to make calculation and provide a Console User Interface, and a Graphic one (not implemented yet).
**N.B.** : *At the time, this project is a showcase to present my skills.*

## Modifying this software
## ✍️ Modifying this software

If you want to modify my software, please fork it. It will show me that this is useful and not a random project on my GitHub account.

## License
## 📄 License

Copyright (c) Antoine James Tournepiche. All rights reserved.

Expand All @@ -24,12 +25,19 @@ Licensed under the [MIT License](https://github.com/AntoineJT/jasc/blob/master/L
This software is provided with the MIT License which means that you need to credit me for my work if you use this, if it's a modified version or not.
If you modify my software and share it, you must credit me as the author of the original software, put the github webpage of the original software, and credit you as the author of the derivated work based on it.

## How to thank me for my work

## 📌 Credits

This repository follows :
- [Semantic Versioning](https://semver.org/)
- [gitmoji](https://gitmoji.carloscuesta.me/)

## 👍 How to thank me for my work

Just :
- put a star if you use it (the more it has stars, the more I will work on it)
- fork it if you want to help me (by patching some issues, etc.)
- fork it if you want to create a derivative work based on it
- open issues if you encounter some bugs, have some ideas, etc.
- ⭐️ put a star if you use it (the more it has stars, the more I will work on it)
- 🔀 fork it if you want to help me (by patching some issues, etc.)
- 🔀 fork it if you want to create a derivative work based on it
- 🔍open issues if you encounter some bugs, have some ideas, etc.

Antoine James Tournepiche
16 changes: 14 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ plugins {

group 'com.github.antoinejt'

def lastUpdate = 'October 10th 2019'
def lastUpdate = 'November 3rd 2019'

def majorVersionNumber = '0'
def minorVersionNumber = '5'
def minorVersionNumber = '6'
def patchVersionNumber = '0'

def versionNumber = "${majorVersionNumber}.${minorVersionNumber}.${patchVersionNumber}"
def internalVersionNumber = "${versionNumber}${versioning.info.branch != 'master' ? '-DEVELOP' : ''}"

version "${versionNumber}-${versioning.info.full}"

def jarName = "${jar.baseName}-${rootProject.version}.jar"
def runScript = "java -jar ${jarName}"

sourceCompatibility = 1.8

jar {
Expand All @@ -34,6 +37,15 @@ jar {
'Build-OS': "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
)
}

doLast {
new File('build/libs', 'run.bat').text = runScript
new File('build/libs', 'run.sh').text = runScript
}
}

task run(dependsOn: ':build', type: Exec) {
commandLine 'java', '-jar', "build/libs/${jarName}"
}

repositories {
Expand Down
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
198 changes: 114 additions & 84 deletions src/main/java/com/github/antoinejt/jasc/ConsoleUI.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,65 @@
/*
* MIT License
*
* Copyright (c) 2019 Antoine James Tournepiche
*
* This source file come from Just another Stack Calculator
* Repository : https://github.com/AntoineJT/jasc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.antoinejt.jasc;

import com.github.antoinejt.jasc.calculator.CalculatorEngine;
import com.github.antoinejt.jasc.calculator.FunctionType;
import com.github.antoinejt.jasc.calculator.OperationType;
import com.github.antoinejt.jasc.util.HashMapBuilder;
import com.github.antoinejt.jasc.util.TextFormatter;

import java.util.*;

class ConsoleUI {
private static final Map<String, FunctionType> functions = Collections.unmodifiableMap(new HashMap<String, FunctionType>() {{
put("sqrt", FunctionType.SQRT);
put("log", FunctionType.LOG10);
put("ln", FunctionType.LN);
put("lb", FunctionType.LOGB);
put("cos", FunctionType.COS);
put("sin", FunctionType.SIN);
put("tan", FunctionType.TAN);
put("arccos", FunctionType.ARCCOS);
put("arcsin", FunctionType.ARCSIN);
put("arctan", FunctionType.ARCTAN);
put("exp", FunctionType.EXP);
}});
private static final Map<String, OperationType> operators = Collections.unmodifiableMap(new HashMap<String, OperationType>() {{
put("+", OperationType.ADDITION);
put("-", OperationType.SUBSTRACTION);
put("*", OperationType.MULTIPLICATION);
put("/", OperationType.DIVISION);
put("%", OperationType.MODULO);
put("^", OperationType.POWER);
}});
private static final List<String> commands = Collections.unmodifiableList(new ArrayList<String>() {{
addAll(functions.keySet()); // functions are added here
addAll(operators.keySet()); // operators are added here
addAll(Arrays.asList("=", "help", "clear", "pop", "quit")); // Commands
}});

// TODO Replace that by some txt templates (use of MVC
private static Map<String, FunctionType> functions = new HashMapBuilder<String, FunctionType>()
.put("sqrt", FunctionType.SQRT)
.put("log", FunctionType.LOG10)
.put("ln", FunctionType.LN)
.put("lb", FunctionType.LOGB)
.put("cos", FunctionType.COS)
.put("sin", FunctionType.SIN)
.put("tan", FunctionType.TAN)
.put("arccos", FunctionType.ARCCOS)
.put("arcsin", FunctionType.ARCSIN)
.put("arctan", FunctionType.ARCTAN)
.put("exp", FunctionType.EXP).build();
private static Map<String, OperationType> operators = new HashMapBuilder<String, OperationType>()
.put("+", OperationType.ADDITION)
.put("-", OperationType.SUBSTRACTION)
.put("*", OperationType.MULTIPLICATION)
.put("/", OperationType.DIVISION)
.put("%", OperationType.MODULO)
.put("^", OperationType.POWER).build();
private static final Set<String> commands = new HashSet<>(
Arrays.asList("=", "help", "clear", "pop", "quit")
);

// TODO Replace that by some txt templates (use of MVC)
private static void displayHelp() {
TextFormatter.listThings("Available operators (acts on 2 operands) : ",
"+ : Addition operator",
Expand Down Expand Up @@ -81,75 +105,81 @@ private static void displayIntro() {

private static void printStackContent(CalculatorEngine calculatorEngine) {
List stackContent = calculatorEngine.getNumbers();
int stackContentSize = stackContent.size();

if (stackContentSize > 0) {
if (stackContent.size() > 0) {
stackContent.forEach(System.out::println);
return;
}
System.err.println("Stack is empty!");
}

// TODO Refactor it!
static void useConsole() throws Exception {
private static void parseInput(CalculatorEngine calculatorEngine, String input) {
if (operators.containsKey(input)) {
tryToApplyOperation(calculatorEngine, input);
return;
}
if (functions.containsKey(input)) {
FunctionType functionType = functions.get(input);
calculatorEngine.applyFunction(functionType);
return;
}
if (commands.contains(input)) {
executeCommand(calculatorEngine, input);
return;
}

tryToAddNumberToTheStack(calculatorEngine, input);
}

private static void executeCommand(CalculatorEngine calculatorEngine, String input) {
switch (input) {
case "=":
printStackContent(calculatorEngine);
break;
case "help":
displayHelp();
break;
case "clear":
calculatorEngine.clear();
break;
case "pop":
calculatorEngine.removeLastNumber();
break;
case "quit":
System.exit(0);
break;
default: break;
}
}

private static void tryToApplyOperation(CalculatorEngine calculatorEngine, String input) {
try {
OperationType operationType = operators.get(input);
calculatorEngine.applyOperation(operationType);
} catch (IllegalStateException unused) {
System.err.println("You need to specify at least 2 operands before you can make some calculation!");
}
}

private static void tryToAddNumberToTheStack(CalculatorEngine calculatorEngine, String input) {
try {
float number = Float.parseFloat(input);
calculatorEngine.addNumber(number);
} catch (NumberFormatException unused) {
System.err.println("Your input is invalid!");
}
}

@SuppressWarnings({"InfiniteLoopStatement", "WeakerAccess"})
public static void useConsole() throws UnsupportedOperationException {
displayIntro();

CalculatorEngine calculatorEngine = new CalculatorEngine();
Scanner scanner = new Scanner(System.in);
String input;

while (true) {
input = scanner.next();
if (!commands.contains(input)) { // if it's a number
try {
float number = Float.parseFloat(input);

calculatorEngine.addNumber(number);
} catch (NumberFormatException unused) {
System.err.println("Your input is invalid!");
}
continue;
}
if (operators.containsKey(input)) {
try {
OperationType operationType = operators.get(input);

calculatorEngine.applyOperation(operationType);
} catch (IllegalStateException unused) {
System.err.println("You need to specify at least 2 operands before you can make some calculation!");
}
continue;
}

boolean isFunction = false;

switch (input) {
case "=":
printStackContent(calculatorEngine);
break;
case "help":
displayHelp();
break;
case "clear":
calculatorEngine.clear();
break;
case "pop":
calculatorEngine.removeLastNumber();
break;
case "quit":
System.exit(0);
default:
isFunction = true;
}
if (isFunction) {
if (functions.containsKey(input)) {
FunctionType functionType = functions.get(input);

calculatorEngine.applyFunction(functionType);
continue;
}
throw new Exception("This is not good to corrupt my list, hack3rm4n!");
}
String input = scanner.next();
parseInput(calculatorEngine, input);
}
}
}
29 changes: 28 additions & 1 deletion src/main/java/com/github/antoinejt/jasc/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
/*
* MIT License
*
* Copyright (c) 2019 Antoine James Tournepiche
*
* This source file come from Just another Stack Calculator
* Repository : https://github.com/AntoineJT/jasc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.antoinejt.jasc;

public class Main {
public static void main(String[] args) {
try {
ConsoleUI.useConsole();
} catch (Exception exception) {
} catch (UnsupportedOperationException exception) {
exception.printStackTrace();
}
}
Expand Down
Loading

0 comments on commit 5709748

Please sign in to comment.