Click here to Skip to main content
15,913,669 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
You will design and implement your own data class. The class will store data that has been read as
user input from the keyboard (see Getting Input below), and provide necessary operations. As the
data stored relates to monetary change, the class should be named MoneyChange. The class
requires at least 2 instance variables for the name of a person and the coin change amount to be
given to that person. You may also wish to use 6 instance variables to represent amounts for each of
the 6 coin denominations (see Client Class below). There should be no need for more than these
instance variables. However, if you wish to use more instance variables, you must provide legitimate
justification for their usage in the internal and external documentation.
Your class will need to have at least a default constructor, and a constructor with two parameters:
one parameter being a name and the other a coin amount. Your class should also provide
appropriate get and set methods for client usage. Other methods may be provided as needed.
However, make sure they are necessary for good class design; you must provide legitimate
justification for their usage in the internal and external documentation. In particular, your class
should NOT include Input and Output methods. The only way to get data out of a data class object to
the client program is to use an appropriate get method. The data class methods must not write data
out. Data should be entered into a data class object via a constructor or an appropriate set method.

What I have tried:

Java
public class MoneyChange {
	
	//2 dollars, 1 dollar, 50 cents, 10 cents, 20 cents and 5 cents
	
	private String name;
	private int coinChangeAmount;
	
	public Change(String name, int coinChangeAmount) {
		this.name = name;
		this.coinChangeAmount = coinChangeAmount;
	}

	public String getName() {
		return name;
	}

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

	public int getCoinChangeAmount() {
		return coinChangeAmount;
	}

	public void setCoinChangeAmount(int coinChangeAmount) {
		this.coinChangeAmount = coinChangeAmount;
		
	}
	
	public int[] getChange(){
		
		int[] changeToReturn = new int[3];
		//you need to figure out the no of each coin based on the coinChangeAmount
		//results are hardcoded currently in the array
		changeToReturn[0] = 1;//no of 50 cents
		changeToReturn[1] = 1;//no of 10 cents
		changeToReturn[2] = 1;//no of 5 cents
		
		return changeToReturn;
		
	}
	
	public void addCoinChangeAmount(int coinChangeAmountToAdd) {
		this.coinChangeAmount += coinChangeAmountToAdd;
	}
	
	public void display() {
		System.out.println("Name:"+name);
		System.out.println("CoinChangeAmount:"+coinChangeAmount);
	}
	

}


//another class is as follows
Java
public class Client {

	// do not forget to provide a method in the client class that hardcodes data
	// into
	// at least 10 Change objects and stores these objects into the array provided
	// by your program

	// You submission should have the 9 Change objects
	// created and loaded into the Array for immediate testing
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// store up to 10 change
		Change[] changeArray = new Change[10];

		// create 1 change object and store into array
		Change change1 = new Change("John", 85);
		changeArray[0] = change1;

		int[] change = changeArray[0].getChange();
		for (int i = 0; i < change.length; i++) {
			System.out.println(change[i]);
		}

		Change change2 = new Change("Mary", 15);
		changeArray[1] = change2;
		Change change3 = new Change("Peter", 100);
		changeArray[2] = change3;
		Change change4 = new Change("Bob", 20);
		changeArray[3] = change4;
		
		//Mary is found so should add in to the current Mary object
		addNewChange(changeArray,"Mary",10);
		
		//Ada is not found, so should create a new Ada object
		addNewChange(changeArray,"Ada",10);
		printAllChangeObject(changeArray);
	}
	
	private static void printAllChangeObject(Change[] changeArray) {
		System.out.println("Print All Change Objects");
		for(int i=0;i<changeArray.length;i++) {
			Change currentChange = changeArray[i];
			if(currentChange!=null) {
				currentChange.display();
			}
		}
	}

	public static void addNewChange(Change[] changeArray, String nameToAdd,int amountToAdd) {
						
		boolean found = false;
				
		for(int i=0;i<changeArray.length;i++) {
			Change currentChange = changeArray[i];
			if(currentChange!=null) {
				String currentName = currentChange.getName();
				if(currentName.equals(nameToAdd)) {
					currentChange.addCoinChangeAmount(amountToAdd);
					found = true;
				}
			}
		}
		
		if(found) {
			System.out.println("Name found amount added");
		}else {
			Change newChangeObject = new Change(nameToAdd, amountToAdd);
			addNewChangeObject(changeArray,newChangeObject);
		}
	}
	
	
	private static void addNewChangeObject(Change[] changeArray, Change change) {
		
		boolean found = false;
		
		//look for a null slot and add the new change object in
		for(int i=0;i<changeArray.length;i++) {
			Change currentChange = changeArray[i];
			if(currentChange==null) {
				changeArray[i] = change;
				found = true;
				break;
			}
		}
		
		if(found) {
			System.out.println("New change object added");
		}else {
			System.out.println("No free slots found");
		}
	}
		


}
Posted
Updated 21-Jun-21 21:33pm
v2
Comments
Richard MacCutchan 22-Jun-21 3:32am    
What is the problem? And your constructor in the MoneyChange class needs to be named MoneyChange not Change.
bhushan7 22-Jun-21 3:55am    
Thank you It works.
Mr.Corona 1-Jul-21 1:31am    
yeah

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