Skip to content

Commit

Permalink
feat(5.5.2): finish Oven.java
Browse files Browse the repository at this point in the history
  • Loading branch information
101zh committed Nov 15, 2023
1 parent e6bebd1 commit 0932280
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/main/java/Unit5/Oven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package Unit5;

// Have to get rid of package statement

public class Oven {
// Represents the maximum possible temperature of an oven
private int maxTemp;

// Represents the current temperature of an oven
private int currentTemp;

// Constructs an oven with the given max temp and starting temp. The maximum
// temperature of an oven must be greater than 0, but no more than 500.
// The starting temperature should be less than or equal to the maximum t
// temperature, but no less than 0.
public Oven(int maxTemperature, int startTemperature) {
if (maxTemperature > 500 || maxTemperature < 0) {
maxTemperature = 500;
}
if (startTemperature > maxTemperature) {
startTemperature = maxTemperature;
} else if (startTemperature < 0) {
startTemperature = 0;
}

maxTemp = maxTemperature;
currentTemp = startTemperature;
}

// Returns the maximum temperature of an oven
public int getMaxTemp() {
return maxTemp;
}

// Returns the current temperature of an oven
public int getCurrentTemp() {
return currentTemp;
}

// Turns an oven off by setting the current temperature to 0.
public void turnOff() {
if (currentTemp > 0) {
currentTemp = 0;
}

}

// Returns whether the current temperature of an oven is greater than 0. Should
// return false if the current temperature is 0.
public boolean isOn() {
if (currentTemp > 0) {
return true;
}
return false;
}

// Sets the current temperature of an oven to the given value. Remember,
// the current temperature should not exceed the max temp.
public void preheat(int temp) {
if (temp > 0) {
if (temp > maxTemp) {
currentTemp = maxTemp;
} else {
currentTemp = temp;
}
}

}
}
43 changes: 43 additions & 0 deletions src/test/java/Unit5/runner_Oven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Unit5;

import java.util.Scanner;

public class runner_Oven
{

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Maximum oven temperature:");
int maxTemp = scan.nextInt();
System.out.println("Starting temperature of the oven:");
int startTemp = scan.nextInt();

Oven oven = new Oven(maxTemp, startTemp);

String instruction = "";

while (!instruction.equals("q"))
{
System.out.println("New oven with a maximum temperature of " + oven.getMaxTemp() + " and a starting temperature of " + oven.getCurrentTemp() + " degrees.");

System.out.println("To preheat the oven enter \"p\", to turn the oven off enter" + " \"o\", to restart enter \"r\", to quit enter \"q\"");

instruction = scan.next();

if (instruction.equals("p"))
{
System.out.println("Enter the temperature to preheat the oven to:");
int temp = scan.nextInt();
oven.preheat(temp);
System.out.println("Current temperature of the oven is now " + oven.getCurrentTemp() + " degrees\n");
}
else if (instruction.equals("o"))
{
System.out.println("Turning the oven off.\n");
oven.turnOff();
}
}
}
}

0 comments on commit 0932280

Please sign in to comment.