Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need a help to fix my code and pass the test. Struggling a lot. The majority of my code is correct, so you don't

I need a help to fix my code and pass the test. Struggling a lot.

The majority of my code is correct, so you don't need to touch it. However, I did not properly update the number of toys that child added. (you'll understand what I mean by this after you read the instruction, I attached it below the page)

So the problem is at addToy method.

There is some logical mistake, that I just cannot figure out.

I also attached failed test case, so please pass the test before you submit the solution.

I cannot use ArrayList.

There are three classes, and I only attached the test cases that I failed because it's really long. I also added the comment on the test case part that I failed. (Bold font)

if you want the whole test case let me know, I'll send you the codeshare link.

Ask me if you have any questions. thank you.

my code: (the method I mentioned is in bold font)

public class Child {

// attributes

private String childname;

private int childage;

private Toy[] listofToy;

private int numberOfToys;

// constructor

public Child(String childname, int childage, Toy[] listofToy) {

this.childname = childname;

this.childage = childage;

if (listofToy != null) {

this.numberOfToys = listofToy.length;

this.listofToy = new Toy[listofToy.length];

for (int i = 0; i < listofToy.length; i++) {

this.listofToy[i] = new Toy(listofToy[i]);

}

} else {

this.numberOfToys = 0;

}

}

public Child(Child c1) {

this.childname = c1.childname;

this.childage = c1.childage;

this.numberOfToys = c1.numberOfToys;

this.listofToy = new Toy[c1.numberOfToys];

for (int i = 0; i < c1.numberOfToys; i++) {

this.listofToy[i] = new Toy(c1.listofToy[i]);

}

}

// getter

public int getChildAge() {

return this.childage;

}

public String getChildName() {

return this.childname;

}

public Toy[] getChildToy() {

return this.listofToy;

}

public String addToy(Toy newToy) {

if (listofToy == null) {

listofToy = new Toy[100]; // Adjust the array size as per your requirements

numberOfToys = 0;

}

// Check if a toy with the same name already exists

for (int i = 0; i < numberOfToys; i++) {

if (listofToy[i].getToyName().equals(newToy.getToyName())) {

// Update the quantity and price of the existing toy

int newQuantity = listofToy[i].getToyQuantity() + newToy.getToyQuantity();

double newPrice = newToy.getToyPrice();

listofToy[i].setToyQuantity(newQuantity);

listofToy[i].setToyPrice(newPrice);

return "Toy with the same name already exists. Toy is updated";

}

}

// If the toy doesn't exist, check if there's room to add a new toy

if (numberOfToys < listofToy.length) {

listofToy[numberOfToys] = newToy;

this.numberOfToys++;

return "Toy is added";

} else {

// If the maximum number of toys is reached, return an error message

return "Child already has the maximum number of toys";

}

}

public int getNumberofToys() {

return this.numberOfToys;

}

// setter

public void setChildName(String childname) {

this.childname = childname;

}

public void setChildAge(int childage) {

this.childage = childage;

}

public void disposeToys() {

listofToy = null;

numberOfToys = 0;

}

public void donate(Child c1) {

if (c1.listofToy == null) {

c1.listofToy = new Toy[10];

c1.numberOfToys = 0;

}

for (int i = 0; i < numberOfToys; i++) {

if (c1.numberOfToys < 10) {

c1.listofToy[c1.numberOfToys] = new Toy(listofToy[i]);

c1.numberOfToys++;

}

}

listofToy = null;

numberOfToys = 0;

}

public String toString() {

return String.format("Child [%s] of age <%d> has (%d) toys", childname, childage, getNumberofToys());

}

}

public class Order {//new class

// attributes

private int max = 5;

private Child[] c;

private int numofChilds;

// constructor

public Order() {

c = new Child[max];

this.numofChilds=0;

}

//getter

public int getNumofChilds() {

return this.numofChilds;

}

public Child[] getChilds() {

Child[] newOrder = new Child[numofChilds];

for (int i = 0; i < numofChilds; i++) {

newOrder[i] = c[i];

}

return newOrder;

}

public void addChildtoOrder(Child child) {

if (numofChilds < max) {

c[numofChilds] = child;

numofChilds++;

}

}

public void removeChildfromOrder(Child child) {

for (int i = 0; i < numofChilds; i++) {

if (c[i].equals(child)) {

for (int j = i; j < numofChilds - 1; j++) {

c[j] = c[j + 1];

}

// c[numofChilds - 1] = null;

numofChilds--;

}

}

}

public String toString() {

return String.format("The order contains %d Childs", numofChilds);

}

}

public class Toy {//new class

// attributes

private int toyid;

private String toyname;

private int quantity;

private double price;

// constructor

public Toy(int toyid, String toyname, int quantity, double price) {

this.toyid = toyid;

this.toyname = toyname;

this.quantity = quantity;

this.price = price;

}

public Toy(Toy toy) {

this.toyid = toy.toyid;

this.toyname = toy.toyname;

this.quantity = toy.quantity;

this.price = toy.price;

}

//getter

public int getToyID() {

return this.toyid;

}

public String getToyName() {

return this.toyname;

}

public int getToyQuantity() {

return this.quantity;

}

public double getToyPrice() {

return this.price;

}

public String getToyInformation() {

return String.format("Toy(%d,%s), quantity(%d) with $(%6.2f)/toy", toyid, toyname, quantity, price);

}

//setter

public void setToyID(int toyid) {

this.toyid = toyid;

}

public void setToyName(String toyname) {

this.toyname = toyname;

}

public void setToyQuantity(int quantity) {

this.quantity = quantity;

}

public void setToyPrice(double price) {

this.price = price;

}

}

test case;

@Test

public void test_2_4() {

Toy t1 = new Toy(1000121, "Red Bike", 3, 98.90);

Toy t2 = new Toy(1000123, "Colouring Book", 4, 19.89);

Toy t3 = new Toy(1000128, "Skateboard", 5, 149.99);

Toy t4 = new Toy(1000127, "SpongeBob DVD", 3, 14.99);

Toy t5 = new Toy(1000130, "Bike Helmet", 1, 18.99);

Toy t6 = new Toy(1000125, "Toy car", 10, 3.99);

Toy t7 = new Toy(1000129, "Ball", 2, 5.59);

Toy t8 = new Toy(1000189, "Teddy Bear", 3, 10.79);

Toy t9 = new Toy(1000188, "Teddy Bear", 4, 12.85);

Toy t10 = new Toy(1000128, "Skateboard", 2, 140.09);

Toy t11 = new Toy(1000127, "SpongeBob DVD", 7, 21.19);

Toy[] listofToy1 = { t1, t2 };

Toy[] listofToy2 = { t3, t4, t5, t6 };

Toy[] listofToy3 = { t7, t8 };

/*

* Create child with empty list of toys

*/

Child c = new Child("Mark", 4, listofToy3);

int expNumofToys = 2;

int expChildAge = 4;

String expChildName = "Mark";

int numofToys = c.getNumberofToys();

int childAge = c.getChildAge();

String childName = c.getChildName();

Toy[] childToysList = c.getChildToy();

String errorMsg1 = String.format(" Test getNumberofToys failed. Returned (%d) " + "but correct is (%d)",

numofToys, expNumofToys);

assertEquals(errorMsg1, expNumofToys, numofToys);

String errorMsg2 = String.format(" Test getChildAge failed. Returned (%d) " + "but correct is (%d)",

childAge, expChildAge);

assertEquals(errorMsg2, expChildAge, childAge);

String errorMsg3 = String.format(" Test getChildName failed. Returned (%s) " + "but correct is (%s)",

childName, expChildName);

assertEquals(errorMsg3, expChildName, childName);

String errorMsg4 = String.format(" Test getChildToy failed. Returned array of length (%d) " + "but correct is (%d)",

childToysList.length, listofToy3.length);

assertEquals(errorMsg4, listofToy3.length, childToysList.length);

// String representation for the child object

String childstr = c.toString();

String expChildStr = "Child [Mark] of age <4> has (2) toys";

String errorMsg5 = String.format(" Test toString failed. Returned (%s) " + "but correct is (%s)", childstr,

expChildStr);

assertEquals(errorMsg5, expChildStr, childstr);

// verify the toys list for c1

// It is important to note that each child owns their toys

// So the child is not sharing his toys with another child

assertTrue(c.getChildToy().length == 2 && c.getChildToy()[0] != t7 && c.getChildToy()[1] != t8);

assertTrue(c.getChildToy().length == 2 && c.getChildToy()[0].getToyName().equals("Ball")

&& c.getChildToy()[1].getToyName().equals("Teddy Bear"));

assertTrue(c.getChildToy().length == 2 && c.getChildToy()[0].getToyQuantity() == 2

&& c.getChildToy()[1].getToyQuantity() == 3);

final double THRESHOLD = .001;

assertTrue(c.getChildToy().length == 2 && Math.abs (c.getChildToy()[0].getToyPrice() - 5.59)

&& Math.abs (c.getChildToy()[1].getToyPrice() - 10.79)

// Test adding same toy name with different quantity and price

String addStr = c.addToy(t9);

expNumofToys = 2;

numofToys = c.getNumberofToys();

errorMsg1 = String.format(" Test getNumberofToys failed. Returned (%d) " + "but correct is (%d)",

numofToys, expNumofToys);

assertEquals(errorMsg1, expNumofToys, numofToys);

String expAddToy = "Toy with the same name already exists. Toy is updated";

// test if adding toy is successfully completed

String errorMsg6 = String.format(" Test addToy failed. Returned (%s) " + "but correct is (%s)",

addStr, expAddToy);

assertEquals(errorMsg6, expAddToy, addStr);

assertTrue(c.getChildToy().length == 2 && c.getChildToy()[0].getToyQuantity() == 2

&& c.getChildToy()[1].getToyQuantity() == 7);

assertTrue(c.getChildToy().length == 2 && Math.abs (c.getChildToy()[0].getToyPrice() - 5.59)

&& Math.abs (c.getChildToy()[1].getToyPrice() - 12.85)

childstr = c.toString();

expChildStr = "Child [Mark] of age <4> has (2) toys";

errorMsg5 = String.format(" Test toString failed. Returned (%s) " + "but correct is (%s)", childstr,

expChildStr);

assertEquals(errorMsg5, expChildStr, childstr);

Child c2 = new Child("Emma", 3, listofToy1);// the initial NumofToys are 2, but she added 4 toys, so it should be 6, but my method is not properly updated , still 2. That's the problem.

// add new toys

c2.addToy(t3); c2.addToy(t4);

c2.addToy(t5); c2.addToy(t6);

expNumofToys = 6;

numofToys = c2.getNumberofToys();

childToysList = c2.getChildToy();

assertTrue(c2.getChildToy().length == 6 && c2.getChildToy()[2] != t3 && c2.getChildToy()[3] != t4);

assertTrue(c2.getChildToy().length == 6 && c2.getChildToy()[4] != t5 && c2.getChildToy()[5] != t6);

assertTrue(c2.getChildToy().length == 6 && c2.getChildToy()[2].getToyName().equals("Skateboard")

&& c2.getChildToy()[3].getToyName().equals("SpongeBob DVD"));

assertTrue(c2.getChildToy().length == 6 && c2.getChildToy()[4].getToyName().equals("Bike Helmet")

&& c2.getChildToy()[5].getToyName().equals("Toy car"));

errorMsg1 = String.format(" Test getNumberofToys failed. Returned (%d) " + "but correct is (%d)", numofToys,

expNumofToys);

assertEquals(errorMsg1, expNumofToys, numofToys);

errorMsg4 = String.format(" Test getChildToy failed. Returned array of length (%d) " + "but correct is (%d)",

childToysList.length, listofToy1.length);

assertEquals(errorMsg4, 6, childToysList.length);

childstr = c2.toString();

expChildStr = "Child [Emma] of age <3> has (6) toys";

errorMsg5 = String.format(" Test toString failed. Returned (%s) " + "but correct is (%s)", childstr,

expChildStr);

assertEquals(errorMsg5, expChildStr, childstr);

// add same toy

addStr = c2.addToy(t10);

expAddToy = "Toy with the same name already exists. Toy is updated";

// test if adding toy is successfully completed

errorMsg6 = String.format(" Test addToy failed. Returned (%s) " + "but correct is (%s)",

addStr, expAddToy);

assertEquals(errorMsg6, expAddToy, addStr);

assertTrue(c2.getChildToy().length == 6 && c2.getChildToy()[0].getToyQuantity() == 3

&& c2.getChildToy()[2].getToyQuantity() == 7);

assertTrue(c2.getChildToy().length == 6 && Math.abs (c2.getChildToy()[0].getToyPrice() - 98.90)

&& Math.abs (c2.getChildToy()[2].getToyPrice() - 149.99)

addStr = c2.addToy(t11);

expAddToy = "Toy with the same name already exists. Toy is updated";

// test if adding toy is successfully completed

errorMsg6 = String.format(" Test addToy failed. Returned (%s) " + "but correct is (%s)",

addStr, expAddToy);

assertEquals(errorMsg6, expAddToy, addStr);

assertTrue(c2.getChildToy().length == 6 && c2.getChildToy()[2].getToyQuantity() == 7

&& c2.getChildToy()[3].getToyQuantity() == 10);

assertTrue(c2.getChildToy().length == 6 && Math.abs (c2.getChildToy()[2].getToyPrice() - 149.99)

&& Math.abs (c2.getChildToy()[3].getToyPrice() - 21.19)

}

Instruction:

Computational thinking for a software developer/computer programmer is a critical skill that is consistently applied. This lab requires you to develop a solution using Java object-oriented programming that simulates an order system for children's toys. The child will manage a collection/list of toys (each child owns his toys), and an order system will create order containing up to 5 children (i.e., the maximum number of children in any given order is 5). The system manages information about a single toy. The system stores the following information (for each attribute, choose any type that you think is appropriate--you must be able to justify the decisions you make): Toy ID: the toy id of the toy (a positive number) Toy name: the name of the toy Toy quantity: the quantity of this toy that the child owns. The quantity of any given toy is a positive integer value. Toy price: the price of this toy The system manages information about a single child. The system stores the following information (for each attribute, choose any type that you think is appropriate--you must be able to justify the decisions you make): Child name: the name of a child Child age: a positive integer number represents the child's age in years. Child list of toys: the system allows a child to have as many toys as the child want. There is no maximum number of toys that a child can own. Child number of toys: a positive number represents a child's number of toys at any given moment.

The system manages information about a single order. The system stores the following information (for each attribute, choose any type that you think is appropriate--you must be able to justify the decisions you make): List of children in this order: The maximum number of children in any given order is 5. Thus, an order can contain any number of children between zero and 5. The number of children: the order object stores the number of children included in this order at any given moment. Your task is to drive and create class(es) and method(s) from the given JUnit class and then add them to the package using the following facts: You can get and set any toy attributes. The child object can be constructed by receiving the child's name, age and list of toys. Each child owns their toys and does not share them with other children. A child can dispose of his toys. A child can denote his toys only to another child. In this case, the toys will be removed from the donor child, and the toys will be added to the list of toys of another child. There is no need to check for duplicated toys when denoting child toys (e.g., two toys with the same information/names). The system allows you to add toys to the list of toys for any child, ensuring that the toys are not duplicated based on their names and using the existing ID if the toys have different IDs (i.e., ignore the new toy ID). Ensure you update the toy quantity and price by considering the maximum price for the first occurrence of duplicate toys. You can get and set the child's name. You can get and set the child's age. You can get and set the child's list of toys. You can add a child to the order as long as you do not reach the maximum number of children per order, which is 5. You can remove a child from the order. If the child exists in a given order, then you need o update the number of children in this order. Otherwise, do nothing. You can retrieve the list of children in any given order.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions