In: Computer Science
JAVAFX LAB 2
1. The Soda class has two fields: a String for the name and a double for the price.
2. The Soda class has two constructors. The first is a parameterized constructor that takes a String and a double to be assigned to the fields of the class. The second is a copy constructor that takes a Soda object and assigns the name and price of that object to the newly constructed Soda object.
3. The Soda class has two getters: one to return the name and one to return the price.
4. The VendingMachine has one field for a Soda. This can initially reference a null object by assigning null to the field.
5. The VendingMachine has one constructor that takes a Soda. Remember to assign a new Soda to the Soda field securely.
6. The addSoda method of the VendingMachine class takes a Soda. Check if the field of the class references a null object (you can use the equals operator to check what the variable references). If the field references a null object, assign the new Soda to the Soda field securely and return true. If the field does not reference a null object, just return false.
7. The removeSoda method of the VendingMachine class takes no arguments. Check if the field of the class does not reference a null object (you can use the equals operator to check what the variable references). If the field does not reference a null object, assign null to the Soda field and return true. If the field references a null object, just return false. 8. In the VendingMachineDemo, change the name and price for the Soda being added to the VendingMachine to any other name and price of your choosing.
public class VendingMachineDemo {
public static void main(String[] args) {
Soda soda = new Soda("Coke",10.0);
VendingMachine machine = new VendingMachine(soda);
System.out.println("Vending machine currently has ::"+ machine.getSoda());
machine.getSoda().setName("Sprite");
machine.getSoda().setPrice(20.0);
System.out.println("After renaming Vending machine currently has ::"+ machine.getSoda());
}
}
class Soda {
private String name;
private double price;
public Soda(String name, double price) {
this.name = name;
this.price = price;
}
public Soda(Soda source) {
this.name = source.name;
this.price = source.price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Soda [name=" + name + ", price=" + price + "]";
}
}
class VendingMachine {
private Soda soda;
public Soda getSoda() {
return soda;
}
public void setSoda(Soda soda) {
this.soda = soda;
}
VendingMachine(Soda sourceSoda) {
if(this.soda == null) {
soda = new Soda(sourceSoda.name,sourceSoda.price);
}
}
public boolean addSoda(Soda soda) {
if(this.soda == null) {
soda = new Soda(soda.name,soda.price);
return true;
} else {
return false;
}
}
public boolean removeSoda() {
if(this.soda != null) {
this.soda = null;
return true;
} else {
return false;
}
}
}
Here first I created a soda object with soda name as "Coke" and price is 10.0
After that I have updated the soda object with values.
Now new name is "Sprite" and price is 20.0.