I have this project ive been working on all week and cannot get the search in Main.java in the switch case 3 to work. Any idea why this will not display??
Here it all is :(
Main.Java
package hartman;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Printer.printWelcome();
Scanner keyboard = new Scanner(System.in);
ArrayList<Person> personList = new ArrayList<>();
boolean keepRunning = true;
while (keepRunning) {
Printer.printMenu();
Printer.printPrompt("Please enter your operation: ");
String userSelection = keyboard.nextLine();
switch (userSelection) {
case "1":
Database.addPerson(personList);
break;
case "2":
Database.printDatabase(personList);
break;
case "3":
Printer.printSearchPersonTitle();
String searchFor = keyboard.nextLine();
Database.findPerson(searchFor);
Printer.printPersonList(personList);
break;
case "4":
keepRunning = false;
break;
default:
break;
}
}
Printer.printGoodBye();
keyboard.close();
}
}
Database.Java
package hartman;
import java.util.ArrayList;
import java.util.Scanner;
public class Database {
static Scanner keyboard = new Scanner(System.in);
private static ArrayList<Person> personList = new ArrayList<Person>();
public Database() {
}
public static void addPerson(ArrayList<Person> personList) {
Printer.printAddPersonTitle();
Printer.printPrompt(" Enter first name: ");
String addFirstName = keyboard.nextLine();
Printer.printPrompt(" Enter last Name: ");
String addLastName = keyboard.nextLine();
Printer.printPrompt(" Enter social Security Number: ");
String addSocial = keyboard.nextLine();
Printer.printPrompt(" Enter year of birth: ");
int addYearBorn = Integer.parseInt(keyboard.nextLine());
System.out.printf("\n%s, %s saved!\n", addFirstName, addLastName);
Person person = new Person();
person.setFirstName(addFirstName);
person.setLastName(addLastName);
person.setSocialSecurityNumber(addSocial);
person.setYearBorn(addYearBorn);
personList.add(person);
}
public static void printDatabase(ArrayList<Person> personList) {
System.out
.printf("\nLast Name First Name Social Security Number Age\n");
System.out
.printf("=================== =================== ====================== ===\n");
for (Person p : personList) {
System.out.printf("%-20s%-21s%-24s%s\n", p.getLastName(),
p.getLastName(), p.getSocialSecurityNumber(), p.getAge());
}
}
public static ArrayList<Person> findPerson(String searchFor) {
ArrayList<Person> matches = new ArrayList<>();
for (Person p : personList) {
boolean isAMatch = false;
if (p.getFirstName().equalsIgnoreCase(searchFor)) {
isAMatch = true;
} else if (p.getLastName().equalsIgnoreCase(searchFor)) {
isAMatch = true;
} else if (p.getSocialSecurityNumber().contains(searchFor)) {
isAMatch = true;
;
} else if (String.format("%d", p.getAge()).equals(searchFor)) {
isAMatch = true;
}
if (isAMatch) {
matches.add(p);
}
Printer.printPersonList(matches);
}
return matches;
}
}
Person.Java
package hartman;
public class Person {
private String firstName;
private String lastName;
private String socialSecurityNumber;
private int yearBorn;
public Person() {
}
public Person(String firstName, String lastName,
String socialSecurityNumber, int yearBorn) {
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
this.yearBorn = yearBorn;
}
public int getAge() {
return yearBorn = 2014 - yearBorn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSocialSecurityNumber() {
return socialSecurityNumber;
}
public void setSocialSecurityNumber(String socialSecurityNumber) {
this.socialSecurityNumber = socialSecurityNumber;
}
public int getYearBorn() {
return yearBorn;
}
public void setYearBorn(int yearBorn) {
this.yearBorn = yearBorn;
}
}
Printer.Java
package hartman;
import java.util.ArrayList;
public class Printer {
public static void printWelcome() {
System.out.printf("WELCOME TO PERSON DATABASE!\n");
}
public static void printGoodBye() {
System.out.printf("\nGOOD BYE!!\n");
}
public static void printMenu() {
System.out.printf("\nMain Menu\n");
System.out.printf("---------\n\n");
System.out.printf(" 1. Add a new Person to the database.\n");
System.out.printf(" 2. Print the database.\n");
System.out.printf(" 3. Search for a person in the database.\n");
System.out.printf(" 4. Exit the application.\n");
System.out.printf("\n");
}
public static void printPrintMenu() {
System.out.printf("Print\n\n");
}
public static void printAddPersonTitle() {
System.out.printf("\nAdd Person to Database\n\n");
}
public static void printPrompt(String promptForWhat) {
System.out.printf("%s", promptForWhat);
}
public static void printPersonSaved(Person personSaved) {
System.out.printf("%s", personSaved);
}
public static void printSearchPersonTitle() {
System.out.printf("\nSearch for Person in Database\n\n");
System.out.printf("Enter search value: ");
}
public static void printPersonList(ArrayList<Person> personListToPrint) {
System.out
.printf("\nLast Name First Name Social Security Number Age\n");
System.out
.printf("=================== =================== ====================== ===\n");
for (Person p : personListToPrint) {
System.out.printf("%-20s%-21s%-24s%s\n", p.getLastName(),
p.getLastName(), p.getSocialSecurityNumber(), p.getAge());
}
}
}
Any help would be great... im about to break my pc and give up.
The problems is that you are using the static instance from DataBase
ArrayList<Person> personList = new ArrayList<Person>();
I was gonna say follow the rules of OOP click here if you want to learn
But since you already started a lot of coding ill try to answer your problem..
Dont use instance of personList in the DataBase. Just use from the Main class.
here is where the problem started:
for (Person p : personList)
You are using an empty ArrayList from DataBase class..
The one that has data and you are using is from the Main class..
solution:
Remove the
private static ArrayList<Person> personList = new ArrayList<Person>();
from your DataBase then do this in you Main Class
public class Main {
public static ArrayList<Person> personList;
public static void main(String[] args) {
Printer.printWelcome();
Scanner keyboard = new Scanner(System.in);
personList = new ArrayList<Person>();
and change the for loop to:
for (Person p : Main.personList)
This is what you get when you dont use OOP on your problems its soo ugly and complicated..
Its better when it is object oriented.. P.S. learn that..
Related
i am new to java and i need some help. I want to create a login screen in command line.I have a menu class where at first i want the user to choose if he wants to login, sign up or exit. I am in the sign up case, where he puts his email,password and his identity, which is either Buyer or Owner(they are subclasses of a User class both). Buyer and Owner are two classes of my java project. I have thought of making an extra class Person(subclass to User), with variables String email, int password, User identity and i thought when the user of the eshop writes "Buyer" at the identity variable, the identity becomes buyer, so i can add him to an arraylist
If someone thinks that i should do it differently, i am glad to hear it.
import java.util.ArrayList;
import java.util.Scanner;
public class Menu{
Eshop Eshop = null;
ArrayList<Item> itemsList = new ArrayList<>();
ArrayList<Buyer> buyersList = new ArrayList<>();
User buyer = null;
User Owner = null;
boolean exit;//false
/* public static void main(String[] args){
Menu menu = new Menu();
menu.runMenu();
}
TEMPORARY MAIN TO RUN THE METHODS OF MENU CLASS
*/
Menu()
{
}
Menu(Eshop Eshop, User owner, ArrayList<Item> itemslist, ArrayList<User> buyerslist){
this.Eshop=Eshop;
this.Owner=owner;
itemsList = itemslist;
buyersList = buyerslist;
}
public void runMenu(){
printHeader();
while(!exit){
printMenu();
int choice = getInput();
performAction(choice);
}
}
public int getInput(){
Scanner kb = new Scanner(System.in);
int choice=-1;
while(choice<1 || choice >3){
try{
System.out.print("\n\nEnter your choice:");
choice = Integer.parseInt(kb.nextLine());
}
catch(NumberFormatException e){
System.out.println("Invalid selection plz try again.");
}
}
return choice;
}
public void printHeader(){
System.out.println("+-------------------------------------------+");
System.out.println("| |");
System.out.println("| Welcome to our Eshop Menu |");//+Eshop.getname();
System.out.println("| |");
System.out.println("+-------------------------------------------+");
}
public void printMenu(){
System.out.println("\nPlease make a selection:");
System.out.println("1)Login ");
System.out.println("2)Sign up");
System.out.println("3)Exit!!!!");
}
public void enteremail()
{
Scanner keyboard = new Scanner(System.in);
Scanner codeboard = new Scanner(System.in);
Scanner identityboard = new Scanner(System.in);
String email=null,identity=null;
int code=0;
System.out.println("\n\nEnter your email:");
email=keyboard.nextLine();
System.out.println("\nEnter your code:");
code=codeboard.nextInt();
System.out.println("\nEnter what you are Buyer or Owner");
identity=identityboard.nextLine();
Person p1= new Person(email,code,identity);
if(identity.equals("Buyer"))
buyersList.add(p1);//HERE IS MY PROBLEM HOW I MAKE THE IDENTITY OF PERSON CLASS TO BUYER
}
private void performAction(int choice){
switch(choice){
case 3:
exit = true;
System.out.println("Sta tsakidia!!!");
case 2:
}
}
}//THIS IS THE MENU CLASS
import java.util.ArrayList;
import java.io.*;
public class Person extends User{
int password=0;
String identity;
ArrayList <Person> personList=new ArrayList<Person>();
Person(String email,int password,String identity)
{
this.email=email;
this.password=password;
this.identity=identity;
}
public void Register(Person a)
{
personList.add(a);
}
public void setemail(String email)
{
this.email=email;
}
public String getemail()
{
return email;
}
public void setpassword(int password)
{
this.password=password;
}
public int getpassword()
{
return password;
}
public void setidentity(String identity)
{
this.identity=identity;
}
public String getidentity()
{
return identity;
}
}// THIS IS THE PERSON CLASS
public class Owner extends User
{
private boolean isAdmin;
Owner(String name,String email)
{
this.name=name;
this.email=email;
this.isAdmin=true;
}
}//Owner class
import java.io.*;
public class Buyer extends User
{
private int bonus=0,distance=0;
private double transfercost=0;//metaforika
protected buyerCategory category;//katigoria
private int pointcounter=0;//pointer twn agorwn
enum buyerCategory//Enum gia tin katigoria
{
BRONZE, SILVER,GOLD
}
Buyer(String name, String email,int bonus, ShoppingCart cart,int distance)//DHMIOURGOS
{
this.name=name;
this.email=email;
this.bonus=bonus;
if(bonus>=0 && bonus<100)
category=buyerCategory.BRONZE;
else if(bonus>100 && bonus<200)
category=buyerCategory.SILVER;
else if(bonus<0)
throw new IllegalArgumentException("Bonus has to positive or 0");
else
category=buyerCategory.GOLD;
this.category=category;
if(distance>=0 && distance<10)
transfercost=5;
else if(distance>=10 && distance<100)
transfercost=10;
else if(distance>=100)
transfercost=25;
else
System.out.println("Prepei na baleis thetiko distance");//Thelei exception
/*if(category == buyerCategory.SILVER)
transfercost=transfercost/2;
if(category == buyerCategory.GOLD)
transfercost=0;
*/
}
public void setbuyerCategory(Buyer buyer)
{
int temp;
temp=buyer.getbonus();
if (temp>=0 && temp<100)
category= buyerCategory.BRONZE;
if (temp>100 && temp<200)
category= buyerCategory.SILVER;
//transfercost = transfercost/2;
if (temp>200)
category= buyerCategory.GOLD;
//transfercost = 0;
}
public void awardBonus(Buyer buyer,ShoppingCart cart)
{
cart.CountShoppingCart(cart);
System.out.println("H aksia tis paraggelias sou einai:\t"+cart.calculateNet());
int ten=(int) cart.calculateNet()/10;
/*if (cart.gettotalprice()>=20 && cart.gettotalprice()<50)
temp=buyer.getbonus()+20;
else if(cart.gettotalprice()>=50 && cart.gettotalprice()<100)
temp=buyer.getbonus()+50;
else if (cart.gettotalprice()>100)
temp=buyer.getbonus()+100;
else //ligotero apo 20
temp=buyer.getbonus()+0;
*/
int temp= buyer.getbonus()+ten;
setbonus(temp);
setbuyerCategory(buyer);
if(category == buyerCategory.GOLD)
transfercost=0;
if(category == buyerCategory.SILVER)
transfercost=transfercost/2;
}
public void placeOrder(ShoppingCart so1,ItemOrdered ItemO,int quantity)
{
quantity=ItemO.getquantity();
so1.addItemOrdered(ItemO);
}
public String getname()
{
return name;
}
public String getemail()
{
return email;
}
public void setbonus(int bonus)
{
this.bonus=bonus;
}
public int getbonus()
{
return bonus;
}
public buyerCategory getCategory()
{
return category;
}
public int getdistance()
{
return distance;
}
public double gettransfercost()
{
return transfercost;
}
void print()
{
System.out.println("H stoixeia tou pelati einai:\nCategory:" +getCategory()+"\t\tName:" +getname()+ "\t\tEmail:"+getemail()+"\t\tBonus:" +getbonus()+"\t\tTransfercost:"+gettransfercost());
}
}
I am writing a client database. I want to know the customer's name and hometown based on the customer number. When I enter number 2, I want to see Arya Stark, Edinburgh and when I enter number 1, I want to see Jon Snow, London. Why doesn't my program work? How to fix this?
package app;
import java.util.Scanner;
class Person {
String name;
String homeCity;
int customerNumber;
}
public class Customers {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String name;
System.out.print ("Give a customer card number: ");
name = input.next();
Person person1 = new Person();
person1.name = "Jon Snow";
person1.homeCity = "London";
person1.customerNumber = 1;
Person person2 = new Person();
person2.name = "Arya Stark";
person2.homeCity = "Edinburgh";
person2.customerNumber = 2;
System.out.println();
}
}
This should work:
class Person {
private String name;
private String homeCity;
private int customerNumber;
public Person(String name, String homeCity, int customerNumber) {
this.name = name;
this.homeCity = homeCity;
this.customerNumber = customerNumber;
}
public boolean isMatch(int num) {
return num == customerNumber;
}
#Override
public String toString() {
return name + " from " + homeCity;
}
}
import java.util.Scanner;
class Main {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Person person1 = new Person("Jon Snow", "London", 1);
Person person2 = new Person("Arya Stark", "Edinburgh", 2);
while(true) {
System.out.print("Give a customer card number: ");
String num = input.next();
if (person1.isMatch(Integer.parseInt(num))) {
System.out.println(person1);
} else if (person2.isMatch(Integer.parseInt(num))) {
System.out.println(person2);
} else {
System.out.println("Not found");
}
}
}
}
I tried to create ArrayList with Ingredients object and then iterate through this list but this is only showing the last added ingredient. For example when I'm adding two ingredients (mushrooms, tomatoes) in arrayList are only tomatoes with index 0 and 1.
import java.util.ArrayList;
import java.util.List;
public class Ingredients {
private String ingredientName;
private int ingriedientQuantity;
List<Ingredients> ingredients;
public Ingredients() {
this.ingredients = new ArrayList<>();
}
public void addIngredient(Ingredients ingredient) {
ingredients.add(ingredient);
}
public String getIngredientName() {
return ingredientName;
}
public void setIngredientName(String ingredientName) {
this.ingredientName = ingredientName;
}
public int getIngriedientQuantity() {
return ingriedientQuantity;
}
public void setIngriedientQuantity(int ingriedientQuantity) {
this.ingriedientQuantity = ingriedientQuantity;
}
public void showIngredients() {
for (Ingredients ingredientI : ingredients) {
System.out.println(ingredientI.getIngredientName() + " " + ingredientI.getIngriedientQuantity());
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Ingredients ing = null;
boolean exitFlag = false;
String name;
int quantity;
int option = 0;
String o;
while (!exitFlag) {
System.out.println("1 - Add");
System.out.println("2 - Show");
System.out.println("3 - Exit");
option = sc.nextInt();
sc.nextLine();
switch (option) {
case 1:
ing = new Ingredients();
do {
System.out.println("Product name: ");
name = sc.nextLine();
ing.setIngredientName(name);
System.out.println("Quantity");
quantity = sc.nextInt();
sc.nextLine();
ing.setIngriedientQuantity(quantity);
System.out.println("Add more? Y/N");
o = sc.nextLine();
ing.addIngredient(ing);
} while (!o.equalsIgnoreCase("N"));
break;
case 2:
ing.showIngredients();
break;
case 3:
exitFlag = true;
break;
default:
System.out.println("Error");
break;
}
}
sc.close();
}
}
Is better way to do it?
Your class design is wrong. Create a separate class Ingredient with properties IngredientName and IngredientQuantity. The Ingredients class maintains an ArrayList for Ingredient objects.
A sample Ingredient class may look like:
import java.util.ArrayList;
import java.util.List;
public class Ingredient {
private String ingredientName;
private int ingriedientQuantity;
public Ingredient() {
}
public String getIngredientName() {
return ingredientName;
}
public void setIngredientName(String ingredientName) {
this.ingredientName = ingredientName;
}
public int getIngriedientQuantity() {
return ingriedientQuantity;
}
public void setIngriedientQuantity(int ingriedientQuantity) {
this.ingriedientQuantity = ingriedientQuantity;
}
}
Your Ingredients can be implemented as:
import java.util.ArrayList;
import java.util.List;
public class Ingredients {
List<Ingredient> ingredients;
public Ingredients() {
this.ingredients = new ArrayList<Ingredient>();
}
public void addIngredient(Ingredient ingredient) {
ingredients.add(ingredient);
}
public void showIngredients() {
for (Ingredient ingredientI : ingredients) {
System.out.println(ingredientI.getIngredientName() + " " + ingredientI.getIngriedientQuantity());
}
}
}
I would personnaly create a "Recipe" Class as follow :
public class Recipe {
private List<Ingredients> ingredients;
+ getters and setters
}
Then change a little bit your ingredient class :
public class Ingredient {
private String ingredientName;
private int ingredientQuantity;
...
}
Then in your main method create a Recipe at the beginning, and then in your while loop create a new instance of Ingredient each time and add it to your recipe.
I'm pretty new to programming so I need help. I wanna add the SubjectGrades to the studentList ArrayList. But I think I'm doing the wrong way. What should I do for me to add the SubjectGrades to the ArrayList? Thanks
Here's my partial Main class.
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
private static Scanner in;
public static void main(String[] args) {
ArrayList<Student> studentList = new ArrayList<Student>();
//ArrayList<SubjectGrades> Grades = new ArrayList<SubjectGrades>();
in = new Scanner(System.in);
String search, inSwitch1, inSwitch2;
int inp;
do {
SubjectGrades sGrade = new SubjectGrades();
Student student = new Student();
System.out.println("--------------------------------------");
System.out.println("What do you want to do?");
System.out.println("[1]Add Student");
System.out.println("[2]Find Student");
System.out.println("[3]Exit Program");
System.out.println("--------------------------------------");
inSwitch1 = in.next();
switch (inSwitch1) {
case "1":
System.out.println("Input student's Last Name:");
student.setLastName(in.next());
System.out.println("Input student's First Name:");
student.setFirstName(in.next());
System.out.println("Input student's course:");
student.setCourse(in.next());
System.out.println("Input student's birthday(mm/dd/yyyy)");
student.setBirthday(in.next());
System.out.println("Input Math grade:");
student.subjectGrade.setMathGrade(in.nextDouble());
System.out.println("Input English grade:");
student.subjectGrade.setEnglishGrade(in.nextDouble());
System.out.println("Input Filipino grade:");
student.subjectGrade.setFilipinoGrade(in.nextDouble());
System.out.println("Input Java grade:");
student.subjectGrade.setJavaGrade(in.nextDouble());
System.out.println("Input SoftEng grade:");
student.subjectGrade.setSoftEngGrade(in.nextDouble());
studentList.add(student);
studentList.add(student.setSubjectGrade(sGrade)); //Here it is that I want to add
break;
//end case 1
Here is my Student Class.
package santiago;
public class Student {
private String lastName;
private String firstName;
private String course;
private String birthday;
SubjectGrades subjectGrade = new SubjectGrades();
public SubjectGrades getSubjectGrade() {
return subjectGrade;
}
public void setSubjectGrade(SubjectGrades subjectGrade) {
this.subjectGrade = subjectGrade;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
And my SubjectGrades class
package santiago;
public class SubjectGrades{
Double mathGrade, englishGrade, filipinoGrade, javaGrade, softEngGrade, weightedAverage;
public Double getMathGrade() {
return mathGrade;
}
public void setMathGrade(Double mathGrade) {
this.mathGrade = mathGrade;
}
public Double getEnglishGrade() {
return englishGrade;
}
public void setEnglishGrade(Double englishGrade) {
this.englishGrade = englishGrade;
}
public Double getFilipinoGrade() {
return filipinoGrade;
}
public void setFilipinoGrade(Double filipinoGrade) {
this.filipinoGrade = filipinoGrade;
}
public Double getJavaGrade() {
return javaGrade;
}
public void setJavaGrade(Double javaGrade) {
this.javaGrade = javaGrade;
}
public Double getSoftEngGrade() {
return softEngGrade;
}
public void setSoftEngGrade(Double softEngGrade) {
this.softEngGrade = softEngGrade;
}
public Double getWeightedAverage(){
weightedAverage = ((mathGrade + englishGrade + filipinoGrade + javaGrade + softEngGrade)*3) / 15;
return weightedAverage;
}
public String getScholarStatus(){
String status = "";
if(weightedAverage <= 1.5) {
status = "full-scholar";
} else if (weightedAverage <= 1.75){
status = "half-scholar" ;
} else {
status = "not a scholar";
}
return status;
}
}
Your mistake:
studentList.add(student);
studentList.add(student.setSubjectGrade(sGrade));
You are adding the student, then trying to add a void. The return value of setSubjectGrade is void, so nothing will be added:
Just do:
student.setSubjectGrade(sGrade);
studentList.add(student);
Where sGrade is an Object of type SubjectGrades, which was populated in the same way
student.subjectGrade.setSoftEngGrade(in.nextDouble()); was populated.
Use
ArrayList <SubjectGrades> list;
in student class instead SubjectGrades subjectGrade = new SubjectGrades();.
and generate getters and setters
Just remove this line:
studentList.add(student.setSubjectGrade(sGrade)); //Here it is that I want to add
The way you have done it, the student object already has the subjectGrade attribute with its values set.
You can access it with studentList.get(0).getSubjectGrade()
I have to have the code below, pass the data typed in by the user passed to the following method:
public void addPerson(Person p)
{
personList.add(p);
}
and it must be from there added to the array "personList" that is in databse.java (which is posted at the very bottom.
This is what i have so far:
package hartman;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Printer.printWelcome();
Scanner keyboard = new Scanner(System.in);
boolean keepRunning = true;
while (keepRunning) {
Printer.printMenu();
Printer.printPrompt("Please enter your operation: ");
String userSelection = keyboard.nextLine();
switch (userSelection) {
case "1":
handleAddPerson(keyboard);
break;
case "2":
Database.printDatabase();
break;
case "3":
handleSearchPerson(keyboard);
break;
case "4":
keepRunning = false;
break;
default:
break;
}
}
Printer.printGoodBye();
}
private static void handleAddPerson(Scanner keyboard) {
Printer.printAddPersonTitle();
Printer.printPrompt("First Name: ");
String addFirstName = keyboard.nextLine();
Printer.printPrompt("Last Name: ");
String addLastName = keyboard.nextLine();
Printer.printPrompt("Social Security Number: ");
String addSocial = keyboard.nextLine();
Printer.printPrompt("Year Born: ");
int addYearBorn = Integer.parseInt(keyboard.nextLine());
Person person = new Person();
person.setFirstName(addFirstName);
person.setLastName(addLastName);
person.setSocialSecurityNumber(addSocial);
person.setYearBorn(addYearBorn);
}
static void handleSearchPerson(Scanner keyboard) {
Printer.printSearchPersonTitle();
Printer.printPrompt(" Enter search value: ");
String keyword = keyboard.nextLine();
}
}
here is Person.java for those who wondered.
package hartman;
public class Person {
private String firstName;
private String lastName;
private String socialSecurityNumber;
private int yearBorn;
public Person() {
}
public Person(String firstName, String lastName,
String socialSecurityNumber, int yearBorn) {
}
public int getAge() {
return yearBorn = 2014 - yearBorn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSocialSecurityNumber() {
return socialSecurityNumber;
}
public void setSocialSecurityNumber(String socialSecurityNumber) {
this.socialSecurityNumber = socialSecurityNumber;
}
public int getYearBorn() {
return yearBorn;
}
public void setYearBorn(int yearBorn) {
this.yearBorn = yearBorn;
}
}
and here is database.java
package hartman;
import java.util.ArrayList;
import java.util.Scanner;
public class Database {
Scanner keyboard = new Scanner(System.in);
private ArrayList<Person> personList;
public Database() {
}
public void addPerson(Person p) {
personList.add(p);
}
public static void printDatabase() {
}
public ArrayList<Person> findPerson(String searchFor) {
Main.handleSearchPerson(keyboard);
ArrayList<Person> matches = new ArrayList<>();
for (Person p : personList) {
boolean isAMatch = false;
if (p.getFirstName().equalsIgnoreCase(searchFor)) {
isAMatch = true;
}
if (p.getLastName().equalsIgnoreCase(searchFor)) {
isAMatch = true;
}
if (p.getSocialSecurityNumber().contains(searchFor)) {
isAMatch = true;
}
if (String.format("%d", p.getAge()).equals(searchFor))
if (isAMatch) {
matches.add(p);
}
}
return matches;
}
}