Created
October 31, 2018 16:41
-
-
Save TimMazhari/e9f6fd9a4e3275c728223febf2c50a67 to your computer and use it in GitHub Desktop.
Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package garage; | |
public class Car { | |
String carName; | |
String carModel; | |
String status; | |
String color; | |
int value; | |
boolean sold; | |
/** | |
* getters and setters for all the car attributes | |
* @return | |
*/ | |
public boolean isSold() { | |
return sold; | |
} | |
public void setSold(boolean sold) { | |
this.sold = sold; | |
} | |
public int getValue() { | |
return value; | |
} | |
public String getCarName() { | |
return carName; | |
} | |
public String getCarModel() { | |
return carModel; | |
} | |
public String getStatus() { | |
return status; | |
} | |
public void setStatus(String status) { | |
this.status = status; | |
} | |
public String getColor() { | |
return color; | |
} | |
/** | |
* constructor | |
* @param carName | |
* @param carModel | |
* @param color | |
* @param status | |
* @param value | |
* @param sold | |
*/ | |
public Car(String carName, String carModel, String color, String status, int value, boolean sold){ | |
this.carName = carName; | |
this.carModel = carModel; | |
this.color = color; | |
this.status = status; | |
this.value = value; | |
this.sold = sold; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package garage; | |
import java.util.ArrayList; | |
public class CarListOutput { | |
/** | |
* Formated output for the "repair car" list output. | |
* @param carList | |
*/ | |
public void brokenCarListOutput(ArrayList<Car> carList){ | |
System.out.println("The following cars are registered as broken:"); | |
System.out.format("%20s %20s %20s %20s %20s %20s %20s", "Car Number", "Car Name", "Car Model", "Car Color", "Car Status", "Car Value", "Sold"); | |
System.out.println(); | |
System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------"); | |
for(int i = 0; i < carList.size(); i++) { | |
if(carList.get(i).getStatus().toLowerCase().equals("broken")){ | |
String valueString = String.valueOf(carList.get(i).getValue()); | |
String soldString = ""; | |
if(carList.get(i).isSold()){ | |
soldString = "Yes"; | |
} | |
else if(!carList.get(i).isSold()){ | |
soldString = "No"; | |
} | |
System.out.format("%20d %20s %20s %20s %20s %20s %20s", | |
i, carList.get(i).getCarName(), carList.get(i).getCarModel(), carList.get(i).getColor(), carList.get(i).getStatus(), valueString, soldString); | |
} | |
System.out.println(); | |
} | |
System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------"); | |
System.out.println("Which one do you want to repair? (enter car number):"); | |
} | |
/** | |
* Formated output for the "sell car" list. | |
* @param carList | |
*/ | |
public void unsoldCarListOutput(ArrayList<Car> carList){ | |
System.out.println("The following cars are registered as unsold:"); | |
System.out.format("%20s %20s %20s %20s %20s %20s %20s", "Car Number", "Car Name", "Car Model", "Car Color", "Car Status", "Car Value", "Sold"); | |
System.out.println(); | |
System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------"); | |
for(int i = 0; i < carList.size(); i++) { | |
if(!carList.get(i).isSold()){ | |
String valueString = String.valueOf(carList.get(i).getValue()); | |
String soldString = ""; | |
if(carList.get(i).isSold()){ | |
soldString = "Yes"; | |
} | |
else if(!carList.get(i).isSold()) { | |
soldString = "No"; | |
} | |
System.out.format("%20d %20s %20s %20s %20s %20s %20s", | |
i, carList.get(i).getCarName(), carList.get(i).getCarModel(), carList.get(i).getColor(), carList.get(i).getStatus(), valueString, soldString); | |
System.out.println(); | |
} | |
} | |
System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------"); | |
System.out.println("Which one do you want to sell? (enter car number):"); | |
} | |
/** | |
* Formated output for the "display cars" list. | |
* @param carList | |
*/ | |
public void regularCarListOutput(ArrayList<Car> carList){ | |
System.out.format("%20s %20s %20s %20s %20s %20s %20s", "Car Number", "Car Name", "Car Model", "Car Color", "Car Status", "Car Value", "Sold"); | |
System.out.println(); | |
System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------"); | |
for (int i = 0; i < carList.size(); i++) { | |
String valueString = String.valueOf(carList.get(i).getValue()); | |
String soldString = ""; | |
if(carList.get(i).isSold()){ | |
soldString = "Yes"; | |
} | |
else if(!carList.get(i).isSold()){ | |
soldString = "No"; | |
} | |
System.out.format("%20d %20s %20s %20s %20s %20s %20s", | |
i, carList.get(i).getCarName(), carList.get(i).getCarModel(), carList.get(i).getColor(), carList.get(i).getStatus(), valueString, soldString); | |
System.out.println(); | |
} | |
System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package garage; | |
import java.util.ArrayList; | |
public class ChangeCarAttribute { | |
/** | |
* Check for every slot in carlist if the control variable matches the choice from the user, and if the slot of the | |
* arraylist at the index of the control variable is not null, set status to repaired. | |
* @param carList | |
* @param choice | |
*/ | |
public void repair(ArrayList<Car> carList, int choice){ | |
for(int i = 0; i < carList.size(); i++) { | |
if(i == choice && carList.get(i) != null) { | |
carList.get(i).setStatus("repaired"); | |
try { | |
System.out.println("Repairing car " + carList.get(i).carName + " " + carList.get(i).carModel); | |
Thread.sleep(1000); | |
System.out.println("The car is now repaired."); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
else{ | |
System.out.println("Invalid car number."); | |
} | |
} | |
} | |
/** | |
* method used to change the attribute "sold" from yes to no. | |
* @param carList | |
* @param bankAccount | |
* @param choice | |
*/ | |
public void sell(ArrayList<Car> carList, Money bankAccount, int choice){ | |
for(int i = 0; i < carList.size(); i++){ | |
if(i == choice && carList.get(i) != null) { | |
carList.get(i).setSold(true); | |
try { | |
System.out.println("Selling car " + carList.get(i).carName + " " + carList.get(i).carModel); | |
Thread.sleep(2000); | |
bankAccount.setBankAccount(carList.get(i).getValue()); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
else{ | |
System.out.println("Invalid car number."); | |
} | |
} | |
} | |
/** | |
* method delete is used to actually delete the car from the list. | |
* @param carList | |
* @param choice | |
*/ | |
public void delete(ArrayList<Car> carList, int choice){ | |
for(int i = 0; i < carList.size(); i++){ | |
if(i == choice && carList.get(i) != null){ | |
try{ | |
System.out.println("Deleting car " + carList.get(i).carName + " " + carList.get(i).carModel); | |
Thread.sleep(1000); | |
} | |
catch(InterruptedException e){ | |
e.printStackTrace(); | |
} | |
carList.remove(i); | |
} | |
else{ | |
System.out.println("Invalid car number."); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package garage; | |
/** | |
* Used to clear the screen. | |
*/ | |
public class ClearScreen { | |
public static void clearScreen() { | |
for(int i = 0; i < 50000; i++){ | |
System.out.println(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package garage; | |
import java.util.Scanner; | |
/** | |
* Input for normal Strings. This is used for reading the car attributes. | |
*/ | |
public class InputOutput { | |
private String input; | |
Scanner sc = new Scanner(System.in); | |
public String inputManager() { | |
input = sc.nextLine(); | |
return input; | |
} | |
/** | |
* Input for number. | |
* checking if the first read input string contains chars other than numbers. if so, ask the user to enter a number. | |
*/ | |
private int inputNumber; | |
private String inputNumberString; | |
Scanner scNumber = new Scanner(System.in); | |
public int inputManagerNumber(int tempNumber) { | |
while (tempNumber == 0){ | |
inputNumberString = scNumber.nextLine(); | |
if(inputNumberString.matches("[a-zA-Z]+")){ | |
System.out.println("Please enter a number:"); | |
} | |
else{ | |
inputNumber = Integer.parseInt(inputNumberString); | |
tempNumber = 1; | |
} | |
} | |
return inputNumber; | |
} | |
/** | |
* Special Scanner for the car attribute "Status" this is needed because the Status needs to be | |
* "broken" or "repaired". | |
*/ | |
private String inputBroken; | |
Scanner scBroken = new Scanner(System.in); | |
public String inputManagerBroken(int tempBroken) { | |
while (tempBroken == 0){ | |
inputBroken = scBroken.nextLine(); | |
if(inputBroken.toLowerCase().equals("broken")){ | |
tempBroken = 1; | |
} | |
else if(inputBroken.toLowerCase().equals("repaired")){ | |
tempBroken = 1; | |
} | |
else{ | |
System.out.println("Please enter valid input: 'broken' or 'repaired'"); | |
tempBroken = 0; | |
} | |
} | |
return inputBroken; | |
} | |
/** | |
* Separate scanner for the car attribute "sold". Because the user writes "yes" or "no", this is needed to even set | |
* the boolean value. | |
*/ | |
private boolean inputBool; | |
private String inputBoolString; | |
Scanner scBool = new Scanner(System.in); | |
public boolean inputManagerBool(int tempBool) { | |
while(tempBool == 0) { | |
inputBoolString = scBool.nextLine(); | |
if (inputBoolString.toLowerCase().equals("yes")) { | |
inputBool = true; | |
tempBool = 1; | |
} else if (inputBoolString.toLowerCase().equals("no")) { | |
inputBool = false; | |
tempBool = 1; | |
} else { | |
System.out.println("Please enter 'yes' or 'no'"); | |
} | |
} | |
return inputBool; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package garage; | |
import java.util.ArrayList; | |
public class main { | |
/** | |
* Creating arraylist and array. | |
* ArrayList "carList" is where the whole cars will be saved. | |
* Array "carAttributes" is where the individual attributes will be saved (name, color etc.) | |
*/ | |
private static ArrayList<Car> carList = new ArrayList<>(); | |
private static String[] carAttributes = new String[4]; | |
public static void main(String [ ] args){ | |
/** | |
* Creating the objects which will be needed throughout the program. | |
*/ | |
ClearScreen screen = new ClearScreen(); | |
InputOutput menuInput = new InputOutput(); | |
InputOutput inputOutput = new InputOutput(); | |
CarListOutput listOutput = new CarListOutput(); | |
Money bankAccountOne = new Money(); | |
ChangeCarAttribute changeCar = new ChangeCarAttribute(); | |
/** | |
* clearing the screen once for a normal output. | |
* getting user input which will be needed to determine the following action. | |
* boolean "close" is needed to shut down the program if wanted. | |
*/ | |
screen.clearScreen(); | |
System.out.println("What you want to do? (type 'help' for a menu list):"); | |
String menuInputString = menuInput.inputManager(); | |
boolean close = false; | |
do { | |
if (menuInputString.toLowerCase().equals("new car")) { | |
/** | |
* asking for the car attributes and saving the user input | |
* creating "value" and "sold". These are attributes needed to create the car | |
*/ | |
int value; | |
boolean sold; | |
System.out.println("Name:"); | |
carAttributes[0] = inputOutput.inputManager(); | |
System.out.println("Model:"); | |
carAttributes[1] = inputOutput.inputManager(); | |
System.out.println("Color:"); | |
carAttributes[2] = inputOutput.inputManager(); | |
System.out.println("Status:"); | |
carAttributes[3] = inputOutput.inputManagerBroken(0); | |
System.out.println("Value (in $):"); | |
value = inputOutput.inputManagerNumber(0); | |
System.out.println("Sold (Yes or No):"); | |
sold = inputOutput.inputManagerBool(0); | |
/** | |
* Adding new slot to the arraylist "carList". Filling the slot with a newly created object "Car". | |
* "Car" contains the attributes saved in the array "carAttributes" in addition to the variables "value" | |
* and "sold" | |
*/ | |
carList.add(new Car(carAttributes[0], carAttributes[1], carAttributes[2], carAttributes[3], value, sold)); | |
System.out.println("New Car added to garage."); | |
} | |
else if (menuInputString.toLowerCase().equals("display cars")) { | |
/** | |
* using method "regularCarListOutput" which creates a normal output of the cars in the arrayList "carList" | |
* with all their attributes. | |
*/ | |
if(carList.size() > 0){ | |
listOutput.regularCarListOutput(carList); | |
} | |
else{ | |
System.out.println("No cars to display."); | |
} | |
} | |
else if (menuInputString.toLowerCase().equals("repair car")) { | |
/** | |
* create a output of the cars which have set the attribute "Status" to "broken". | |
* integer "choice" is needed by the method "repair". "choice" will determine which car, if wanted, will be repaired. | |
*/ | |
if(carList.size() > 0) { | |
int choice; | |
listOutput.brokenCarListOutput(carList); | |
choice = inputOutput.inputManagerNumber(0); | |
changeCar.repair(carList, choice); | |
} | |
else{ | |
System.out.println("No cars are broken."); | |
} | |
} | |
else if(menuInputString.toLowerCase().equals("sell car")){ | |
/** | |
* First, check if the carList even contains any cars which could be sold. If not, print out that there | |
* are no cars available. | |
* Output a list of the cars with the attribute "Sold" will be printed. | |
* Attribute "Sold" will be changed from no to yes in the chosen car. | |
*/ | |
if(carList.size() > 0) { | |
listOutput.unsoldCarListOutput(carList); | |
int choice = inputOutput.inputManagerNumber(0); | |
changeCar.sell(carList, bankAccountOne, choice); | |
} | |
else{ | |
System.out.println("No cars available to sell"); | |
} | |
} | |
else if(menuInputString.toLowerCase().equals("help")){ | |
/** | |
* Help list outpout | |
*/ | |
System.out.println("Here is a list of the possible commands with a short description:"); | |
System.out.println("New car (creates a new car)"); | |
System.out.println("Display cars (displays a list of all the cars)"); | |
System.out.println("Repair car (displays a list of the broken cars and lets you repair one)"); | |
System.out.println("Sell car (displays a list of the unsold cars and lets you sell one)"); | |
System.out.println("Delete car (delete a car out of the list)"); | |
System.out.println("Get money (display your money)"); | |
System.out.println("Close (exits the program)"); | |
} | |
else if(menuInputString.toLowerCase().equals("get money")){ | |
/** | |
* getBankAccount prints out the current money available | |
*/ | |
System.out.println("You have " + bankAccountOne.getBankAccount() + "$"); | |
} | |
else if(menuInputString.toLowerCase().equals("delete car")){ | |
/** | |
* Check if there are any cars in arraylist carlist. if so, print out a list of all the cars in the program. | |
* choice is the input which determines the car that will be deleted. | |
*/ | |
if(carList.size() > 0) { | |
listOutput.regularCarListOutput(carList); | |
System.out.println("Which car do you want to delete?"); | |
int choice = inputOutput.inputManagerNumber(0); | |
changeCar.delete(carList, choice); | |
} | |
else{ | |
System.out.println("No cars available to delete"); | |
} | |
} | |
else{ | |
System.out.println("Unknown command. Type 'help' for a list of the available commands"); | |
} | |
System.out.println("What you want to do? (type 'help' for a menu list):"); | |
menuInputString = inputOutput.inputManager(); | |
screen.clearScreen(); | |
if(menuInputString.toLowerCase().equals("close")){ | |
close = true; | |
} | |
}while (close == false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package garage; | |
public class Money { | |
/** | |
* getters and setters for the bank account. bank account is used to display the money. | |
* @return | |
*/ | |
public int getBankAccount() { | |
return bankAccount; | |
} | |
public void setBankAccount(int bankAccount) { | |
this.bankAccount = getBankAccount() + bankAccount; | |
} | |
int bankAccount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ciao homie