Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.io.StdIn.readInt
- class Product(name:String, price: Int, count: Int) { //count shows the intial stock
- var sold: Int = 0; //currently no.of items sold
- def print() = println("Product: " + name + " (" + price + " LKR) : " + getStock());
- def getName() = name; //returns the product name
- def getPrice() = price; //returns the product price
- def getStock() = count - sold; //returns the products current stock
- def decrementStock(qty: Int) = sold += qty; //updates the stock
- };
- object store {
- var products: Array[Product] = new Array[Product](3); //consist of the products of the market
- def main(args: Array[String]){
- //adding items to the array
- products(0) = new Product("Apple", 10, 3);
- products(1) = new Product("Mango", 30, 2);
- products(2) = new Product("Orange", 40, 1);
- //infinite loop to main menu
- while(true){
- printMainMenu();
- val choice = readInt();
- choice match {
- case 1 => printStock(); //prints the current stock
- case 2 => newCustomer(); //handels the customer actions
- case _ => return; //exit the program whenever a value otherthan 1,2
- };
- }
- }
- //prints the main menu
- def printMainMenu() {
- println("********** Welcome to Store on Reid **********");
- println("Choose an option:");
- println("\t1. Print current stock");
- println("\t2. New customer");
- println("\t3. Exit");
- print("Enter your choice: ");
- }
- //customers menu actions
- def printCustomerMenu() {
- println("The following items are available for you to buy");
- printStock();
- println("Choose an item number to add to cart. Press 0 to complete purchase.");
- print("Enter your choice: ");
- }
- //prints the current stock
- def printStock() {
- var index: Int = 1; //varible to print the index
- println("Item\t\tPrice\t\tQty");
- for(item <- products){ // for each loop on products array
- print(index + ". ");
- print(item.getName() + "\t");
- print(item.getPrice() + " LKR\t\t");
- print(item.getStock() + "\n");
- index = index + 1;
- }
- println("");
- }
- //handles the customer actions
- def newCustomer() {
- var sum: Int = 0; //total of the bill
- var continue: Boolean = true; //variable to break out of the loop
- while(continue){
- printCustomerMenu();
- val choice = readInt(); // 1 2 3
- if(choice > 3 || choice < 1) {
- continue = false;
- } else {
- print("How many do you need? ");
- val qty = readInt();
- var product = products(choice-1); //get the specific product reference
- if(product.getStock() < qty) println("Product doesn't have enough stock");
- else {
- sum = sum + product.getPrice() * qty;
- product.decrementStock(qty);
- println("Product " + product.getName() + " added to cart");
- }
- }
- println("Current Cart Total: " + sum + " LKR");
- println("");
- }
- println("Cart Total: " + sum + " LKR");
- println("Thank you for shopping!");
- println("");
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement