Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a limited app. to track a person’s first name, last name, and age. Desig

ID: 3629057 • Letter: D

Question

Develop a limited app. to track a person’s first name, last name, and age.
Design and implement an object to record the info. Use Scanner methods and an ArrayList for storage. using java class Human
Application should allow:
-person to ADD new human
-DELETE human- prompting user for last name
-DISPLAY human list with first/last name and age and average age of human(s) in list
-SAVE the output to a file, prompt user for filename
-END the application.
-Prompted (ADD, DELETE, DISPLAY, SAVE, END) - **Case insensitive. all needs to be capital.

Example output:
What operation next? (ADD, DELETE, DISPLAY, SAVE, END)? ADD
Please enter Hunam's Last name: Doe
Please enter Human's First name: Jane
Please enter Human's Age: 43
Added!
Jane Doe: Age: 43
What operation next...

So far I have in my player class
private String lastName;
private String firstName;
private int age;
//have for each last name, first and age
public void setLastname(String last){
}
public void getLastName(){
return lastName;}

This is my first time programming something with a main and class.. I do not understand how to incorporate both. My professor confuses me and my book does not display good examples for me to understand. My major problem right now is the case insensitive and the ArrayList. Thanks!

Explanation / Answer

This should be your class:

package myPackage;
import java.util.*;
import java.io.*;

public class Human{
    private Scanner kboard= new Scanner(System.in);
    private static ArrayList mankind= new ArrayList();
    private String lastname;
    private String firstname;
    private int age;
    //constructor (you can set this to private if you would allow only the ADD method)
    public Human(){

    }
    public Human(String lastname, String firstname, int age){
        this.firstname=firstname;
        this.lastname=lastname;
        this.age=age;
    }
    //Accessors
    public String getFirstname(){
        return firstname;
    }
    public String getLastname(){
        return lastname;
    }
    public int getAge(){
        return age;
    }
    //Mutators
    public void setLastname(String last){
        lastname=last;
    }
    public void setFirstname(String first){
        firstname=first;
    }
    public void setAge(int age){
        this.age=age;
    }
    @Override
    public String toString(){
        return "NAME: "+lastname+", "+firstname+" AGE: "+age;
    }
    public void ADD(){
        System.out.print("Please enter Humam's Last name: ");
        String Lname=kboard.next();
        System.out.print("Please enter Human's First name: ");
        String Fname=kboard.next();
        System.out.print("Please enter Human's Last name: ");
        int Age=kboard.nextInt();
        Human newborn = new Human(Lname,Fname,Age);
        //Storing human in the list
        mankind.add(newborn);
        System.out.println("ADDED!");
    }
    public void DELETE(){
        System.out.print("Please enter Human's Last name: ");
        String lname= kboard.next();
        boolean kill = false;
        Human[] temp = new Human[mankind.size()];
        mankind.toArray(temp);
        for(int i=0; i<temp.length; i++){
            if(lname.equals(temp[i].getLastname())){
                mankind.remove(i);
                kill=true;
            }
        }

        if(kill)
            System.out.println("DELETED!");
        else
            System.out.println("Element not found");
    }
    public void DISPLAY(){
        int average=0;
        Human[] temp = new Human[mankind.size()];
        mankind.toArray(temp);
        for(int i=0;i<temp.length; i++){
            System.out.println(temp[i]);
            average+=temp[i].getAge();
        }
        average/=temp.length;
        System.out.println(" The Average age is: "+average);
    }
    public void SAVE() throws IOException{
        System.out.print("Please insert file path: ");
        String path=kboard.next();
        Human[] temp = new Human[mankind.size()];
        mankind.toArray(temp);
        FileWriter filew = new FileWriter(path+"/file.txt");
        for(int i=0; i<temp.length; i++)
            filew.write(temp[i].toString()+" ");
        filew.close();
        System.out.println("SAVED!");
    }
    public void END(){
        System.out.println("--- APPLICATION TERMINATED ---");
        System.exit(0);
    }
}

Classes allow you to create new custom tools for your main application. for instance Scanner is the class that contains all the parameters and methods that I can use through the Scanner object.

The main class can be implemented as you want, here the one that I used for testing purpose.

package myPackage;
import java.io.IOException;
import java.util.Scanner;

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

        String command;
        Scanner kboard = new Scanner(System.in);
        Human pers = new Human();
        while(true){
            System.out.print("What you wish to do?(ADD, DELETE, DISPLAY, SAVE, END): ");
            command=kboard.next();
           
            // I tried to do not use ENUM :)
            command=command.toUpperCase();
            switch(command.charAt(1)){
                case 'D':
                    pers.ADD();
                    break;
                case 'E':
                    pers.DELETE();
                    break;
                case 'I':
                    pers.DISPLAY();
                    break;
                case 'A':
                    pers.SAVE();
                    break;
                case 'N':
                    pers.END();
                default:
                    System.out.println("Command not found.");
            }
        }
    }

}