zoukankan      html  css  js  c++  java
  • day06_03

    随机点名器案例

    自定义类

    package com.tedu.Demo;
    
    /*
     * 描述学生的类
     *     属性:姓名、年龄
     *         
     * */
    public class StudentName {
        String name;
        int age;
    }

    在main中使用,定义StudentName类

     1 package com.tedu.Demo;
     2 
     3 /*
     4  * 随机点名器
     5  * 现实中有学生这个事物,使用定义类的形式,描述学生事物
     6  * 属性:姓名、年龄
     7  * 
     8  * 姓名存储看数组,将容器换成了集合
     9  * String[] s = {"",""};
    10  * 集合中,存储的是学生的姓名吗?应该存储的是student类型
    11  * 
    12  * 存储学生:
    13  *     学生类型,存储到集合中
    14  *     总览:遍历集合
    15  *     随机:随机数作为索引,到集合中找到元素
    16  * 三个功能,共享数据,集合容器
    17  * 定义三个方法,必须参数传递集合
    18  * 
    19  * */
    20 
    21 import java.util.ArrayList;
    22 
    23 public class CallName {
    24     public static void main(String[] args) {
    25         ArrayList<StudentName> array = new ArrayList<StudentName>();
    26         addArrayList(array);  //调用集合存储学生的方法
    27         printArrayList(array); //调用总览方法
    28         
    29         randomStudentName(array); // 调用随机数输出随机学生的信息
    30     }
    31 
    32     // 随机数学生姓名
    33     public static void randomStudentName(ArrayList<StudentName> array) {
    34         int index = (int) (Math.random() * array.size());
    35         StudentName s = array.get(index);
    36         System.out.println(s.name+"  "+s.age);
    37 
    38     }
    39 
    40     // 总览:遍历集合
    41     public static void printArrayList(ArrayList<StudentName> array) {
    42         for (int i = 0; i < array.size(); i++) {
    43             StudentName s = array.get(i);
    44             System.out.println(s.name + " " + s.age);
    45 
    46         }
    47     }
    48 
    49     // 将学生类型存储到集合中
    50     public static void addArrayList(ArrayList<StudentName> array) {
    51         StudentName sn1 = new StudentName();
    52         StudentName sn2 = new StudentName();
    53         StudentName sn3 = new StudentName();
    54         StudentName sn4 = new StudentName();
    55         StudentName sn5 = new StudentName();
    56 
    57         sn1.name = "张三";
    58         sn1.age = 20;
    59 
    60         sn2.name = "张三1";
    61         sn2.age = 201;
    62 
    63         sn3.name = "张三2";
    64         sn3.age = 202;
    65 
    66         sn4.name = "张三3";
    67         sn4.age = 203;
    68 
    69         sn5.name = "张三4";
    70         sn5.age = 204;
    71 
    72         array.add(sn1);
    73         array.add(sn2);
    74         array.add(sn3);
    75         array.add(sn4);
    76         array.add(sn5);
    77     }
    78 
    79 }

    库存管理案例

    自定义商品类:

     1 package com.tedu.Demo;
     2 
     3 public class Goods {
     4     //定义商品名字
     5     String brand;
     6     //定义大小
     7     double size;
     8     //定义商品价格
     9     double price;
    10     //定义库存数量
    11     int count;
    12 }

    主方法:

      

    package com.tedu.Demo;
    
    import java.util.*;
    
    /*
     实现库存管理案例:
     1. 存储商品信息
     存储商品类型变量
     将商品类型的变量,存储到集合中
    
     2. 查看库存清单
     将集合进行遍历, 获取出集合中存储的Goods类型变量
     输出每一个Goods类型的属性
     计算求和: 总库存,总金额
    
     3. 修改商品的库存
     集合遍历 ,获取出集合中存储的Goods类型变量
     变量调用Goods类的属性count,值进行修改 (键盘输入)
     */
    public class Shopp {
        public static void main(String[] args) {
            ArrayList<Goods> array = new ArrayList<Goods>();
            addGoods(array);
            
            while(true){
                int number = chooseFunction();
                
                switch(number){
                case 1:
                    printGoods(array);
                    break;
                case 2:
                    update(array);
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无此功能!");
                    break;
                }
            }
        }
        
        //定义方法,实现选择菜单,用户根据功能选择菜单
        public static int chooseFunction(){
            System.out.println("-------------库存管理------------");
            System.out.println("1.查看库存清单");
            System.out.println("2.修改商品库存数量");
            System.out.println("3.退出");
            System.out.println("请输入要执行的操作序号:");
            Scanner scan = new Scanner(System.in);
            int number = scan.nextInt();
            return number;
        }
        
        //修改商品的库存
        public static void update(ArrayList<Goods> array){
            Scanner scan = new Scanner(System.in);
            for(int i=0;i<array.size();i++){
                //集合方法get获取的是集合的元素,元素类型Goods
                Goods g = array.get(i);
                System.out.println("请输入"+g.brand+"的数量");
                //Goods属性,count进行修改
                g.count = scan.nextInt();
            }
        }
    
        // 定义方法,查看库存清单
        public static void printGoods(ArrayList<Goods> array) {
            // 输出表头
            System.out.println("----------商场库存清单----------");
            System.out.println("品牌型号                         尺寸                             价格                       库存数");
            int totalCount = 0;
            double totalPrice = 0;
            for (int i = 0; i < array.size(); i++) {
                Goods g = array.get(i);
                System.out.println(g.brand + "   " + g.size + "     " + g.price
                        + "    " + g.count);
                totalCount += g.count;
                totalPrice = g.count*g.price;
            }
            System.out.println("总库存:"+totalCount);
            System.out.println("总价值:"+totalPrice);
        }
    
        // 定义方法,将商品的信息存储的集合中
        public static void addGoods(ArrayList<Goods> array) {
            // 创建商品类型变量 Goods类型的变量
            Goods g1 = new Goods();
            Goods g2 = new Goods();
    
            g1.brand = "MacBook";
            g1.size = 13.3;
            g1.price = 8999.88;
            g1.count = 5;
    
            g2.brand = "ThinkPad";
            g2.size = 15.6;
            g2.price = 5999.88;
            g2.count = 2;
    
            // goods类型的变量存到集合中
            array.add(g1);
            array.add(g2);
        }
    }
  • 相关阅读:
    大胆决定+细致用功, 才可能改变命运
    采用用同步编程的方式实现跨进程异步获取数据
    关于DataGridViewComboBoxCell修改后提交数据源
    学习:如何具有坚韧的性格
    汉语-成语:坚韧不拔
    汉语-词语:坚韧
    阿里云-OSS-Help-SKD参考-.NET:前言
    阿里云-OSS-OSS管理控制台:Bucket列表
    阿里云-OSS-OSS管理控制台:概览
    汉语-词语:推理
  • 原文地址:https://www.cnblogs.com/msn-z/p/6959219.html
Copyright © 2011-2022 走看看