- 商品类:
- 商品编号
- 商品名称
- 商品价格
- 商品数量
- 订单列表类:
- 商品
- 数量
- 订单类:
- 订单编号
- 订单总额
- 订单详情
package com.oop1.Test;
/*
商品类
*/
public class Project {
private String Pro_id; // 商品编号(id)
private String Pro_name; // 商品名称
private double Pro_price; // 商品价格
private int Pro_quantity; // 商品数量
private int Pro_pno; // 存储编号
// 创建有参构造方便传参调用
public Project(int pro_pno,String pro_id,String pro_name,double pro_price,int pro_quantity){
this.Pro_id = pro_id;
this.Pro_name = pro_name;
this.Pro_price = pro_price;
this.Pro_quantity = pro_quantity;
this.Pro_pno = pro_pno;
}
// 生成 getter、setter方法方便调用以及传参
public String getPro_id() {
return Pro_id;
}
public void setPro_id(String pro_id) {
Pro_id = pro_id;
}
public String getPro_name() {
return Pro_name;
}
public void setPro_name(String pro_name) {
Pro_name = pro_name;
}
public double getPro_price() {
return Pro_price;
}
public void setPro_price(double pro_price) {
Pro_price = pro_price;
}
public int getPro_quantity() {
return Pro_quantity;
}
public void setPro_quantity(int pro_quantity) {
Pro_quantity = pro_quantity;
}
public int getPro_pno() {
return Pro_pno;
}
public void setPro_pno(int pro_pno) {
Pro_pno = pro_pno;
}
}
package com.oop1.Test;
/*
订单类
用于管理用户的订单
订单类(Order):
a) 订单号(Pro_id)
b) 订单总额(Pro_balance)
c) 订单项列表(Pro_details)
*/
public class Order {
private String Pro_id; // 订单编号(id)
private final OrderDetails[] Pro_details; // OrderDetails:订单列表类 订单详情列表
private double Pro_balance; // 订单总额
// 定义有参构造方法,并且传入订单id
public Order(String pro_id){
this.Pro_id = pro_id;
this.Pro_details = new OrderDetails[3];
}
// 生成 getter、setter 方法
public String getPro_id() {
return Pro_id;
}
public void setPro_id(String pro_id) {
Pro_id = pro_id;
}
public double getPro_balance() {
probAlan();
return Pro_balance;
}
// 定义一个订单总额的方法 probAlan
public void probAlan() {
// 定义一个变量用于接收订单总额
double balance = 0;
// 使用 for 循环进行输出 小于订单类表的长度
for (int i = 0; i < Pro_details.length ; i++) {
// 用数量乘以商品和价格并且赋值给 balance
balance += Pro_details[i].getNums() * Pro_details[i].getProject().getPro_price();
}
// 初始化当前订单列表的值
this.Pro_balance = balance;
}
public void setPro_balance(double pro_balance) {
Pro_balance = pro_balance;
}
public OrderDetails[] getPro_details() {
return Pro_details;
}
public void setPro_details(OrderDetails pro_details,int idx) {
this.Pro_details[idx] = pro_details;
}
}
package com.oop1.Test;
/*
订单详情类 订单项类(OrderItem):
a) 商品(Project)
b) 购买数量(nums)
方便用户查询订单
*/
public class OrderDetails {
// 把那个商品类拿过来进行封装
private Project project;
// 定义一个变量,用于存储订单的数量信息
private int nums;
// 定义有参构造,方便传参;并把上边的商品类以及订单信息定义为形参
public OrderDetails(Project project,int nums){
// 并且进行初始化值为动态的
this.nums = nums;
this.project = project;
}
// 生成 getter、setter 方法方便调用以及传参
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
public int getNums() {
return nums;
}
public void setNums(int nums) {
this.nums = nums;
}
}
package com.oop1.Test;
/*
主要编写用户 *** 作订单
*/
public class OrderDemo1 {
public void testOrder(Order order){
System.out.println("===================\t商品订单\t===================");
System.out.println("订单编号:"+order.getPro_id());
System.out.println("商品名称\t购买数量\t商品价格\t");
// 获取订单列表使用 get 方法
OrderDetails[] details = order.getPro_details();
// 定义一个空的变量 line 用于接收订单信息
String line = null;
for (int i = 0; i < details.length ; ++i) {
line = details[i].getProject().getPro_name()+"\t"+details[i].getNums()+"\t"+details[i].getProject().getPro_price();
System.out.println(line);
}
System.out.println("商品总额为:"+order.getPro_balance());
System.out.println("-------------------------------------------");
}
}
package com.oop1.Test;
import java.time.LocalTime;
import java.util.Scanner;
/*
购买商品
*/
public class ProjectDemo1 {
public Order testProject(Project[] projects){
Scanner scanner = new Scanner(System.in);
OrderDetails orderDetails = null;
Order order = new Order("Pro_id-"+ LocalTime.now().toString());
for (int i = 0; i < projects.length ; i++) {
System.out.print("请选择需要购买的商品编号:");
int bno = scanner.nextInt();
System.out.print("购买数量:");
int num = scanner.nextInt();
orderDetails = new OrderDetails(projects[bno-1],num);
order.setPro_details(orderDetails,i);
System.out.println("请继续购买图书...");
}
scanner.close();
return order;
}
}
package com.oop1.Test;
/*
商品类
*/
public class ProjectDemo2 {
public void project(Project[] project){
project[0] = new Project(1,"100001","iphone13",5999.0,10);
project[1] = new Project(2,"100002","iphone13pro",7999.0,10);
project[2] = new Project(3,"100003","iphone13proMax",9999.0,10);
System.out.println("===================\t图书\t===================");
System.out.println("编号\t\nBST\t名称\t价格\t库存");
String li = null;
for (int i = 0; i < project.length ; ++i) {
li = project[i].getPro_pno() + "\t" + project[i].getPro_id() + "\t"
+ project[i].getPro_name() + "\t" + project[i].getPro_price()
+ "\t" + project[i].getPro_quantity();
System.out.println(li);
}
System.out.println("==================================");
}
}
正在修改bug更新中....
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)