Click here to Skip to main content
16,015,481 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
According to the pseudocode i have done, one of the requirement is to read the user input and to store the data entered, will need data structure to store the object and utilize the array of object not using ArrayList.

I am pretty lost with data structure and not to sure on how to implement data structure on my program. I am new to java and will like to understand more on data structure & algorithm. Hope to understand more with this program here!

What I have tried:

Method Class:
public class Change {

    // declare variable to store data
    private String name;
    private int amount;


    // default constructor
    public Change() {
        this.name = "";
        this.amount = 0;
    }

    // parameterized constructor
    public Change(String name) {
        this.name = name;                  // to accepts specific numbers of parameter
        this.amount = amount;        // and initialise data members of a class with distinct values

    }

    // getters method
    public String getName() {

        return name;
    }

    public int getCoin() {
        return amount;
    }

    // setters method
    public void setName(String name) {
        this.name = name;
    }

    public void setCoin(int amount){
        this.amount = amount;
    }

    // calculate and to return denomination count
    public int[] calculate(){
        int Dollar, FiftyCent, TwentyFiveCent, TenCent, FiveCent;
        // formula to calculate declared instance
        Dollar = amount / 100;
        FiftyCent = (amount % 100) / 50;
        TwentyFiveCent = ((amount % 100) % 50) / 25;
        TenCent = ((amount % 100) % 25) / 10;
        FiveCent = ((((amount % 100) % 50) %25) % 10) / 5;

        int denomination[] = {Dollar, FiftyCent, TwentyFiveCent, TenCent, FiveCent};
        return denomination;

    }
}


Main Class:
import java.util.Scanner;

public class ChangeMain {

    public static void main(String[] args) {
        int amount, min, max, index;
        String name;
        char people;
        boolean newUser;
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Recommendation: Please enter at least 10 records to test the program. ");
        while(newUser = true){
            // prompt user to enter 10 records of name
            // to save the instance in the array
            System.out.println("Please enter the user name: ");
            name = keyboard.nextLine();

            // to print the amount and allow user to know value is valid when divisible by 5
            // prompt user to enter coin value
            System.out.println("Please enter the user coin value: ");
            amount = keyboard.nextInt();
            keyboard.nextLine();
            if(amount < 5 || amount > 100 || (amount % 5) != 0)   // modulus operator for divisor of 5
            {
                System.out.println("Invalid Value! Please enter the value (multiple of 5) again!");
            }
            else
            {
                System.out.println("***** Proceed to continue with the program! *****");
            }

            // to break the loop when user enter "N"
            System.out.println("Do you have more user to enter[Y/N]: ");
            String userInput = keyboard.nextLine();
            if(userInput.equalsIgnoreCase("N"))
            {
                System.out.println("Program exit.....");
                break;
            }
        }

        





    }


}
Posted
Updated 25-Jun-22 0:32am
v2
Comments
Richard MacCutchan 24-Jun-22 7:48am    
As far as I can guess from your question, you need to create a simple array of change objects for each set of data entered.
Your parameterized constructor in the Change class is incomplete, it should accept a name and a number.
Your amount check in the main class only accepts values less than or equal to 100 - is that correct?
There may be other issues that I have not noticed.
noobieq0000 24-Jun-22 7:57am    
the amount check can be more than or equal to 100
Richard MacCutchan 24-Jun-22 8:05am    
Not according to the above code.
noobieq0000 24-Jun-22 8:16am    
so do i have to change my if(amount < 5 || amount > 100 || (amount % 5) != 0) to if(amount < 5 || amount >= 100 || (amount % 5) != 0)?
Richard MacCutchan 24-Jun-22 8:29am    
No just remove the test for 100:
if(amount < 5 || (amount % 5) != 0) // if less than 5 or not a multiple

1 solution

//create empty array of Change objects type
  Change[] changes = ...
  int idx = ...
  
in loop:
   //create Change object
     Change change = ...
     (set it)
   //store this object
      changes[idx++] = change;

Change constructor doesn't input amount, but you try set it inside constructor
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900