面向对象程序设计

MyGoods:

import java.util.Scanner;


public class MyGoods {
    private String name;
    private String peison;
    private int num;
    private double price;
    private String small_pic;
    public MyGoods() {
    }

    public MyGoods(String name, double price,String peison, int num) {
        this.name = name;
        this.peison = peison;
        this.num = num;
        this.price = price;
    }
    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("请输入购买商品的数量");
        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 buy(){
        System.out.println("你购买了这个商品");
        this.make_order();
    }
    public  void addcar(){

        System.out.println("你已经把商品加入购物车");
    }
    protected void make_order(){
    }


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

    public String getPeison() {
        return peison;
    }

    public void setPeison(String peison) {
        this.peison = peison;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getSmall_pic() {
        return small_pic;
    }

    public void setSmall_pic(String small_pic) {
        this.small_pic = small_pic;
    }
}
MyOrder:

public class MyOrder extends MyGoods{
    private double total;
    @Override
    protected final void make_order(){
        long order_id=(int)(Math.random()*10000000+10000000);
        this.total=this.getNum()*this.getPrice();
        this.total=Double.parseDouble(String.format("%.2f",this.total));
        this.pay_order();
    }
    protected void pay_order(){
    }
    public double getTotal() {
        return total;
    }
}
MyPage:

import java.util.Scanner;


public class MyPage {
    public static void main(String[] args) {
        System.out.println("*********************");
        System.out.println("  欢迎光临幸运购买系统");
        System.out.println("          版本:v1.1");
        System.out.println("修改内容:整个继承链上只有一个地址指向");
        System.out.println("--------------------");
        MyGoods goods1=new MyGoods("伤寒论张仲景正版原著中医书籍大全白话彩图典藏版",9.8,"江苏南京至北京市昌平区",1);
        MyGoods goods2=new MyGoods("鬼谷子狼道墨菲定律断舍离说话技巧",178,"河北廊坊至北京市昌平区",1);
        MyGoods goods3=new MyGoods("红岩书正版",21.6,"河北廊坊至北京市昌平区",1);
        MyGoods goods4=new MyGoods("镜花缘 李汝珍著 ",18.9,"天猫优仓天津1仓至北京市昌平区",1);
        MyGoods goods5=new MyGoods("读心术正版心理学书",8.8,"北京至北京市昌平区",1);
        print_goods(1,goods1.getName(),goods1.getPrice());
        print_goods(2,goods2.getName(),goods2.getPrice());
        print_goods(3,goods3.getName(),goods3.getPrice());
        print_goods(4,goods4.getName(),goods4.getPrice());
        print_goods(5,goods5.getName(),goods5.getPrice());
        System.out.println("*********************");
        
        Scanner scanner = new Scanner(System.in);
        
        scanner.nextLine();
       
        System.out.println("请进行用户登录");
       
        MyPay mypay1=new MyPay();
       
        mypay1.setName(goods1.getName());
        mypay1.setPrice(goods1.getPrice());
        mypay1.setPeison(goods1.getPeison());
        mypay1.setNum(1);
        mypay1.myinput();       
        MyPay mypay2=new MyPay();        
        mypay2.setUsername(mypay1.getUsername());
        mypay2.setMoney(mypay1.getMoney());
        mypay2.setName(goods2.getName());
        mypay2.setPrice(goods2.getPrice());
        mypay2.setPeison(goods2.getPeison());
        mypay2.setNum(1);
        MyPay mypay3=new MyPay();
        mypay3.setUsername(mypay1.getUsername());
        mypay3.setMoney(mypay1.getMoney());
        mypay3.setName(goods3.getName());
        mypay3.setPrice(goods3.getPrice());
        mypay3.setPeison(goods3.getPeison());
        mypay3.setNum(1);
        MyPay mypay4=new MyPay();
        mypay4.setUsername(mypay1.getUsername());
        mypay4.setMoney(mypay1.getMoney());
        mypay4.setName(goods4.getName());
        mypay4.setPrice(goods4.getPrice());
        mypay4.setPeison(goods4.getPeison());
        mypay4.setNum(1);
        MyPay mypay5=new MyPay();
        mypay5.setUsername(mypay1.getUsername());
        mypay5.setMoney(mypay1.getMoney());
        mypay5.setName(goods5.getName());
        mypay5.setPrice(goods5.getPrice());
        mypay5.setPeison(goods5.getPeison());
        mypay5.setNum(1);      
        System.out.println("请输入购买商品的编号:");
        try{
            int no=scanner.nextInt();
          
            switch(no){
                case 1:  
                    mypay1.show();
                    break;
                case 2:
                    mypay2.show();
                    break;
                case 3:
                    mypay3.show();
                    break;
                case 4:
                    mypay4.show();
                    break;
                case 5:
                    mypay5.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);
    }

}
MyPage_youhua:

import java.util.Scanner;


public class MyPage_youhua {
    public static void main(String[] args) {
        MyPay[] mypays=new MyPay[5];
        String[] names= {"伤寒论张仲景正版原著中医书籍大全白话彩图典藏版",
                "鬼谷子狼道墨菲定律断舍离说话技巧",
                "红岩书正版",
                "镜花缘 李汝珍著 ",
                "读心术正版心理学书"
        };
        Double[] prices= {9.8,178.0,21.6,18.9,8.8};
        String[] peisongs= {"江苏南京至北京市昌平区",
                "河北廊坊至北京市昌平区",
                "河北廊坊至北京市昌平区",
                "天猫优仓天津1仓至北京市昌平区",
                "北京至北京市昌平区"
        };
        //显示商品列表的菜单
        System.out.println("*********************");
        System.out.println("  欢迎光临幸运购买系统");
        System.out.println("          版本:v1.2");
        System.out.println("修改内容:分类后的优化代码");
        System.out.println("--------------------");

        for(int i=0;i<5;i++){
                mypays[i]=new MyPay();
                mypays[i].setName(names[i]);
                mypays[i].setPrice(prices[i]);
                mypays[i].setPeison(peisongs[i]);
                mypays[i].setNum(1);
                print_goods(i+1,mypays[i].getName(),mypays[i].getPrice());
        }

        System.out.println("*********************");
        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();
        System.out.println("请进行用户登录");
        mypays[0].myinput();
       for(int j=1;j<5;j++){
           mypays[j].setUsername(mypays[0].getUsername());
           mypays[j].setMoney(mypays[0].getMoney());
       }
        System.out.println("请输入购买商品的编号:");
        try{
            int no=scanner.nextInt();
            mypays[no-1].show();

        }catch(Exception e){
            System.out.println("谢谢使用,你输入错误,请输入商品对应的编号");
        }

    }
    public static void print_goods(int i,String name,double price){
        System.out.println( i+"-"+name+"\t"+price);
    }

}
MyPay:

import java.util.Scanner;

public class MyPay extends MyOrder{
    private String username;
    private double money;

    public MyPay() {
    }
    public MyPay(String username, double money) {
        this.username = username;
        this.money = money;
    }
    public void myinput(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入付款帐户名称");
        this.username=scanner.next();
        System.out.println("请输入付款帐户余额:");
        try{
            this.money=scanner.nextDouble();
        }catch(Exception e){
            this.money=100;
        }
    }
    @Override
    public void pay_order(){
        if(this.money>this.getTotal()){
            this.money=this.money-this.getTotal();
            System.out.println("恭喜你,成功买得商品");
            System.out.println(this.getName());
            System.out.println(this.getPeison());
            System.out.println(this.getPrice());
            System.out.println(this.getNum());
            System.out.println(this.getTotal());
        }else{
            System.out.println("大哥,钱不够");
        }
    }
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public double getMoney() {
        return money;
    }

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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值