69期-Java SE-007_面向对象-1-001-002

Posted hijackykun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了69期-Java SE-007_面向对象-1-001-002相关的知识,希望对你有一定的参考价值。

 

技术图片

 

Student.java

public class Student 
    //定义属性
    int id;
    String name;
    int age;
    int userFirstName;
    double myScore;
    //定义方法
    public static int add(int num1,int num2) 
        return num1+num2;
    
    
    public static void add2(int num1,int num2) 
        System.out.println(num1+num2);
    
    
    public static void show() 
        System.out.println("Hello World");
    
    
    public static void main(String[] args) 
        int num = add(1,1);
        add2(1,1);
    
    
    /**
     * 无参构造
     */
    public Student() 
        
    
    
    /**
     * 有参构造
     */
    public Student(int num) 
        
    
    
    public void test() 
        
    
    

 

Test.java

import java.util.Arrays;
import java.util.Scanner;

public class Test 
    public static void main(String[] args) 
        //初始化用户名
        String[] nameArray = "张三","李四","王五","小明";
        //初始化用户年龄
        int[] ageArray = 22,23,20,22;
        //初始化用户状态
        String[] stateArray = "正常","正常","正常","正常";
        Scanner scanner = new Scanner(System.in);
        int num;
        String name;
        int age;
        int index = 0;
        boolean flag;
        do 
            System.out.println("欢迎使用用户管理系统");
            System.out.println("1.查询用户");
            System.out.println("2.添加用户");
            System.out.println("3.删除用户");
            System.out.println("4.账号冻结");
            System.out.println("5.账号解封");
            System.out.println("6.退出系统");
            System.out.print("请选择:");
            num = scanner.nextInt();
            switch(num) 
            case 1:
                System.out.println("------ 查询用户 ------");
                System.out.println("编号\\t\\t姓名\\t\\t年龄\\t\\t状态\\t\\t");
                for(int i=0;i<nameArray.length;i++) 
                    System.out.println((i+1)+"\\t\\t"+nameArray[i]+"\\t\\t"+ageArray[1]+"\\t\\t"+stateArray[i]);
                
                System.out.print("输入0返回:");
                num = scanner.nextInt();
                break;
            case 2:
                System.out.println("------ 添加用户 ------");
                System.out.print("请输入用户姓名:");
                name = scanner.next();
                flag = isNameExist(name, nameArray);
                if(flag == true) 
                    System.out.println(name+"已存在!");
                else 
                    System.out.print("请输入用户年龄:");
                    age = scanner.nextInt();
                    nameArray = Arrays.copyOf(nameArray, nameArray.length+1);
                    nameArray[nameArray.length-1] = name;
                    ageArray = Arrays.copyOf(ageArray, ageArray.length+1);
                    ageArray[ageArray.length-1] = age;
                    stateArray = Arrays.copyOf(stateArray, stateArray.length+1);
                    stateArray[stateArray.length-1] = "正常";
                    System.out.println("添加成功!");
                
                System.out.print("输入0返回:");
                num = scanner.nextInt();
                break;
            case 3:
                System.out.println("------ 删除用户 ------");
                System.out.print("请输入要删除的用户名:");
                name = scanner.next();
                flag = isNameExist(name, nameArray);
                if(flag != true) 
                    System.out.println(name+"不存在,请重新输入!");
                else 
                    index = getIndex(name, nameArray);
                    String[] newNameArray = new String[nameArray.length-1];
                    int[] newAgeArray = new int[ageArray.length-1];
                    String[] newStateArray = new String[stateArray.length-1];
                    for(int i=0;i<index;i++) 
                        newNameArray[i] = nameArray[i];
                        newAgeArray[i] = ageArray[i];
                        newStateArray[i] = stateArray[i];
                    
                    for(int i=index;i<newNameArray.length;i++) 
                        newNameArray[i] = nameArray[i+1];
                        newAgeArray[i] = ageArray[i+1];
                        newStateArray[i] = stateArray[i+1];
                    
                    nameArray = newNameArray;
                    ageArray = newAgeArray;
                    stateArray = newStateArray;
                    System.out.println(name+"删除成功!");
                
                System.out.print("输入0返回:");
                num = scanner.nextInt();
                break;
            case 4:
                System.out.println("------ 账号冻结 ------");
                System.out.print("请输入要冻结的用户名:");
                name = scanner.next();
                flag = isNameExist(name, nameArray);
                if(!flag) 
                    System.out.println(name+"不存在,请重新输入!");
                else 
                    index = getIndex(name, nameArray);
                    if(stateArray[index].equals("冻结")) 
                        System.out.println(name+"已冻结!");
                    else 
                        stateArray[index] = "冻结";
                        System.out.println(name+"冻结成功!");
                    
                
                System.out.print("输入0返回:");
                num = scanner.nextInt();
                break;
            case 5:
                System.out.println("------ 账号解封 ------");
                System.out.print("请输入要解封的用户名:");
                name = scanner.next();
                //判断name是否存在
                flag = isNameExist(name, nameArray);
                if(!flag) 
                    System.out.println(name+"不存在,请重新输入!");
                else 
                    index = getIndex(name, nameArray);
                    if(stateArray[index].equals("正常")) 
                        System.out.println(name+"状态正常!");
                    else 
                        stateArray[index] = "正常";
                        System.out.println(name+"解封成功!");
                    
                
                System.out.print("输入0返回:");
                num = scanner.nextInt();
                break;
            case 6:
                System.out.println("感谢使用用户管理系统!");
                break;
            
        while(num == 0);
    
    
    //检测用户输入的名称是否存在
    public static boolean isNameExist(String name,String[] nameArray) 
        boolean flag = false;
        for(int i = 0; i < nameArray.length;i++) 
            if(name.equals(nameArray[i])) 
                flag = true;
                break;
            
        
        return flag;
    
    //如果用户名存在,获取其下标
    public static int getIndex(String name,String[] nameArray) 
        int index = 0;
        for(int i = 0; i < nameArray.length;i++) 
            if(name.equals(nameArray[i])) 
                index = i;
                break;
            
        
        return index;
    

 

User.java

public class User 
    //属性
    private long id;
    private String name;
    private char gender;
    private String password;
    
    
    

    public long getId() 
        return id;
    




    public void setId(long id) 
        this.id = id;
    




    public String getName() 
        return name;
    




    public void setName(String name) 
        this.name = name;
    




    public char getGender() 
        return gender;
    




    public void setGender(char gender) 
        this.gender = gender;
    




    public String getPassword() 
        return password;
    




    public void setPassword(String password) 
        this.password = password;
    




    public static void show() 
        System.out.println("用户信息如下:");
//        System.out.println(this.id);
//        System.out.println(this.name);
//        System.out.println(this.gender);
//        System.out.println(this.password);
    

 

UserTest.java

public class UserTest 
    public static void main(String[] args) 
//        User user = new User();
//        user.setId(-10);
//        user.setName("张三");
//        user.setGender(‘男‘);
//        user.setPassword("123123");
//        user.show();
        User.show();
    

 

以上是关于69期-Java SE-007_面向对象-1-001-002的主要内容,如果未能解决你的问题,请参考以下文章

69期-Java SE-009_面向对象-3-001-002

69期-Java SE-015_集合-1-001-002

69期-Java SE-016_集合-2-001-002

69期-Java SE-043_Servlet-1

69期-Java SE-004_循环数组-001-002

69期-Java SE-045_JSP-2