我目前正在为网上银行创建一个网络服务,但我的提款方法似乎并没有起作用。提款应该采用指定帐户ID的当前余额列中指定的金额。
这是我的代码......
Accounts.java:
package com.banking.entity;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import static javax.persistence.CascadeType.ALL;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "accounts")
@Entity
@Table(name = "accounts")
public class Account implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@NotNull
@Column(name = "Account_ID")
private int Account_ID;
@Column(name = "Sort_Code")
private int Sort_Code;
@Column(name = "Account_No")
private int Account_No;
@Column(name = "Current_Balance")
private double Current_Balance;
@Column(name = "Customer_ID")
private int Customer_ID;
@OneToMany(fetch=FetchType.EAGER)
private Collection<Transaction> transaction = new ArrayList<Transaction>();
public Collection<Transaction> getTransactions() {
return transaction;
}
public void setTransactions(Collection<Transaction> transaction) {
this.transaction = transaction;
}
@XmlAttribute
public int getCustomer_ID() {
return Customer_ID;
}
public void setCustomer_ID(int Customer_ID) {
this.Customer_ID = Customer_ID;
}
@XmlAttribute
public int getId() {
return Account_ID;
}
public void setId(int Account_ID) {
this.Account_ID = Account_ID;
}
@XmlAttribute
public int getSortCode() {
return Sort_Code;
}
public void setSortCode(int Sort_Code) {
this.Sort_Code = Sort_Code;
}
@XmlAttribute
public int getAccountNo() {
return Account_No;
}
public void setAccountNo(int Account_No) {
this.Account_No = Account_No;
}
@XmlAttribute
public double getCurrentBalance() {
return Current_Balance;
}
public void setCurrentBalance(double Current_Balance) {
this.Current_Balance = Current_Balance;
}
@Override
public String toString() {
return "Account{" + "Account_ID=" + Account_ID + ", Sort_Code=" + Sort_Code + ", Account_No=" + Account_No + ", Current_Balance=" + Current_Balance + ", Customer_ID=" + Customer_ID + '}';
}
}
AccountsResource.java:
package com.banking.resource;
import com.banking.entity.Account;
import com.banking.entity.Customer;
import com.banking.entity.Transaction;
import java.math.BigDecimal;
import java.sql.SQLException;
import static java.util.Collections.singletonList;
import java.util.List;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/accounts")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class AccountResource extends ResourceBase<Account> {
private EntityManagerFactory emf = Persistence.createEntityManagerFactory("Banking");
private EntityManager em = emf.createEntityManager();
private EntityTransaction tx = em.getTransaction();
private List<Account> listAccounts;
@Override
protected List<Account> getMultiQuery() throws NamingException {
em = getEntityManager();
em.getTransaction().begin();
listAccounts = em.createQuery("SELECT e FROM Account e").getResultList();
em.getTransaction().commit();
em.close();
return listAccounts;
}
@Override
protected List getSingleQuery(int id) throws NamingException {
em = getEntityManager();
em.getTransaction().begin();
listAccounts = singletonList(em.find(Account.class, id));
em.getTransaction().commit();
em.close();
return listAccounts;
}
@Override
protected List getAccountBalance(int id) throws NamingException {
em = getEntityManager();
em.getTransaction().begin();
listAccounts = singletonList(em.find(Account.class, id));
Account a = new Account();
a.getCurrentBalance();
em.persist(a);
em.getTransaction().commit();
em.close();
return listAccounts;
}
@Override
protected void createAccount(Account t) throws SQLException, NamingException {
//Account acc = new Account();
em = getEntityManager();
em.getTransaction().begin();
t.setSortCode(t.getSortCode());
t.setAccountNo(t.getAccountNo());
t.setCurrentBalance(t.getCurrentBalance());
t.setCustomer_ID(t.getCustomer_ID());
em.persist(t);
em.getTransaction().commit();
em.close();
}
// public void transfer(Account from, Account to, double amount) throws NamingException {
// em = getEntityManager();
// em.getTransaction().begin();
// Account f = em.find(Account.class, from.getId());
// Account t = em.find(Account.class, to.getId());
// f.getCurrentBalance() - amount;
// t.setCurrentBalance() + amount;
// em.getTransaction().commit();
// em.close();
// }
//@Override
protected Account withdraw(
@PathParam("id") int id,
@PathParam("amount") int amount, Account b) throws NamingException {
em = getEntityManager();
em.getTransaction().begin();
Account test = em.find(Account.class, b.getId());
if (test != null) {
Account a1 = new Account();
double balance = a1.getCurrentBalance();
double bal = balance - amount;
a1.setCurrentBalance(bal);
em.remove(test);
em.persist(a1);
tx.commit();
em.close();
}
return b;
}
@Override
protected void deleteQuery(int id) throws SQLException, NamingException {
Account acc = new Account();
em = getEntityManager();
em.getTransaction().begin();
acc = em.find(Account.class, id);
em.remove(acc);
em.getTransaction().commit();
em.close();
}
@Override
protected void updateQuery(Account t, int id) throws SQLException, NamingException {
//Account acc = new Account();
em = getEntityManager();
em.getTransaction().begin();
t = em.find(Account.class, id);
t.setSortCode(t.getSortCode());
t.setAccountNo(t.getAccountNo());
t.setCurrentBalance(t.getCurrentBalance());
t.setCustomer_ID(t.getCustomer_ID());
em.persist(t);
em.getTransaction().commit();
em.close();
}
@Override
protected void createCustomer(Customer t) throws SQLException, NamingException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
protected void createTransaction(Transaction t) throws SQLException, NamingException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
protected List withdraw(int id, double amount) throws NamingException {
return null;
}
}
ResourceBase.java:
package com.banking.resource;
import com.banking.entity.Account;
import com.banking.entity.Customer;
import com.banking.entity.Transaction;
import java.sql.SQLException;
import java.util.List;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Produces("application/xml")
public abstract class ResourceBase<T> {
protected abstract List getMultiQuery() throws SQLException, NamingException;
protected abstract List getSingleQuery(int id) throws NamingException;
protected abstract List withdraw(int id, double amount) throws NamingException;
protected abstract List getAccountBalance(int id) throws NamingException;
protected abstract void createCustomer(Customer t) throws SQLException, NamingException;
protected abstract void createAccount(Account t) throws SQLException, NamingException;
protected abstract void createTransaction(Transaction t) throws SQLException, NamingException;
protected abstract void deleteQuery(int id) throws SQLException, NamingException;
protected abstract void updateQuery(T t, int id) throws SQLException, NamingException;
protected static EntityManager getEntityManager() throws NamingException {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Banking");
return emf.createEntityManager();
}
@GET
public List<T> getList() throws SQLException, NamingException {
List records = getMultiQuery();
return records;
}
@GET
@Path("{id}")
public List<T> getSingle(@PathParam("id") int id) throws NamingException {
List records = getSingleQuery(id);
return records;
}
@PUT
@Path("{id}/{amount}")
public List<T> makeWithdrawal(@PathParam("id") int id, @PathParam("amount") double amount) throws NamingException {
List records = withdraw(id, amount);
return records;
}
@GET
@Path("/{id}/balance")
public List<T> getBalance(@PathParam("id") int id) throws NamingException {
List records = getAccountBalance(id);
return records;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/savecustomer")
public void insertCustomer(Customer t) throws NamingException, SQLException {
createCustomer(t);
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/saveaccount")
public void insertAccount(Account t) throws NamingException, SQLException {
createAccount(t);
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/addTransaction")
public void addTransaction(Transaction t) throws NamingException, SQLException {
createTransaction(t);
}
@DELETE
@Path("delete/{id}")
public void delete(@PathParam("id") int id) throws SQLException, NamingException {
deleteQuery(id);
}
}
MySQL中的Accounts表: