Skip to content

Instantly share code, notes, and snippets.

@Aroueterra
Created May 28, 2021 05:01
Show Gist options
  • Save Aroueterra/d14d641de0107dd40ed2611b2c9886ee to your computer and use it in GitHub Desktop.
Save Aroueterra/d14d641de0107dd40ed2611b2c9886ee to your computer and use it in GitHub Desktop.
package practice;
//add class definitions below this line
class SodaMachine {
private String[] sodas = {"coke", "pepsi", "sprite", "dr. pepper"};
private int cokeInventory;
private int pepsiInventory;
private int spriteInventory;
private int drPepperInventory;
private int money;
private final int COST = 2;
public String[] getSodas() {
return sodas;
}
public int getCokeInventory() {
return cokeInventory;
}
public int getPepsiInventory() {
return pepsiInventory;
}
public int getSpriteInventory() {
return spriteInventory;
}
public int getDrPepperInventory() {
return drPepperInventory;
}
public int getMoney() {
return money;
}
public int getCOST() {
return COST;
}
public void setSodas(int index, String soda) {
this.sodas[index] = soda;
}
public void setCokeInventory(int cokeInventory) {
this.cokeInventory = cokeInventory;
}
public void setPepsiInventory(int pepsiInventory) {
this.pepsiInventory = pepsiInventory;
}
public void setSpriteInventory(int spriteInventory) {
this.spriteInventory = spriteInventory;
}
public void setDrPepperInventory(int drPepperInventory) {
this.drPepperInventory = drPepperInventory;
}
public void setMoney(int money) {
this.money = money;
}
public void buySoda(String order, int price) {
boolean available = false;
int index = 0;
for (String soda : sodas) {
if (soda == order) {
available = true;
break;
} else {
index++;
}
}
if (available == false) {
System.out.println("That soda is not sold in this machine.");
} else if (price < getCOST()) {
System.out.println("You did not insert enough money.");
} else {
switch (index) {
case 0:
if ((getCokeInventory() - 1) < 0) {
System.out.println("There are no cans of " + capitalize(order) + ".");
break;
}
setCokeInventory(getCokeInventory() - 1);
setMoney(getMoney() + COST);
break;
case 1:
if ((getPepsiInventory() - 1) < 0) {
System.out.println("There are no cans of " + capitalize(order) + ".");
break;
}
setPepsiInventory(getPepsiInventory() - 1);
setMoney(getMoney() + COST);
break;
case 2:
if ((getSpriteInventory() - 1) < 0) {
System.out.println("There are no cans of " + capitalize(order) + ".");
break;
}
setSpriteInventory(getSpriteInventory() - 1);
setMoney(getMoney() + COST);
break;
case 3:
if ((getDrPepperInventory() - 1) < 0) {
System.out.println("There are no cans of " + capitalize(order) + ".");
break;
}
setDrPepperInventory(getDrPepperInventory() - 1);
setMoney(getMoney() + COST);
break;
}
}
}
public String capitalize(String str) {
if (str == "" || str == null) {
return str;
} else {
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
}
public SodaMachine() {
this.sodas = new String[]{"coke", "pepsi", "sprite", "dr. pepper"};
this.cokeInventory = 20;
this.pepsiInventory = 20;
this.spriteInventory = 20;
this.drPepperInventory = 20;
this.money = 10;
}
}
//add class definitions above this line
public class SodaDispenser {
public static void main(String[] args) {
SodaMachine myMachine = new SodaMachine();
myMachine.setDrPepperInventory(0);
myMachine.buySoda("dr. pepper", 2);
System.out.println(myMachine.getMoney());
System.out.println(myMachine.getDrPepperInventory());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment