import java.util.Scanner; /* 商品构买逻辑模块的首页 开发者:XXXX 版本:XXXXX */ public class MyPage15 { public static void main(String[] args) { //显示商品列表的菜单 System.out.println("******************"); System.out.println(" 欢迎光临幸运购买系统"); System.out.println(" 作者:EREC"); System.out.println("***************"); //实例化所有的商品,后面显示 new一个 Goods类,就是实例化一个商品,数量初始化为1 Goods15 goods1=init("伤寒论张仲景正版原著中医书籍大全白话彩图典藏版",9.8,"江苏南京至北京市昌平区",1); Goods15 goods2=init("鬼谷子狼道墨菲定律断舍离说话技巧",178,"河北廊坊至北京市昌平区",1); Goods15 goods3=init("红岩书正版",21.6,"河北廊坊至北京市昌平区",1); Goods15 goods4=init("镜花缘 李汝珍著 ",18.9,"天猫优仓天津1仓至北京市昌平区",1); Goods15 goods5=init("读心术正版心理学书",8.8,"北京至北京市昌平区",1); //初始化五个商品,显示到首页上,重复显示五个数 print_goods(1,goods1.name,goods1.price); print_goods(2,goods2.name,goods2.price); print_goods(3,goods3.name,goods3.price); print_goods(4,goods4.name,goods4.price); print_goods(5,goods5.name,goods5.price); System.out.println("*********************"); //让用户选择某一个商品,这里需要输入语句 Scanner scanner = new Scanner(System.in); //这里只接收商品编号,使用nextInt会报错,直接用try -----catch try { int no=scanner.nextInt(); //当选择对应的商品后,进行商品模块,商品模块中显示商品详情,立即购买,加入购物车两个按钮 //调用商品模块中的显示。 switch(no) { case 1:goods1.show();break; case 2:goods2.show();break; case 3:goods3.show();break; case 4:goods4.show();break; case 5:goods5.show();break; } }catch(Exception e) { System.out.println("谢谢使用,你输入错误,请输入商品对应的编号"); } } //用函数解决代码中重复代码问题,参数不一致,在循环中用多个数组来解决,这里用函数传参的方式 public static void print_goods(int i,String name,double price) { System.out.println( i+"-"+name+"\t"+price); } public static Goods15 init(String name,double price,String peison,int num) { Goods15 goods1=new Goods15(); goods1.name=name; goods1.price=price; goods1.peison=peison; goods1.num=num; return goods1; } }
import java.util.Scanner; /* 商品模块中的商品类 */ public class Goods15 { //把变量定义完成,peisong 配送的变量 num 数量的变量 price 价格的变量 name商品名称 String name; String peison; int num; double price; //有两个方法 buy是立即购买 addcar加入购物车,这个方法中不用加static public void buy() { //最终用户触发buy方法,信息方法知道商品名称,商品数量,价格,配送 //把Goods类中的变量输出,直接输出name变成红色,会报错, class类中帮助定义一个关键字,可以使用关键字引用变量,关键字this //this作用指明当前的类this的实例,这里this报错, this报错的 System.out.println("你购买了这个商品"); //调用订单模块,订单需要实例化,先实例化订单类,然后调用方法 Order15 order = new Order15(); order.make_order(); } //显示商品相关信息方法,提供给主页调用 public void show() { //首先显示商品 System.out.println(this.name); System.out.println(this.num); System.out.println(this.price); System.out.println(this.peison); //用户调整数量 Scanner scanner = new Scanner(System.in); System.out.println("请输入购买商品的数量"); //数量只允许整形,如果防止用户输入错误,这里 try-----catch //这里的商品数量,如果出错,要继续输入 while(true) { try { //输入的数量直接给this.num,this指示的当前对象 this.num=scanner.nextInt(); //成功后退出循环 break; } catch(Exception e) { System.out.println("需要重新输入商品数量,你输入的有问题,请输入整数"); } } System.out.println("*********************"); System.out.println("1------立即购买"); System.out.println("2-------加入购物车"); System.out.println("*********************"); System.out.println("请选择你的购买意向,输入序号"); try { //scanner是本类中本函数的全局变量,可以在这里使用 int no=scanner.nextInt(); switch(no) { case 1:this.buy();break; case 2:this.addcar();break; } } catch(Exception e) { //如果用户输入错误,默认选择立即购买的方法 this.buy(); } } //下面方法为了引用全局的变量,这里不加static public void addcar() { System.out.println("你已经把商品加入购物车"); } }
/* 订单类,订单模块,需要把商品类的所有信息取过来 才能计算订单和找到配送地址 用到java类中继承, extends商品类,找到商品类的各种信息 */ public class Order15 extends Goods15 { //定义属性,总价total double total; public void make_order() { //产生定单号,定单号规定8位的整数 long order_id=(int)(Math.random()*10000000+10000000); this.total=this.num*this.price; //格式化,保留小数位数 this.total=Double.parseDouble(String.format("%.2f",this.total)); //离开了订单页面,到支付界面,调用支付,实例化 Pay15 pay = new Pay15(); //支付的帐户需要实例化赋值,order中的total不需要赋值,是自动算 pay.username="张三"; pay.money=100; pay.pay_order(); } }
/* 在生成订单以后,把上一个订单的总价传送给Pay 这个流程操作的是一个订单 */ public class Pay15 extends Order15 { String username; double money; //实现方法 public void pay_order() { //实现支付的逻辑,判断当前的余额比订单大还是小 if(this.money>this.total) { this.money=this.money-this.total; System.out.println("恭喜你,成功买得商品"); // System.out.println(this.name); // System.out.println(this.peison); // System.out.println(this.price); // System.out.println(this.num); // System.out.println(this.total); } else System.out.println("大哥,钱不够"); } }
/* 首页模块 是最终执行的模块 */ public class SouYe15 { public static void main(String[] args) { Goods15 goods = new Goods15(); goods.name="伤寒论张仲景正版原著中医书籍大全白话彩图典藏版"; goods.price=9.8; goods.peison="江苏南京至北京市昌平区"; goods.num=1; goods.buy(); } }