Skip to content

Conversation

@nkozlova
Copy link
Contributor

Only calculator

@DanAnastasyev DanAnastasyev self-assigned this Dec 18, 2016
@DanAnastasyev
Copy link
Collaborator

А кроме калькулятора что-нибудь будет?

Калькулятор не очень похож на тот, который требуется в задании
Во-первых, должна быть обработка переменных:

a = 10
b = 20
c = 10
volume = a * b * c
amount_of_substance(V, nu) = V / nu

amount_of_substance(volume, 12) -> ?

Нужно разбирать строки с переменными и как-то хранить их результаты.

Во-вторых, ты не можешь себе позволить разбирать строки с выражениями - как для переменных, так и для функций - каждый раз. Кроме переменных нужно хранить и разбор функций в том виде, в котором их значение можно максимально быстро посчитать. В стандартном подходе это выглядит как-нибудь так

В-третьих, обрати внимание, что хочется уметь вычислять выражение функции, которая зависит от функции, вообще говоря:

sqr(x) = x * x // этой функции нет в стандартных
F(m1, m2, r) = G * m1 * m2 / sqr(r)

Если правильно реализовать хранение, это не будет слишком сложно

jdbcTemplate.update("INSERT INTO billing.variables VALUES (?, ?, ?, ?)",
new Object[]{userName, name, value, expression});
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вообще есть merge. C ним можно обновлять существующие значения и добавлять новые - не нужно будет удалять

return jdbcTemplate.queryForObject(
"SELECT username, password, enabled FROM billing.users WHERE username = ?",
new Object[]{userName},
new RowMapper<BillingUser>() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Анонимные классы с единственным методом нынче заменяют на лямбда функции обычно. Получается компактнее и (по крайней мере, когда привыкнешь) читабельнее

return false;
} catch (EmptyResultDataAccessException e) {
jdbcTemplate.update("INSERT INTO billing.users VALUES (?, ?, ?)",
new Object[]{userName, password, enabled});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут можно было не создавать массив, просто передать все аргументы:
jdbcTemplate.update("INSERT INTO billing.users VALUES (?, ?, ?)", userName, password, enabled);

.csrf().disable()
.authorizeRequests()
.antMatchers("/eval/**").authenticated()
.antMatchers("/var/**").authenticated()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Судя по всему, "/variable/**" и "/function/**" должны быть

<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лишняя зависимость. У меня из-за неё не собиралось

import static java.lang.Character.isLetter;

@RestController
public class CalculatorController {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно весь контроллер пометить @RequestMapping(produces = "text/plain") и не писать во всех остальных методах


public Function getFunction(String userName, String function) {
return jdbcTemplate.queryForObject(
"SELECT userName, name, arguments, expression FROM billing.functions WHERE userName = ? AND name = ?",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А где ты создаешь billing.functions?

@RequestMapping(path = "/function/{functionName}", method = RequestMethod.PUT, consumes = "text/plain",
produces = "text/plain")
public void putFunction(Authentication authentication, @PathVariable String functionName,
@RequestParam(value = "arguments", defaultValue = "") Array arguments,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почему Array? Оно реально работает? У меня не захотело

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants