Person
unknown
plain_text
2 years ago
5.2 kB
6
Indexable
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package vo7; import java.util.Scanner; /** * * @author HP */ public class Person { private String name; private float salary; private String address; Scanner sc = new Scanner(System.in); /** * constructor with no parameters * */ public Person() { } /** * constructor with parameters * * @param name * @param salary * @param address */ public Person(String name, float salary, String address) { this.name = name; this.salary = salary; this.address = address; } /** * getName * * @return */ public String getName() { return name; } /** * Set name * * @param name */ public void setName(String name) { this.name = name; } /** * getSalary * * @return */ public float getSalary() { return salary; } /** * setSalary * * @param salary */ public void setSalary(float salary) { this.salary = salary; } /** * getAddress * * @return */ public String getAddress() { return address; } /** * setAddress * * @param address */ public void setAddress(String address) { this.address = address; } /** * Enter the name, will error and re-enter it when nothing is entered and * enter the number for the name * * @param scanner * @param message * @return */ public String getInputName(Scanner scanner, String message) { String name; boolean valid; do { //The loop will repeat if wrong data is entered valid = true; System.out.print(message); name = scanner.nextLine().trim(); if (name.matches("[a-zA-Z]+")) { valid = true; } else if (name.equals("")) { // check if the user has entered anything System.out.println("Error: Your input can not be empty!"); valid = false; } else if (name.matches("[0-9]+")) { //check if the name has a number or not System.out.println("Error: Your input can not be number!"); valid = false; } else { // check if the name has character System.out.println("Error: Your input can not be character!"); valid = false; } } while (valid == false); return name; } /** * Enter a string of characters, an error will be displayed when nothing is * entered * * @param scanner * @param message * @return */ public String getInputString(Scanner scanner, String message) { String string; do { //The loop will repeat if wrong data is entered System.out.print(message); string = scanner.nextLine().trim(); if (string.equals("")) { // check if the user has entered anything System.out.println("Error: Your input can not be empty!"); } } while (string.equals("")); return string; } /** * Enter salary, it will display an error and re-enter it when entering a * number less than zero, enter text and enter nothing * * @param scanner * @param message * @return */ public float getInputSalary(Scanner scanner, String message) { Float input = null; while (input == null) { try { input = Float.parseFloat(getInputString(scanner, message)); if (input < 0) { //salary must be greater than 0 System.out.println("Error: The salary must be greater than 0 !"); input = null; } } catch (NumberFormatException e) { //salary can't be word System.out.println("Error: The salary must be a number !"); } } return input; } /** * Enter data for the object including name, address and salary * */ public void inputPersonInfo() { System.out.println("Input Information of Person"); this.name = getInputName(sc, "Please input name: "); this.address = getInputString(sc, "Please input address: "); this.salary = getInputSalary(sc, "Please input salary: "); } /** * Display information on the screen including name, address and salary * */ public void outputPersonInfo() { System.out.println("Information of Person you have entered:"); System.out.println("Name: " + this.getName()); System.out.println("Address: " + this.getAddress()); System.out.println("Salary: " + this.getSalary()); System.out.println(""); } }
Editor is loading...