When writing Java programs, privacy such as age cannot be checked at will.

When writing Java programs, you cannot randomly check the user's age, salary and other privacy, and conduct reasonable verification of the set age. Set the age if it is reasonable, otherwise the default age is given, which must be between 1 and 150. Age and salary cannot be viewed directly, and the name length is between 2 and 5 characters.

First create a class and define variables such as name, age, salary, etc. in this class.

package test01;

public class employee {
	public String name; // 姓名
	private int age; // 年龄
	private double salary; // 工资

Then encapsulate the variables just now

    //封装姓名
    public String getName() {
		return name;
	}

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

	// 封装年龄
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
			this.age = age;
	}

	// 封装工资
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary=salary;
	}

The question says that variables should be reasonably verified, so IF is used to make judgments under the method public void...

Because the names of ethnic minorities in Xinjiang are generally very long, I increased the condition to 20 characters.

The age is 0-150 as mentioned in the question, with a default value of 3 years old.

Finally, because all information needs to be output, a printInfo() method is created

public String getName() {
		return name;
	}

	public void setName(String name) {
		if (name.length() >= 0 && name.length() <= 5) {
			this.name = name;
		} else if (name.length() <= 20) {
			this.name = name;
			System.out.println("哇~ 你名字这么长");
		} else if (name.length() > 20) {
			System.out.println("过分了啊!达芬奇全名都没你这么长, (→_→)");
		}
	}

	// 封装年龄
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		if (age >= 0 && age <= 150) {
			System.out.println("好了~ ,你的信息录入好了");
			this.age = age;
		} else if (age < 0) {
			System.out.println("你还是人吗?(╯▔皿▔)╯,给你默认岁数3岁 (→_→)");
            this.age = 3;
		} else if (age > 150) {
			System.out.println("你是吸血鬼吗?活这么久!给你默认岁数3岁 (→_→)");
            this.age = 3;
		}
	}

	// 封装工资
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {	
		this.salary=salary;
	}
    public void printInfo() {
		System.out.println("姓名:" + name + "\t\t" + "年龄:" + age + "\t\t" + "工资:"+salary);
	}
}

Create a class to output the variables just now

package test01;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);//键盘录入

        employee em = new employee();// 封装的类
        System.out.print("请输入姓名:");
        int a = sc.nextInt();
		em.setName(a);// 输出姓名
        System.out.print("请输入年纪:");
        int b = sc.nextInt();
        em.setAge(b);// 输出年纪

        em.printInfo();

The output is: 

Please enter your name: Xiao Ming

Please enter your age: 20

Name: Xiao Ming Age: 20 Salary: 0.0

 The title says that information such as age cannot be viewed at will, so I want to give him a function that requires a login password to view information and a function that allows him to press key 1 to enter information and key 2 to view information.

First create a new class to write the login password page

package test01;

import java.util.Scanner;

public class password {
	private String[] userBox = { "qixian", "123456" };// 数据库存储的账号和密码,private修饰,只能在本类中使用

	public void secretKey() {
		password t = new password(); // 创建一个类的对象
		Scanner input = new Scanner(System.in);

		boolean userNameExist = false; // 用户名已存在标志
		boolean loginSuccess = false; // 账号和密码输入都正确,则登录成功标志
		do {
			System.out.println("请输入账号:");
			String user = input.nextLine(); // 获取用户输入的账号

			System.out.println("请输入密码:");
			String password = input.nextLine();// 获取用户输入的密码

			if (t.userBox[0].equals(user)) { // 输入的账号和数据库存在的用户名比较
				if (t.userBox[1].equals(password)) {// 输入的密码和数据库存在的密码比较
					System.out.println("恭喜您登录成功!");
					loginSuccess = true;
					// 在这里插入数据
				} else {
					loginSuccess = false;
					System.out.println("账号或者密码输入错误,请重新输入!");
				}
				userNameExist = true;
			} else {
				userNameExist = false;
			}

			if (!userNameExist == true) {
				System.out.println("用户名不存在,请重新输入!");
			}
		} while ((!userNameExist == true) || (!loginSuccess == true));// 用户名或者密码任意一个错误,都重新输入,两者输入正确,则退出do{}while();继续往下执行
	}
}

Suddenly it occurred to me that I wanted to add a function to the code:

When logging in with the password, press "+" to pay the other party wages, and press "-" to deduct wages.

So a new class was created to write

package test01;

import java.util.Scanner;

public void access() {
				
		Scanner sc = new Scanner(System.in);
		employee em = new employee();// 封装的类
				
		System.out.println("发工资按(+)号,扣钱按(-)号");
		double salary;
		char income = sc.next().charAt(0);

		if (income == '+') {
			System.out.println("对方工资是多少元");
			double c = sc.nextInt();
			salary = c;
			em.setSalary(salary);
			System.out.println("工资已发过去,请注意提醒员工!");
		} else if (income == '-') {
			System.out.println("您要扣多少元");
			double c = sc.nextInt();
			salary = c;
			em.setSalary(salary);
			System.out.println("金额已被对方工资中扣出,请注意提醒员工!");
		}
	}

Before completing the second function, use the printInfo() method in the Interface class to prompt what functions are available.

public void printInfo() {
		System.out.println("录入信息,		按1号键");
		System.out.println("查看信息,		按2号键");
		System.out.println("发工资或扣工资,	按3号键");
		System.out.println("推出程序,		按4号键");
		System.out.println("请输入(1-4)");
	}

Finally, a do-while loop is used to complete the second function.

package test01;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		employee em = new employee();// 封装的类
		Interface as = new Interface();
		password sd = new password();// 用户登录的类

		String key = ""; // 键盘录入环节
		boolean loop = true; // 用在死循环上

		as.printInfo();

		do {
			key = sc.next();
			switch (key) {
			case "1": // 录入信息
				System.out.print("请输入姓名:");
				String a = sc.next();
				em.setName(a);

				System.out.print("请输入年纪:");
				int b = sc.nextInt();
				em.setAge(b);
				
				System.out.print("您的信息已经录入好了,按2号键可以查看信息");
				break;

			case "2": // 查看信息
				sd.secretKey();
				em.printInfo();
				break;

			case "3": // 发,扣工资
				sd.secretKey();
				as.access();

				break;

			case "4": // 退出程序
				String choice = "";
				while (true) {
					System.out.println("你确定要退出吗? 请输入YES或NO");
					choice = sc.next();
					if (choice.equals("YES") || choice.equals("NO")) { // A
						break;
					}
				}
				if (choice.equals("YES")) {
					loop = false;
				}
				break;
			default:
				System.out.println("输入有误!请重新选择!");
			}

		} while (loop);

		System.out.println("系统已推出!");
	}
}

// Thank you for reading this. I am a novice in the programming world. This is also the first complete small project that I have completed independently. It is also the first time to write a blog. There may be bugs in many places in the code, or the writing is not correct. Very good. If you have better opinions, I hope the seniors can give them some advice. Thank you.

Below is the full code

package test01;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		employee em = new employee();// 封装的类
		Interface as = new Interface();
		password sd = new password();// 用户登录的类

		String key = ""; // 键盘录入环节
		boolean loop = true; // 用在死循环上

		as.printInfo();

		do {
			key = sc.next();
			switch (key) {
			case "1": // 录入信息
				System.out.print("请输入姓名:");
				String a = sc.next();
				em.setName(a);

				System.out.print("请输入年纪:");
				int b = sc.nextInt();
				em.setAge(b);
				
				System.out.print("您的信息已经录入好了,按2号键可以查看信息");
				break;

			case "2": // 查看信息
				sd.secretKey();
				em.printInfo();
				break;

			case "3": // 发,扣工资
				sd.secretKey();
				as.access();

				break;

			case "4": // 退出程序
				String choice = "";
				while (true) {
					System.out.println("你确定要退出吗? 请输入YES或NO");
					choice = sc.next();
					if (choice.equals("YES") || choice.equals("NO")) { // A
						break;
					}
				}
				if (choice.equals("YES")) {
					loop = false;
				}
				break;
			default:
				System.out.println("输入有误!请重新选择!");
			}

		} while (loop);

		System.out.println("系统已推出!");
	}
}

package test01;

public class employee {
	public String name; // 姓名
	private int age; // 年龄
	private double salary; // 工资

	// 封装姓名
	public String getName() {
		return name;
	}

	public void setName(String name) {
		if (name.length() >= 0 && name.length() <= 5) {
			this.name = name;
		} else if (name.length() <= 20) {
			this.name = name;
			System.out.println("哇~ 你名字这么长");
		} else if (name.length() > 20) {
			System.out.println("过分了啊!达芬奇全名都没你这么长,按1号键重写 (→_→)");
		}
	}

	// 封装年龄
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		if (age >= 0 && age <= 150) {
			this.age = age;
		} else if (age < 0) {
			System.out.println("你还是人吗?(╯▔皿▔)╯,给你默认岁数3岁(→_→)");
			this.age = 3;
		} else if (age > 150) {
			System.out.println("你是吸血鬼吗?活这么久!给你默认岁数3岁(→_→)");
			this.age = 3;
		}
	}

	// 封装工资
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {		
		this.salary=salary;
	}

	public void printInfo() {
		System.out.println("姓名:" + name + "\t\t" + "年龄:" + age + "\t\t" + "工资:"+salary);
	}
}
package test01;

import java.util.Scanner;

public class Interface {
	public void printInfo() {
		System.out.println("录入信息,		按1号键");
		System.out.println("查看信息,		按2号键");
		System.out.println("发工资或扣工资,	按3号键");
		System.out.println("推出程序,		按4号键");
		System.out.println("请输入(1-4)");
	}
	
	public void access() {
				
		Scanner sc = new Scanner(System.in);
		employee em = new employee();// 封装的类
				
		System.out.println("发工资按(+)号,扣钱按(-)号");
		double salary;
		char income = sc.next().charAt(0);

		if (income == '+') {
			System.out.println("对方工资是多少元");
			double c = sc.nextInt();
			salary = c;
			em.setSalary(salary);
			System.out.println("工资已发过去,请注意提醒员工!");
		} else if (income == '-') {
			System.out.println("您要扣多少元");
			double c = sc.nextInt();
			salary = c;
			em.setSalary(salary);
			System.out.println("金额已被对方工资中扣出,请注意提醒员工!");
		}
	}
}
package test01;

import java.util.Scanner;

public class password {
	private String[] userBox = { "mrziyulla", "123456" };// 数据库存储的账号和密码,private修饰,只能在本类中使用

	public void secretKey() {
		password t = new password(); // 创建一个类的对象
		Scanner input = new Scanner(System.in);

		boolean userNameExist = false; // 用户名已存在标志
		boolean loginSuccess = false; // 账号和密码输入都正确,则登录成功标志
		do {
			System.out.println("请输入账号:");
			String user = input.nextLine(); // 获取用户输入的账号

			System.out.println("请输入密码:");
			String password = input.nextLine();// 获取用户输入的密码

			if (t.userBox[0].equals(user)) { // 输入的账号和数据库存在的用户名比较
				if (t.userBox[1].equals(password)) {// 输入的密码和数据库存在的密码比较
					System.out.println("恭喜您登录成功!");
					loginSuccess = true;
					// 在这里插入数据
				} else {
					loginSuccess = false;
					System.out.println("账号或者密码输入错误,请重新输入!");
				}
				userNameExist = true;
			} else {
				userNameExist = false;
			}

			if (!userNameExist == true) {
				System.out.println("用户名不存在,请重新输入!");
			}
		} while ((!userNameExist == true) || (!loginSuccess == true));// 用户名或者密码任意一个错误,都重新输入,两者输入正确,则退出do{}while();继续往下执行
	}
}

Supongo que te gusta

Origin blog.csdn.net/m0_69655483/article/details/127610836
Recomendado
Clasificación