一、Java简介
Java是一门面向对象的编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
面向对象的程序设计语言必须有描述对象及其相互之间关系的语言成分。即系统中一切事物皆为对象;对象是属性及其操作的封装体;对象可按其性质划分为类,对象成为类的实例;实例关系和继承关系是对象之间的静态关系;消息传递是对象之间动态联系的唯一形式,也是计算的唯一形式;方法是消息的序列。
二、购物系统
/* 商品模块 */ import java.util.Scanner; public class Goods { //定义变量:商品名称name,商品价格price,商品数量num,商品配送peisong String name; double price; int num; String peisong; //立即购买buy,加入购物车addcar public void buy(){ //触发buy方法,显示商品名称,商品价格,商品数量,商品配送 System.out.println("您购买了此商品!"); //调用订单模块,实例化订单类,调动订单类 Order order = new Order(); order.make_order(); } //显示商品相关信息 public void show(){ //显示商品 System.out.println(this.name); System.out.println(this.price); System.out.println(this.num); System.out.println(this.peisong); //用户调整数量 Scanner scanner = new Scanner(System.in); System.out.println("请您输入购买商品的数量:"); //try——catch防止报错 while(true){ try{ 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{ int no=scanner.nextInt(); switch(no){ case 1: this.buy(); break; case 2: this.addcar(); break; } }catch(Exception e) { //默认方法 this.buy(); } } //显示加入购物车 public void addcar(){ System.out.println("您已将商品加入购物车"); } }
/* 订单模块 */ public class Order extends Goods{ //定义属性,总价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)); //调用支付,实例化 Pay pay = new Pay(); pay.username="张三"; pay.money=100; pay.pay_order(); } }
/* 支付模块 */ public class Pay extends Order{ String username; double money; //实现方法 public void pay_order(){ //判断当前余额比订单大还是小 if(this.money>this.total){ this.money=this.money-this.total; System.out.println("您购物成功!"); }else{ System.out.println("您购物失败!"); } } }
/* 商品模块首页 开发者:Wx 版本:1.0 */ import java.util.Scanner; public class Shopping { public static void main(String[] args) { //显示商品列表的菜单 System.out.println("————————————————————————————————"); System.out.println(" 幸运购物系统"); System.out.println(" 版本:v1.0"); System.out.println("————————————————————————————————"); //实例化商品 Goods goods1=init("伤寒论张仲景正版原著中医书籍大全白话彩图典藏版",9.8,1,"江苏南京至北京市昌平区"); Goods goods2=init("鬼谷子狼道墨菲定律断舍离说话技巧",178,1,"河北廊坊至北京市昌平区"); Goods goods3=init("红岩书正版",21.6,1,"河北廊坊至北京市昌平区"); Goods goods4=init("镜花缘 李汝珍著 ",18.9,1,"天猫优仓天津1仓至北京市昌平区"); Goods 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); //接收商品编号,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 Goods init(String name,double price,int num,String peisong){ Goods goods1=new Goods(); goods1.name=name; goods1.price=price; goods1.num=num; goods1.peisong=peisong; return goods1; } }
三、运行结果
需要完整版资源请访问博主个人码云地址:JavaSS: Java实训