Java面向对象练习题账户余额购买商品返回订单数据

题目:账户余额购买商品返回订单数据

创建一个trade包,其下创建model包、service包和controller包。在model包中包含Product类、Trade类和Account类,其中Product类至少有名称、价格和库存数量等属性,Trade类包含购买的什么商品和数量等属性,Account类包含账户名称、剩余金额等属性。service包中创建TradeService接口及其实现类TradeServiceImpl,编写创建订单的方法creatTrade,账户和所购买的商品及数量作为参数,实现验证商品库存是否充足,以及账户金额是否足够支付本次购买,满足这两个条件时createTrade方法返回Trade信息。在controller包中创建TradeController类,编写mian方法调用创建订单的方法,打印输出订单信息。

简单实现一次购买一种商品然后返回订单的方法,脱离题目可升级为一次购买多件商品。 -----------------Tip:使用List<>

controller

package cn.maidaotech.java11.exercise03.trade.controller;

import java.math.BigDecimal;

import cn.maidaotech.java11.exercise03.trade.model.Account;
import cn.maidaotech.java11.exercise03.trade.model.Product;
import cn.maidaotech.java11.exercise03.trade.model.Trade;
import cn.maidaotech.java11.exercise03.trade.service.TradeService;
import cn.maidaotech.java11.exercise03.trade.service.TradeServiceImpl;

public class TradeController {// 交易
 // private TradeService tradeService;

 public static void main(String[] args) throws Exception {
   Product product1 = new Product("电脑", BigDecimal.valueOf(1200), 9);
   Product product2 = new Product("键盘", BigDecimal.valueOf(120), 2);
   Product product3 = new Product("鼠标", BigDecimal.valueOf(12), 1);

   Account account1 = new Account("詹姆斯", BigDecimal.valueOf(50000));
   Account account2 = new Account("西蒙斯", BigDecimal.valueOf(500));
   // 一次只买一种商品 买多件创建多笔订单
   TradeService tradeService = new TradeServiceImpl();

   Trade trade1 = tradeService.creatTrade(product2, 0, account2);
   System.out.println("-账户姓名-----" + trade1.getAccount().getName());
   System.out.println("-购买数量-----" + trade1.getNumber());
   System.out.println("-商品名称-----" + trade1.getProduct().getName());
   System.out.println("-商品数量-----" + trade1.getProduct().getNumber());
   System.out.println("-商品价格-----¥" + trade1.getProduct().getPrice());
   System.out.println("-总价---------¥" + trade1.getTotalPrice());
 }
}

model

package cn.maidaotech.java11.exercise03.trade.model;

import java.math.BigDecimal;

//账户
public class Account {
 private String name;// 账户名称
 private BigDecimal money;// 剩余金额

 public Account() {
 }

 public Account(String name, BigDecimal money) {
   this.name = name;
   this.money = money;
 }

 public String getName() {
   return name;
 }

 public void setName(String name) {
   this.name = name;
 }

 public BigDecimal getMoney() {
   return money;
 }

 public void setMoney(BigDecimal money) {
   this.money = money;
 }

}
package cn.maidaotech.java11.exercise03.trade.model;

import java.math.BigDecimal;

//产品
public class Product {
// 其中Product类至少有名称、价格和库存数量等属性,
private String name;// 名称
private BigDecimal Price;// 价格
private Integer number;// 库存数量

public Product() {
}

public Product(String name, BigDecimal price, Integer number) {
  this.name = name;
  Price = price;
  this.number = number;
}

public String getName() {
  return name;
}

public void setName(String name) {
  this.name = name;
}

public Integer getNumber() {
  return number;
}

public void setNumber(Integer number) {
  this.number = number;
}

public BigDecimal getPrice() {
  return Price;
}

public void setPrice(BigDecimal price) {
  Price = price;
}

}
package cn.maidaotech.java11.exercise03.trade.model;

import java.math.BigDecimal;

//订单
public class Trade {

private Product product;// 商品
private Integer number;// 购买数量
private BigDecimal totalPrice;// 总价

private Account account;// 账户

public Trade() {
}

public Integer getNumber() {
 return number;
}

public Trade(Product product, Integer number, BigDecimal totalPrice, Account account) {
 this.product = product;
 this.number = number;
 this.totalPrice = totalPrice;
 this.account = account;
}

public Trade(Product product, Integer number, BigDecimal totalPrice) {
 this.product = product;
 this.number = number;
 this.totalPrice = totalPrice;
}

public void setNumber(Integer number) {
 this.number = number;
}

public BigDecimal getTotalPrice() {
 return totalPrice;
}

public void setTotalPrice(BigDecimal totalPrice) {
 this.totalPrice = totalPrice;
}

public Product getProduct() {
 return product;
}

public void setProduct(Product product) {
 this.product = product;
}

public Account getAccount() {
 return account;
}

public void setAccount(Account account) {
 this.account = account;
}

}

service

package cn.maidaotech.java11.exercise03.trade.service;

import cn.maidaotech.java11.exercise03.trade.model.Account;
import cn.maidaotech.java11.exercise03.trade.model.Product;
import cn.maidaotech.java11.exercise03.trade.model.Trade;

//接口类
public interface TradeService {
Trade creatTrade(Product product, Integer number, Account account) throws Exception;
// 必须对接口类方法进行重写
// 登录账号 传入账户信息
}
package cn.maidaotech.java11.exercise03.trade.service;

import java.math.BigDecimal;

import org.springframework.stereotype.Service;

import cn.maidaotech.java11.exercise03.trade.model.Account;
import cn.maidaotech.java11.exercise03.trade.model.Product;
import cn.maidaotech.java11.exercise03.trade.model.Trade;

//接口实现类
@Service
public class TradeServiceImpl implements TradeService {

@Override
public Trade creatTrade(Product product, Integer number, Account account) throws Exception {
 if (product == null || number == null || account == null) {
   throw new Exception("product or number or accout is null");
 }

 if (number == 0) {
   throw new Exception("!0");
 }

 if (number > product.getNumber()) {
   throw new Exception("number is not enough");
 }
 BigDecimal totalPrice = product.getPrice().multiply(BigDecimal.valueOf(number));
 int a = totalPrice.compareTo(account.getMoney());
 if (a == 1) {
   throw new Exception("money is not enough");
 }

 Trade trade = new Trade();
 trade.setNumber(number);
 trade.setProduct(product);
 trade.setTotalPrice(totalPrice);
 trade.setAccount(account);
 return trade;
}

}

你可能感兴趣的:(java案例,代码逻辑,java,开发语言,jvm)