Have to create java program account management program using a MVC pattern.I kin
ID: 3554491 • Letter: H
Question
Have to create java program account management program using a MVC pattern.I kind of stuck it to do coding it. system suppose to ask me amount of deposit and widraw, and then I suppose to enter the amount of deposit and widraw money and updated it .
supposed print out id, name,updated balance.also make transfer option that balance transfer euro or yuan .I have to evaluate 6 people. but i dont have idea to evaluate multiple people account. please help me ~
MODEL
package account.model;
public class AccountModel {
private final double euro = 0.72;
private final double yuan = 6.2;
private String id;
private String name;
private double balance;
public AccountModel(String id, String name, double balance) {
super();
this.id = id;
this.name = name;
this.balance = balance;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double deposit(double amount)
{
balance= balance+amount;
return balance;
}
public double withdraw(double amount)
{
balance= balance-amount;
return balance;
}
public double editin_euro()
{
balance = balance * euro;
return balance;
}
public double editin_yuan()
{
balance = balance / yuan;
return balance;
}
public double getBalance()
{
return balance;
}
}
VIew
package account.model;
public class AccountView {
public void Accountview(String id,String name, Double money) {
System.out.println("ID: "+ id);
System.out.println("Name: " + name);
System.out.println("Balance: " + money);
}
}
Control
package account.model;
public class AccountController {
private AccountModel model;
private AccountView view;
public AccountController(AccountModel model, AccountView view) {
this.model = model;
this.view = view;
}
public void setaccountName(String name) {
model.setName(name);
}
public String getaccountName() {
return model.getName();
}
public void setaccountid(String id) {
model.setId(id);
}
public String id() {
return model.getId();
}
public Double balance()
{
return model.getBalance();
}
public void updateView() {
view.Accountview(model.getName(), model.getId(),model.getBalance());
}
}
TEst
package account.model;
public class MVCPatternTest {
public static void main(String[] args) {
AccountModel model = retriveaccountFromDB();
AccountView view = new AccountView();
AccountController controller = new AccountController(model, view);
System.out.println("Initially... ======================");
controller.updateView();
System.out.println(" After Deposit... ======================");
model.deposit(1234589.0);
controller.updateView();
System.out.println(" After Withdrawal... ======================");
model.withdraw(12345.0);
controller.updateView();
System.out.println(" Balance in Euro... =======================");
model.editin_euro();
controller.updateView();
System.out.println(" Balance in Yuan... ======================");
model.editin_yuan();
controller.updateView();
}
private static AccountModel retriveaccountFromDB() {
AccountModel account= new AccountModel("1070","Strreter Angela M",34.4360);
account.getBalance();
return account;
}
}
Explanation / Answer
Put the accounts into an array list with a hashmap for example :
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.util.StringUtils;
public class StubAccountManager implements AccountManager {
private final Map<String, Account> accounts;
private final AtomicInteger nextNumber = new AtomicInteger(3);
public StubAccountManager() {
this.accounts = new HashMap<String, Account>();
addAccount("a1", "Jay");
addAccount("a2", "Jason");
addAccount("a3", "Julie");
}
private void addAccount(String number, String name) {
this.accounts.put(number, new Account(number, name));
}
@Override
public List<Account> getAccounts() {
return new ArrayList<Account>(this.accounts.values());
}
@Override
public Account getAccount(String number) {
Account account = this.accounts.get(number);
if (account == null) {
throw new AccountNotFoundException(number);
}
return account;
}
@Override
public void saveOrUpdate(Account account) {
if (!StringUtils.hasText(account.getNumber())) {
account.setNumber(getNextNumber());
}
this.accounts.put(account.getNumber(), account);
}
private String getNextNumber() {
return "a" + this.nextNumber.incrementAndGet();
}
@Override
public void delete(Account account) {
this.accounts.remove(account.getNumber());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.