GGS校园超市购物系统

Java新人一个,基于程序基本概念、流程控制和数组制作了一个简陋的超市购物系统,还望各位大佬指点指点不足之处

完整代码在最后

一:数组的运用

1:创建数组分配空间并赋值

//声明数组类型并分配空间
            //str、price用来存储商品名称、商品价格;sno、birthday、integral用来存储会员号、会员生日、会员积分...
            String[] str = new String[100];
            double[] price = new double[100];
            String[] sno = new String[100];
            String[] birthday = new String[100];
            double[] integral = new double[100];
            String[] str1 = new String[100];
            double[] price1 = new double[100];
            //为数组赋值
            str[0] = "泡面";
            str[1] = "火腿";
            price[0] = 122.5;
            price[1] = 4.5;

2:通过控制台方法赋值,例如

 str[j] = sc.next();

3:交换数组元素

方法(1)

String temp = str[index];
str[index] = str1[a];
str1[a] = temp;

方法(2)

price1[a] = price1[a] + price[index];
price[index] = price1[a] - price[index];
price1[a] = price1[a] - price[index];

4:遍历数组

private static int printArray(String[] arr, String e) {
        for (int i = 0; i < arr.length; i++) {
            if (Objects.equals(arr[i], e)) {
                return i;
            }
        }
        return 0;
    }

5:输出数组元素

while ("2".equals(i)){
                    System.out.println("商品名称" + "   " + "商品单价");
                    for (int v = 0; v <= (j - 1); v++) {
                        System.out.println(str[v] + "       " + price[v] + "元");
                    }
                    break;
                }

6:continue、break

continue只是中止本次循环,进行下一次循环;break用于跳出一个循环体或者完全结束一个循环

二:流程控制循环语句

1:if语句如果判断条件为真则无限循环;可以用return结束,如:

while ("9".equals(i)) {
                    return;
                }

2:for语句,先判断条件是否符合,符合则执行下一步;不符合则结束语句

for (int v = 0; v <= (a - 1); v++) {
                        System.out.println(str1[v] + price1[v]);
                    }

3:while语句和for语句一样先判断再执行;否则跳过语句

while ("8".equals(i)) {
                    System.out.println("会员号" + "  " + "会员生日" + "   " + "会员积分");
                    for (int v = 0; v <= (x - 1); v++) {
                        System.out.println(sno[v] + "    " + birthday[v] + "    " + integral[v] + "\n");
                    }
                    break;
                }

4:do...while语句无论条件是否符合都会执行一次,然后再判断条件,不符合则结束循环

do {
                        System.out.println("请输入购买的商品");
                        e = sc.next();
                        //index是指定元素的下标
                        int index = printArray(str, e);
                        sum = sum + price[index];
                        //两个不同的方法交换数组的元素
                        String temp = str[index];
                        str[index] = str1[a];
                        str1[a] = temp;
                        str[index]=temp;
                        price1[a] = price1[a] + price[index];
                        price[index] = price1[a] - price[index];
                        price1[a] = price1[a] - price[index];
                        price[index] = price1[a] - price[index];
                        a = a + 1;
                        break;
                    } while (true);

三:完整代码如下

​
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Scanner;
public class GroupThree {
    public static void main(String[] args) {
        System.out.println("请输入账号密码登录系统");
        Scanner sc = new Scanner(System.in);
        int account = sc.nextInt();
        int password = sc.nextInt();
        int j = 2;
        int x = 0;
        int w = 0;
        if (account == 20519 && password == 123456) {
            System.out.println("""
                    欢迎使用智慧购物系统
                    请输入下面序号进行操作
                    1:增加商品         2:商品信息查询
                    3:我要购物         4:查看购物车
                    5:提交订单         6:删除订单
                    7:会员信息录入      8:会员信息查询
                    9:退出购物系统""");
            //声明数组类型并分配空间
            //str、price用来存储商品名称、商品价格;sno、birthday、integral用来存储会员号、会员生日、会员积分
            String[] str = new String[100];
            double[] price = new double[100];
            String[] sno = new String[100];
            String[] birthday = new String[100];
            double[] integral = new double[100];
            String[] str1 = new String[100];
            double[] price1 = new double[100];
            //为数组赋值
            str[0] = "泡面";
            str[1] = "火腿";
            price[0] = 122.5;
            price[1] = 4.5;
            double sum1 = 0;
            int a = 0;
            while (w == 0) {
                String i;
                i = sc.next();
                while ("1".equals(i)) {
                    System.out.println("继续操作请输入管理员密码");
                    password= sc.nextInt();
                    if (password==88888888) {
                        while (j < str.length) {
                            System.out.println("请输入商品名称");
                            str[j] = sc.next();
                            System.out.println("请输入商品价格");
                            price[j] = sc.nextDouble();
                            j++;
                            //结束while循环
                            break;
                        }
                        j = j++;
                        break;
                    } else if (password!=88888888) {
                        System.out.println("你不是管理员,无法操作");
                        break;
                    }
                    //i = sc.next();
                }
                while ("2".equals(i)){
                    System.out.println("商品名称" + "   " + "商品单价");
                    for (int v = 0; v <= (j - 1); v++) {
                        System.out.println(str[v] + "       " + price[v] + "元");
                    }
                    break;
                }
                while ("3".equals(i)) {
                    double sum = 0;
                    String e;
                    do {
                        System.out.println("请输入购买的商品");
                        e = sc.next();
                        //index是指定元素的下标
                        int index = printArray(str, e);
                        sum = sum + price[index];
                        //两个不同的方法交换数组的元素
                        String temp = str[index];
                        str[index] = str1[a];
                        str1[a] = temp;
                        str[index]=temp;
                        price1[a] = price1[a] + price[index];
                        price[index] = price1[a] - price[index];
                        price1[a] = price1[a] - price[index];
                        price[index] = price1[a] - price[index];
                        a = a + 1;
                        break;
                    } while (true);
                    //System.out.println(sum);
                    sum1 = sum + sum1;
                    //System.out.println(sum1);
                    System.out.println("请选择是否继续购物“y/n”");
                    String t=sc.next();
                    if ("y".equals(t)){
                        continue;
                    } else if ("n".equals(t)) {
                        break;
                    }
                }
                while ("4".equals(i)){
                    for (int v = 0; v <= (a - 1); v++) {
                        System.out.println(str1[v] + price1[v]);
                    }
                    System.out.println("合计:" + sum1 + "元");
                    break;
                }
                while ("5".equals(i)){
                    String r,e;
                    //获取当前系统的时间
                    Date date= new Date();
                    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    System.out.println("请选择是否会员“y/n”");
                    r = sc.next();
                    double[] integral1 = new double[1];
                    if ("y".equals(r)) {
                        System.out.println("请输入会员号");
                        e = sc.next();
                        int index = printArray(sno, e);
                        System.out.println("请输入支付金额");
                        double f= sc.nextDouble();
                        double sum11=sum1*0.88;
                        double f1=f-sum11;
                        integral1[0] = sum1 / 10.0;
                        integral[index] = integral[index] + integral1[0];
                        System.out.println("支付成功,欢迎再次光临"+"\n"+"支付时间:"+dateFormat.format(date)+"\n"
                                +"总金额"+sum1+"元"+"\n" +"会员享8.8折"+"\n"+"折后金额:"+sum11+"元"+"\n"
                                +"实付金额"+f+"元"+"\n"+"找零:"+f1+"元");
                        break;
                    } else if ("n".equals(r)) {
                        System.out.println("请输入支付金额");
                        double f= sc.nextDouble();
                        double f1=f-sum1;
                        System.out.println("支付成功,欢迎再次光临"+"\n"+"支付时间:"+dateFormat.format(date)+"\n"
                                +"总金额"+sum1+"元"+"\n" +"非会员"+"\n"+"实付金额"+f+"元"+"\n"+"找零:"+f1+"元");
                        break;
                    }
                    break;
                }
                //通过交换将数组存储的元素逐步清空,并将sum1和a重新赋值为0来删除订单
                while ("6".equals(i)){
                    for (;(a+1)>0;a--){
                        String []str2=new String[100];
                        double []price2=new double[100];
                        String temp1=str2[a];
                        str2[a]=str1[a];
                        str1[a]=temp1;
                        double temp2=price2[a];
                        price2[a]=price1[a];
                        price1[a]=temp2;
                        sum1= 0;
                    }
                    break;
                }
                while ("7".equals(i)) {
                    System.out.println("继续操作请输入管理员密码");
                    password= sc.nextInt();
                    if (password==88888888) {
                        while (x < sno.length) {
                            System.out.println("请输入会员号");
                            sno[x] = sc.next();
                            System.out.println("请输入会员生日");
                            birthday[x] = sc.next();
                            System.out.println("请输入会员积分");
                            integral[x] = sc.nextDouble();
                            x++;
                            break;
                        }
                        x = x++;
                        break;
                    }else if (password!=88888888) {
                        System.out.println("你不是管理员,无法操作");
                        break;
                    }
                }
                while ("8".equals(i)) {
                    System.out.println("会员号" + "  " + "会员生日" + "   " + "会员积分");
                    for (int v = 0; v <= (x - 1); v++) {
                        System.out.println(sno[v] + "    " + birthday[v] + "    " + integral[v] + "\n");
                    }
                    break;
                }
                //结束整个循环
                while ("9".equals(i)) {
                    return;
                }
                System.out.println("""
                        请输入下面序号进行操作
                        1:增加商品         2:商品信息查询
                        3:我要购物         4:查看购物车
                        5:提交订单         6:删除订单
                        7:会员信息录入      8:会员信息查询
                        9:退出购物系统
                        温馨提示购物前请输入6清空订单""");
            }
        }
    }
    //遍历数组
    private static int printArray(String[] arr, String e) {
        for (int i = 0; i < arr.length; i++) {
            if (Objects.equals(arr[i], e)) {
                return i;
            }
        }
        return 0;
    }
}

​

总结

一:数组

   1:数组的输入输出与数组元素交换

   2:获取数组下标

   3:数组的遍历

二:流程控制循环语句

   1:continue与break的用法

   2:if、for、while与do...while的区别

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晴星语心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值