java小程序-----用if for写会员登陆和商品列表

一、父类

 1 public class Father{                            //父类
 2
 3     protected static int stock1 = 10;            //库存
 4     protected static int stock2 = 12;
 5     protected static int stock3 = 15;
 6     protected static int stock4 = 21;
 7     protected static int stock5 = 9;
 8     protected static int stock6 = 15;
 9
10     protected static float p1 = 1200;                //商品价格
11     protected static float p2 = 89;
12     protected static float p3 = 688;
13     protected static float p4 = 5900;
14     protected static float p5 = 399;
15     protected static float p6 = 2588;
16
17     protected static int integral1=10;            //会员积分
18     protected static int integral2=200;
19     protected static int integral3=900;
20     protected static int integral4=3000;
21
22     protected static int id1 = 1;                //商品id
23     protected static int id2 = 2;
24     protected static int id3 = 3;
25     protected static int id4 = 4;
26     protected static int id5 = 5;
27     protected static int id6 = 6;
28
29     int vipName;            //会员编号
30     String  name;            //商品名
31     float price;            //商品价格
32     int stock;                //商品库存
33     int integral;            //会员积分
34     int id;                    //商品id
35     protected static float sumprice = 0;
36
37
38     public Father(String name,float price,int stock,int id){    //有参构造(商品)
39         this.name = name;
40         this.price = price;
41         this.stock = stock;
42         this.id  = id;
43     }
44     public Father(int vipName,int integral){            //有参构造(会员)
45         this.vipName = vipName;
46         this.integral = integral;
47     }
48
49     public Father() {
50         super();
51     }
52
53
54 }

二、会员登陆

import java.util.Scanner;
public class Welcome extends Father{
    Scanner land= new Scanner(System.in);
    Father vip0 = new Father(0,integral1);                            //创建对象(会员)
    Father vip1 = new Father(1,integral2);
    Father vip2 = new Father(2,integral3);
    Father vip3 = new Father(3,integral4);
    public void vip(Scanner land){
        System.out.println("您好   欢迎光临!\n");
        System.out.println("请您先登陆!\n");
        System.out.println("请输入会员编号:");
        vipName=land.nextInt();

        Father [] vip = new Father[4];                                        //创建对象数组(会员)
        vip[0]=vip0;
        vip[1]=vip1;
        vip[2]=vip2;
        vip[3]=vip3;

        //用户名正确
        if(vipName==1||vipName==2||vipName==3||vipName==4||vipName==5||vipName==6){
            System.out.println("请输入密码:");
            String pasW = land.next();
            //密码正确
            if(pasW.equals("123")){
                System.out.println("登陆成功!\n"+vipName+"号vip,欢迎您!");
                System.out.println("您的当前积分为:"+vip[vipName].integral);

            }
            //密码错误
            else{
                System.out.println("密码错误0.0,请重新输入!");
                this.vip(land);
            }
        }
        //用户名错误
        else{
            System.out.println("用户不存在0.0,请重新输入!");
            this.vip(land);
        }
    }
    public static void main(String[]args){
        Scanner land= new Scanner(System.in);
        Welcome landVip = new Welcome();
        landVip.vip(land);

    }

}

三、商品列表及购买

import java.util.Scanner;
public class CommodityList extends Welcome{
    Father com1 = new Father("格力空调                         ",p1,stock1,id1);                //创建对象(商品)
    Father com2 = new Father("蓝色圆领polo衫(男)",p2,stock2,id2);
    Father com3 = new Father("Nice平底运动鞋透气型",p3,stock3,id3);
    Father com4 = new Father("华硕笔记本FL5900  ",p4,stock4,id4);
    Father com5 = new Father("adidas黑白户外双肩背包",p5,stock5,id5);
    Father com6 = new Father("chanel男士香水",p6,stock6,id6);

    Father comAll = new Father();                                                //无参构造对象
    Scanner sort= new Scanner(System.in);

    public void allCom(){

        Object selected=sort.next();

        Father [] com = new Father[6];                        //商品对象数组
        com[0] = com1;                                        //创建对象(商品)
        com[1] = com2;
        com[2] = com3;
        com[3] = com4;
        com[4] = com5;
        com[5] = com6;
        if(selected.equals("all")|selected.equals("0")|selected.equals("所有")){    //所有商品列表

            for(int i=0;i<6;i++){                            //遍历打印数组
                System.out.print("name:"+com[i].name+" \t");
                System.out.print("stock:"+com[i].stock+" \t");
                System.out.print("price:"+com[i].price+" \t");
                System.out.print("id:"+com[i].id+"\t");
                System.out.println();
                System.out.println();
            }
        }
        else if(selected.equals("assort")|selected.equals("1")|selected.equals("分类")){//分类商品列表
            System.out.println(‘\u2605‘+"数码电器"+‘\u2605‘);
            System.out.print("name:"+com[0].name+" \t");
            System.out.print("stock:"+com[0].stock+" \t");
            System.out.print("price:"+com[0].price+" \t");
            System.out.print("id:"+com[0].id+"\t");
            System.out.println();
            System.out.println();
            System.out.print("name:"+com[3].name+" \t");
            System.out.print("stock:"+com[3].stock+" \t");
            System.out.print("price:"+com[3].price+" \t");
            System.out.print("id:"+com[3].id+"\t");
            System.out.println();
            System.out.println();
            System.out.println(‘\u2605‘+"男士专区"+‘\u2605‘);
            System.out.print("name:"+com[1].name+" \t");
            System.out.print("stock:"+com[1].stock+" \t");
            System.out.print("price:"+com[1].price+" \t");
            System.out.print("id:"+com[1].id+"\t");
            System.out.println();
            System.out.println();
            System.out.print("name:"+com[5].name+" \t");
            System.out.print("stock:"+com[5].stock+" \t");
            System.out.print("price:"+com[5].price+" \t");
            System.out.print("id:"+com[5].id+"\t");
            System.out.println();
            System.out.println();
            System.out.println(‘\u2605‘+"户外运动"+‘\u2605‘);
            System.out.print("name:"+com[2].name+" \t");
            System.out.print("stock:"+com[2].stock+" \t");
            System.out.print("price:"+com[2].price+" \t");
            System.out.print("id:"+com[2].id+"\t");
            System.out.println();
            System.out.println();
            System.out.print("name:"+com[4].name+" \t");
            System.out.print("stock:"+com[4].stock+" \t");
            System.out.print("price:"+com[4].price+" \t");
            System.out.print("id:"+com[4].id+"\t");
            System.out.println();
            System.out.println();
        }
        else{
        System.out.println("输入错误!\n请重新输入");
        this.allCom();
        }

    }
    public void shopping(){
        Father [] com = new Father[6];                        //商品对象数组
        com[0] = com1;                                        //创建对象(商品)
        com[1] = com2;
        com[2] = com3;
        com[3] = com4;
        com[4] = com5;
        com[5] = com6;
        //是否购买判断
        System.out.println("是否购买商品(yes/no/是/否)");
        String sure = sort.next();

        //购买
        if(sure.equals("yes")|sure.equals("是")){

            System.out.println("请输入您选中的商品id");
            int shopId = sort.nextInt();
            for(int i=0;shopId<=com.length;i++){
                comAll.sumprice +=com[shopId-1].price;        //商品总价格
                --com[shopId-1].stock;                        //库存-1
                System.out.println("继续添加?(yes/no)");
                String cont = sort.next();
                //继续
                if(cont.equals("yes")){
                    System.out.println("请输入您选中的商品id");
                    shopId = sort.nextInt();
                }
                //不继续
                else if(cont.equals("no")){

                    System.out.println("谢谢惠顾!您购买的商品总价格为:"+comAll.sumprice+"元!积分增加"+comAll.sumprice/100);

                    break;
                }
                //其他回答
                else{
                    System.out.println("不好意思,没听清!");
                    CommodityList Com = new CommodityList();                    //创建对象
                    Com.shopping();
                }
            }

        }

        //不购买
        else if(sure.equals("no")|sure.equals("否")){
            System.out.println("欢迎下次光临!");
        }
        //输入错误
        else{
            System.out.println("不好意思,没听清!请您再说一次0.0");
            CommodityList Com = new CommodityList();                    //创建对象
            Com.shopping();
        }
    }

    public static void main(String[]args){
        CommodityList Com = new CommodityList();                    //创建对象
        System.out.println("0  所有商品列表");                            //提示信息
        System.out.println("1  商品分类列表");
        System.out.println("请选择商品显示列表(0/1/all/assort/“所有”/“分类”)");//提示输入信息
        Com.allCom();
        Com.shopping();

    }
}
时间: 2024-10-22 04:03:09

java小程序-----用if for写会员登陆和商品列表的相关文章

基于Bluemix快速构建部署一款Java小程序——微博影响力分析器

一.前言 近年来IT界风云际会,各种新概念如大数据.云平台.虚拟化等,如雨后春笋般层出不穷.而云平台,可以说是近些年来引领时代新潮的边缘概念之一,各大厂商竞相推出各种云产品,抢占云市场高地.近期,IT百年老厂IBM也推出了其花费重金打造的基于CloudFoundry开放标准的云平台Bluemix.本文就旨在从一位普通Java开发者角度,尝鲜Bluemix云平台及其提供的Cloudant服务,快速构建并部署一款Java小程序"微博影响力分析器". 二."微博影响力分析器&quo

Linux下搭建Java环境变量并实现第一个Java小程序

1.首先,我们打开虚拟机里面的Linux操作系统,然后点击下图菜单下的虚拟机选项: 在点击虚拟机下的设置菜单,如下图所示: 这里我已经添加进去我所需要的文件夹了,我放在本机E盘下的as文件夹里面到底放了什么呢? 上图这两个压缩文件,一个是android studio的压缩包,上年发布出来的,一直没用它,今天本来想在windows下安装这个软件,发现所需要的jdk是1.7,而我在windows下的jdk版本为1.6,所以打算在Linux操作系统下安装jdk1.7,并安装andoid studio工

java学习的一些预备知识和一些简单的java小程序

1先了解一些基本的dos命令 dir     ·列出当前目录下的文件和文件夹 md      创建文件夹 rd        删除文件夹(文件下没有子文件) del "文件夹名"    可以删除带子文件的文件夹 cd..    使文件夹目录一层一层后退 cd\    回到根目录 haha>1.txt    创建文档,并将haha写入到1.txt文档中 del 1.txt    删除文件(不走回收站) del  *.txt    只删除txt文件 exit    退出命令行 set

搜索文件或目录中包含字符串的文件 java小程序

package com.ruishenh.spring.test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Enumeration; import java.

输出多行字符的一个简单JAVA小程序

1 public class JAVA 2 { 3 public static void main(String[] args) 4 { 5 System.out.println("----------------------"); 6 System.out.println("|| 我要学会 ||"); 7 System.out.println("|| JAVA语言 ||"); 8 System.out.println("-------

hello world Java小程序入门

/** 作者:haha版本:V1.0这个类是用于演示hello world.*/class Demo //这是我的第一个java小程序,//很爽!{/*main函数可以保证该的独立运行.它是程序的入口.它会被JVM所调用.*/public static void main(String[] args){/*System.out.println("hello java");//这是输出语句,可以打印小括号中的内容.System.out.println("hello world&

Java小程序之fibonacci数列 BigInteger(高精度整型)

package com.java.learning.recursion; import java.math.*; public class MainClass { public static void main(String args[]){ for(int i = 0; i < 100; i++){ f(i+1); } } public static BigInteger f(long n){ if(n <= 2){ return new BigInteger("1");

java 小程序

import java.applet.Applet; import java.awt.*; public class Appelet extends Applet { String name; public void init () { name=getParameter("myName"); } public void paint(Graphics gr) { gr.drawString("欢迎学习java", 25,30); } } java 小程序

框架一般用作Java应用程序的窗口,而Applet是Java小程序的窗口

框架一般用作Java应用程序的窗口,而Applet是Java小程序的窗口. 与Frame不同,Applet是在网页中显示的,也可以通过添加Panel进行组件布局. 1 package TomAwt; 2 3 4 import java.applet.Applet; 5 import java.awt.*; 6 public class TomAwt_18 extends Applet{ 7 private TextField textField1,textField2; 8 public voi