Created
December 9, 2018 20:32
-
-
Save D3strukt0r/bb3ac7a3be3286a09a83ca5b1a74833a to your computer and use it in GitHub Desktop.
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
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.Scanner; | |
public class Shop { | |
public static void main(String[] args) { | |
// Create the products list | |
ArrayList<Product> productList = new ArrayList<>(); | |
// Create and add product 1 | |
LanguagesString n1 = new LanguagesString() | |
.setEn("RAM") | |
.setDe("RAM"); | |
LanguagesString d1 = new LanguagesString() | |
.setEn("Storage for your PC") | |
.setDe("Speicher für dein PC"); | |
Product p1 = new Product(n1, d1, 1, 199.99); | |
productList.add(p1); | |
// Create and add product 2 | |
LanguagesString n2 = new LanguagesString() | |
.setEn("CPU") | |
.setDe("Prozessor"); | |
LanguagesString d2 = new LanguagesString() | |
.setEn("The brain of your computer") | |
.setDe("Das Gehirn deines Computers"); | |
Product p2 = new Product(n2, d2, 2, 199.99); | |
productList.add(p2); | |
// What language does the user want? | |
Scanner input = new Scanner(System.in); | |
System.out.println("Select language: \n1) English\n2) German"); | |
int lang = input.nextInt(); | |
String language; | |
if (lang == 1) { | |
language = "en"; | |
} else if (lang == 2) { | |
language = "de"; | |
} else { | |
language = "en"; | |
} | |
// Print all the products | |
Iterator it = productList.iterator(); | |
System.out.println("Products:"); | |
int listID = 0; | |
// Go through all the products and print each one | |
while (it.hasNext()) { | |
System.out.println("ID: " + listID); | |
Product currentProduct = (Product) it.next(); | |
System.out.println(currentProduct.toString(language)); | |
listID++; | |
} | |
// Select product to buy | |
System.out.println("Add a product to the cart by entering it's ID or 'pay' to pay"); | |
ArrayList<Product> shoppingCart = new ArrayList<>(); | |
String command; | |
int product; | |
command = input.nextLine(); | |
// If no command has been set, or the command is "pay" than skip | |
while (command == null || !command.equals("pay")) { | |
command = input.nextLine(); | |
// End the loop if user entered "pay" | |
if (command.equals("pay")) { | |
continue; | |
} | |
// Get product id and add to cart | |
try { | |
// Check whether it can be translated into an integer | |
product = Integer.parseInt(command); | |
} catch (NumberFormatException e) { | |
System.out.println("You can either enter 'pay' or the product id, nothing else"); | |
continue; | |
} | |
try { | |
// Check whether the product exists and add it to the shopping cart | |
Product addToCart = productList.get(product); | |
shoppingCart.add(addToCart); | |
System.out.println(addToCart.getName(language) + " was added to the cart"); | |
} catch (IndexOutOfBoundsException e) { | |
System.out.println("This product doesn't exist"); | |
} | |
} | |
// Check whether cart is empty or not | |
if (shoppingCart.isEmpty()) { | |
System.out.println("You haven't bought anything"); | |
} else { | |
Iterator itCart = shoppingCart.iterator(); | |
System.out.println("You bought:"); | |
while (itCart.hasNext()) { | |
Product currentProduct = (Product) itCart.next(); | |
System.out.println(currentProduct.getName(language) + ": " + currentProduct.getDescription(language)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment