-1
for (int i = 0; i < 2; i++) {
    System.out.println("Please enter your name: ");
    name = scan.nextLine();
    peoples.setName(name);
    System.out.println("Please enter your IC: ");
    icNo = scan.nextLine();
    peoples.setIcNo(icNo);
    System.out.println("Please enter your marital status: ");
    status = scan.nextLine();
    taxes.setStatus(status);
    System.out.println("Please enter your taxable income: ");
    taxableIncome = scan.nextDouble();
    taxes.setTaxableIncome(taxableIncome); 
    peoples.addPeople(name, icNo, taxableIncome, taxAmount);
}
System.out.printf("NAME " + "IC NO " + "TAXABLE INCOME " + "TAX AMOUNT");
System.out.println("");
System.out.println(peoples.toString());

This is a section of the code for the problem. I'm supposed to ask a person's name, ID, marital status and taxable income. I have three classes, one for the person's details, one to calculate the tax imposed and the main class here.

The information obtained from the people was supposed to be placed into an array but they're all in different data types. Well, name and ID are string types while both taxable income and tax amount are in double data types.

I tried to make an array in the people class but it didn't work out. I tried casting the array into string but it didn't work either. I'm supposed to obtain data from two or more people and print them out below a header. I just can't think of how it's supposed to store the data from user inputs and print them outside of the for loop. The user input should be stored as an array but I'm open to any other solutions.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
jaytee
  • 5
  • 1
  • May be this will help you , you can create a array of type peoples and store different objects of that class for different people .https://stackoverflow.com/questions/30250625/making-an-array-of-size-10-employee-objects this is a similar one – Anish Dec 04 '22 at 09:16

1 Answers1

1

You would need to have a Tax class that would store the tax info and it should contain a method that calculate tax like this

public class Tax {
    private double taxableIncome_;
    private double taxAmount_;
    private String status_;

    Tax(double taxableIncome, double taxAmount, String status) {
        // initialize your members
    }

    public double calculateTax() {
        //calculate tax
    }
    // implement setters and getters if needed.
}

Then you should create Person class that saves a person info and an instance of Tax like this:

public class Person {
    private String name_;
    private String id_;
    private Tax tax_;

    Person(String name, String id, Tax tax) {
        // initialize members
    }

    @Override
    public String toString() {
// return a string that has a person's information and tax's information
    }
}

Then in your main program after creating an array of people you can use Arrays.toString(pepoles) which will call toString for each object in your array

Example:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public  static void main(String[] args)
    {
        Person[] people = new Person[2];
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<2;i++)
        {
            String name = scan.nextLine();
            System.out.println("Please enter your IC: ");
            String icNo = scan.nextLine();
            System.out.println("Please enter your marital status: ");
            String status = scan.nextLine();
            System.out.println("Please enter your taxable income: ");
            double taxableIncome = scan.nextDouble();
            System.out.println("Please enter your taxable Amount: ");
            double taxAmount = scan.nextDouble();
            people[i] = new Person(name, icNo, new Tax(taxableIncome, taxAmount,status));
        }
        System.out.println("NAME " + "IC NO " + "TAXABLE INCOME " + "TAX AMOUNT");
        System.out.print(Arrays.toString(people));
    }
}