I have been tasked with writing a code to create 4 classes(Main, Person,Account,Vehicle)and create objects of the Person class as per user requirements, doing so using an array. I had to store data within the array via user input, and retrieve them as per user choice. Which I believe I have successfully done so. One of the two inputs I had to ask from the user were, "Do you have an Account" and "Do you have a vehicle". If the User answers "Yes", I had to pass the object or something as a reference to the Account and Vehicle class to seclude them separately as well, and when the User asks for the People with an Account and Vehicle, It were to print the details of those specific users only. . Here is my code below Main.java:
package com.company;
import java.util.Scanner;
public class Main {
public static int ids;
static int choice = 0;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
menus();
}
public static void conditions(int choice){
switch (choice) {
case 1:
int i;
System.out.println("Enter id of person");
i = sc.nextInt();
Person user2[] = new Person[10];
user2[i] = new Person();
user2[i].setArray(i,user2);
//Getters
int k;
System.out.println("Enter id of existing person to retrieve info or press 0 to go to menu");
int l;
l = sc.nextInt();
if (l == 0) {
menus();
}
else {
System.out.println(user2[l].getName());
System.out.println(user2[l].getGender());
System.out.println(user2[l].getAge());
System.out.println(user2[l].isAccount());
System.out.println(user2[l].isVehicle());
}
case 2:
//Call getters
Account a1=new Account();
a1.getArrays();
case 3:
Vehicle v1=new Vehicle();
v1.printArray();
}
}
public static void menus() {
System.out.println("Choose an option");
System.out.println("1.Enter new person details");
System.out.println("2.Does not work(should display people who want an acc only");
System.out.println("3.Does not work(Should display people who have a vehicle only)");
choice = sc.nextInt();
conditions(choice);
}
}
Person.java:
package com.company;
import java.util.Scanner;
public class Person {
private String name;
private int age;
private String gender;
private static String Account;
private static String Vehicle;
Person array[];
public String isAccount() {
return Account;
}
public void setAccount(String account) {
Account = account;
}
public String isVehicle() {
return Vehicle;
}
public void setVehicle(String vehicle) {
Vehicle = vehicle;
}
public Person[] getArray() {
return array;
}
public void setArray(int i,Person[] array) {
this.array = array;
Scanner sc=new Scanner(System.in);
System.out.println("Enter name");
name=sc.next();
array[i].setName(name);
System.out.println("Enter age");
age=sc.nextInt();
array[i].setAge(age);
System.out.println("Enter gender");
gender=sc.next();
array[i].setGender(gender);
System.out.println("Enter Yes if you want an account,No if otherwise");
Account=sc.next();
if(Account=="Yes"){
Account user3[] = new Account[10];
user3[i].setArray(i,this.array);
user3[i] = new Account(i,name,gender,Account);
}
else{
array[i].setAccount("No");
}
System.out.println("Enter Yes if you have a vehicle, No if otherwise");
Vehicle=sc.next();
if(Vehicle=="Yes"){
Vehicle user4[] = new Vehicle[10];
user4[i] = new Vehicle();
user4[i].setArray(i,this.array);
}
else{
array[i].setVehicle("No");
}
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
Accounts.java:
package com.company;
public class Account extends Person{
Account[] arrays;
private int ages;
private String names;
private String genders;
private String vehicles;
private String accounts;
Account(int id,String names,String genders,String accounts){
arrays[id].setNames(names);
arrays[id].setAges(ages);
arrays[id].setGenders(genders);
arrays[id].setAccounts(accounts);
}
Account(){
}
public com.company.Account[] getArrays() {
return arrays;
}
public void setArrays(com.company.Account[] arrays) {
this.arrays = arrays;
}
public int getAges() {
return ages;
}
public void setAges(int ages) {
this.ages = ages;
}
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
public String getGenders() {
return genders;
}
public void setGenders(String genders) {
this.genders = genders;
}
public String getVehicles() {
return vehicles;
}
public void setVehicles(String vehicle) {
this.vehicles = vehicles;
}
public String getAccounts() {
return accounts;
}
public void setAccounts(String accounts) {
this.accounts = accounts;
}
public void printArray(){
for(int k=0;k<array.length;k++){
System.out.println(array[k]);
}
}
}
My Vehicle class is the same as Accounts. -To seclude and print -I have tried to pass the array from the Person class to the Accounts class and Vehicle, If the User does wish to have an Account and has a Vehicle. -However, upon printing via for loop or a basic getter, I am met with an Error "this.array is null" which probably means my Array isnt being passed or stored within the Account class. -Maybe there is a simpler way of secluding and printing them, as per the task I have to implement the use of Abstraction, Arrays, Constructors and Functions. I am new to Java and the Stackoverflow platform, so please forgive me If the formatting of the question isnt precise. Hope to recieve some detailed help on getting around this.