Advertisement
Guest User

store

a guest
Mar 11th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.43 KB | None | 0 0
  1. import scala.io.StdIn.readInt
  2.  
  3. class Product(name:String, price: Int, count: Int) { //count shows the intial stock
  4.     var sold: Int = 0; //currently no.of items sold
  5.     def print() = println("Product: " + name + " (" + price + " LKR) : " + getStock());
  6.     def getName() = name; //returns the product name
  7.     def getPrice() = price; //returns the product price
  8.     def getStock() = count - sold; //returns the products current stock
  9.     def decrementStock(qty: Int) = sold += qty; //updates the stock
  10. };
  11.  
  12. object store {
  13.     var products: Array[Product] = new Array[Product](3); //consist of the products of the market
  14.  
  15.     def main(args: Array[String]){
  16.         //adding items to the array
  17.         products(0) = new Product("Apple", 10, 3);
  18.         products(1) = new Product("Mango", 30, 2);
  19.         products(2) = new Product("Orange", 40, 1);
  20.        
  21.         //infinite loop to main menu
  22.         while(true){
  23.             printMainMenu();
  24.             val choice = readInt();
  25.  
  26.             choice match {
  27.                 case 1 => printStock(); //prints the current stock
  28.                 case 2 => newCustomer(); //handels the customer actions
  29.                 case _ => return; //exit the program whenever a value otherthan 1,2
  30.             };
  31.         }
  32.     }
  33.  
  34.     //prints the main menu
  35.     def printMainMenu() {
  36.         println("********** Welcome to Store on Reid **********");
  37.         println("Choose an option:");
  38.         println("\t1. Print current stock");
  39.         println("\t2. New customer");
  40.         println("\t3. Exit");
  41.  
  42.         print("Enter your choice: ");
  43.     }
  44.  
  45.     //customers menu actions
  46.     def printCustomerMenu() {
  47.         println("The following items are available for you to buy");
  48.         printStock();
  49.         println("Choose an item number to add to cart. Press 0 to complete purchase.");
  50.         print("Enter your choice: ");
  51.     }
  52.  
  53.     //prints the current stock
  54.     def printStock() {
  55.         var index: Int = 1; //varible to print the index
  56.         println("Item\t\tPrice\t\tQty");
  57.         for(item <- products){ // for each loop on products array
  58.             print(index + ". ");
  59.             print(item.getName() + "\t");
  60.             print(item.getPrice() + " LKR\t\t");
  61.             print(item.getStock() + "\n");
  62.             index = index + 1;
  63.         }
  64.         println("");
  65.     }
  66.  
  67.     //handles the customer actions
  68.     def newCustomer() {
  69.         var sum: Int = 0; //total of the bill
  70.         var continue: Boolean = true; //variable to break out of the loop
  71.         while(continue){
  72.             printCustomerMenu();
  73.             val choice = readInt(); // 1 2 3
  74.             if(choice > 3 || choice < 1) {
  75.                 continue = false;
  76.             } else {
  77.                 print("How many do you need? ");
  78.                 val qty = readInt();    
  79.                 var product = products(choice-1); //get the specific product reference
  80.                 if(product.getStock() < qty) println("Product doesn't have enough stock");
  81.                 else {
  82.                     sum = sum + product.getPrice() * qty;
  83.                     product.decrementStock(qty);
  84.                     println("Product " + product.getName() + " added to cart");
  85.                 }
  86.             }
  87.             println("Current Cart Total: " + sum + " LKR");
  88.             println("");
  89.         }
  90.         println("Cart Total: " + sum + " LKR");
  91.         println("Thank you for shopping!");
  92.         println("");
  93.     }
  94. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement