Question 1
public void updateInventory(int numParts) {
int baseCount = 0;
int wheelCount = 0;
for (int i = 1; i <= numParts; i++) {
String type = generator.nextPartType();
if (type.equals("base")) {
baseCount++;
} else {
wheelCount++;
}
}
basesAvail += baseCount;
wheelsAvail += wheelCount;
}
public String checkInventory(int numToys, String toyInfo) {
int atLoc = toyInfo.indexOf("*");
int dollarLoc = toyInfo.indexOf("$");
String modelName = toyInfo.substring(atLoc + 1, dollarLoc);
String wheelNum = toyInfo.substring(dollarLoc);
int wheelsPerToy = 2;
if (wheelNum.equals("four")) {
wheelsPerToy = 4;
}
if (numToys <= basesAvail && numToys * wheelsPerToy <= wheelsAvail) {
return modelName + "-enough parts";
} else {
return modelName + "-need parts";
}
}
Question 2
public class RentalCarPlan {
private double dailyRentalFee;
private int numberOfFreeMiles;
private double perMileCharge;
public RentalCarPlan(double fee, int mile, double milefee) {
dailyRentalFee = fee;
numberOfFreeMiles = mile;
perMileCharge = milefee;
}
public double calculateBill(int days, int miles) {
double totalRental = days * dailyRentalFee;
double mileageCharge = 0;
if (miles > numberOfFreeMiles) {
mileageCharge = (miles - numberOfFreeMiles) * perMileCharge;
}
return totalRental + mileageCharge;
}
public boolean costsLessThan(RentalCarPlan otherPlan, int days, int miles) {
double cost1 = calculateBill(days, miles);
double cost2 = otherPlan.calculateBill(days, miles);
return cost1 < cost2;
}
}