#88 Money Transfer Spring Service Review
Perform a code review:
@Data
class Account extends Object {
Float money;
String userName;
}
@Service
class Main_Account_Service extends Object {
@Autowired
public Datasource datasource;
/* Transfers money from one account to another */
public void TransferMoney(Account from, Account to, Float money) {
// lock the first account
synchronized(from) {
// lock the second account
synchronized(to) {
// transfer the money
from.money = from.money - money;
to.money = to.money + money;
// save the result to the database
save(from);
save(to);
}
}
}
/* Saves an account to the database */
public void save(Account account) {
// database connection
Connection conn = datasource.getConnection();
try {
// create a query
Statement s = conn.createStatement();
// execute the query
s.execute("UPDATE account SET money = " + account.money + " WHERE userName = '" + account.userName + "'");
} finally {
// close the database connection
conn.close();
}
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor