Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class StockManagement {
- private static Product[] products;
- private static int maxProducts = 0;
- private static int index = 0;
- private static int selection = 0;
- public static int getMaxNumProducts(Scanner sc) {
- System.out.println("Please enter maximum number of products:");
- maxProducts = sc.nextInt();
- while(maxProducts < 0 ) {
- System.out.println("Please enter maximum number of products:");
- maxProducts = sc.nextInt();
- }
- System.out.println();
- if(maxProducts > 0) {
- products = new Product[maxProducts];
- }
- return maxProducts;
- }
- public static int viewProduct(Product[] p, Scanner sc) {
- int choice;
- for(int i=0;i<maxProducts; i++) {
- System.out.println("Index: "+i+". \n"+products[i].toString());
- System.out.println();
- }
- System.out.print("Please enter the index of product you want to select : ");
- choice = sc.nextInt();
- while(choice < 0 || choice >= maxProducts) {
- System.out.print("Please enter the product you want to select : ");
- choice = sc.nextInt();
- }
- System.out.println();
- return choice;
- }
- public static int displayMenu(Scanner sc) {
- int choice;
- System.out.println("1. View products");
- System.out.println("2. Add stock");
- System.out.println("3. Deduct stock");
- System.out.println("4. Discontinue product");
- System.out.println("0. Exit");
- System.out.println("Please enter a menu option: ");
- choice = sc.nextInt();
- while(choice < 0 || choice >4) {
- System.out.println("Please enter a menu option: ");
- choice = sc.nextInt();
- }
- System.out.println();
- return choice;
- }
- public static void addStock(Product[] p, Scanner sc) {
- int num=-1;
- System.out.println("How many values do you want to add: ");
- num = sc.nextInt();
- while(num < 0) {
- System.out.println("Please enter only positive values!");
- System.out.println("How many values do you want to add: ");
- num = sc.nextInt();
- }
- System.out.println();
- p[selection].addQuantity(num);
- }
- public static void deductStock(Product[] p, Scanner sc) {
- int num=-1;
- System.out.println("How many values do you want to deduct: ");
- num = sc.nextInt();
- while(num < 0 || num > p[selection].getQuantity()) {
- System.out.println("Please enter value greater than equal to 0 and not more than current quantity of stock");
- System.out.println("How many values do you want to deduct: ");
- num = sc.nextInt();
- }
- System.out.println();
- p[selection].deductQuantity(num);
- }
- public static void discontinueProduct(Product[] p, Scanner sc) {
- p[selection].setStatus(false);
- }
- public static void executeMethod(Product[] p, Scanner sc, int choice) {
- switch(choice) {
- case 1:
- selection = viewProduct(p, sc);
- System.out.println("Product selected: "+p[selection].getName());
- break;
- case 2:
- addStock(p, sc);
- break;
- case 3:
- deductStock(p,sc);
- break;
- case 4:
- discontinueProduct(p, sc);
- break;
- default:
- System.out.println("Thank you for visting! Goodbye!");
- break;
- }
- }
- public static void addProduct(Product[] p, Scanner sc) {
- int choice;
- System.out.println("Please select (1 or 2) depending upon product to add: ");
- System.out.println("1. TV");
- System.out.println("2. Refrigerator");
- choice = sc.nextInt();
- while(choice <1 || choice>2) {
- System.out.println("Please select (1 or 2) depending upon product to add: ");
- System.out.println("1. TV");
- System.out.println("2. Refrigerator");
- choice = sc.nextInt();
- }
- switch(choice) {
- case 1:
- addTV(p,sc);
- break;
- case 2:
- addRefrigerator(p, sc);
- break;
- }
- }
- public static void addRefrigerator(Product[] p, Scanner sc) {
- String name, design, color;
- int capacity, quantity, itemNum;
- double price;
- System.out.println();
- System.out.println("Please enter name of refrigerator: ");
- name = sc.next();
- System.out.println("Please enter door design: ");
- design = sc.next();
- System.out.println("Please enter color: ");
- color = sc.next();
- System.out.println("Please enter capacity: ");
- capacity = sc.nextInt();
- System.out.println("Please enter quantity in stock: ");
- quantity = sc.nextInt();
- System.out.println("Please enter price: ");
- price = sc.nextDouble();
- System.out.println("Please enter item number: ");
- itemNum = sc.nextInt();
- p[index] = new Refrigerator(name, design, color, capacity, quantity, price, itemNum);
- index = index + 1;
- System.out.println();
- }
- public static void addTV(Product[] p, Scanner sc) {
- String name, screenType;
- int resolution, displaySize, quantity, itemNum;
- double price;
- System.out.println();
- System.out.println("Please enter name of TV: ");
- name = sc.next();
- System.out.println("Please enter screen type: ");
- screenType = sc.next();
- System.out.println("Please enter resolution: ");
- resolution = sc.nextInt();
- System.out.println("Please enter display size: ");
- displaySize = sc.nextInt();
- System.out.println("Please enter quantity in stock: ");
- quantity = sc.nextInt();
- System.out.println("Please enter price: ");
- price = sc.nextDouble();
- System.out.println("Please enter item number: ");
- itemNum = sc.nextInt();
- p[index] = new TV(name, screenType, resolution, displaySize, quantity, price, itemNum);
- index = index + 1;
- System.out.println();
- }
- public static void displayContent(Product[] p) {
- for(int i=0; i<maxProducts;i++) {
- System.out.println(p[i].toString());
- }
- }
- public static void main(String args[]) {
- String name;
- int choice;
- System.out.println("Please enter your name: ");
- Scanner sc = new Scanner(System.in);
- name = sc.nextLine();
- System.out.println("Welcome "+name+"! Please select menu option: ");
- System.out.println("1. Add product");
- System.out.println("0. Exit");
- choice = sc.nextInt();
- System.out.println();
- switch(choice) {
- case 0:
- System.out.println("Thank you for visting! Goodbye!");
- break;
- case 1:
- maxProducts = getMaxNumProducts(sc);
- for(int i=0; i<maxProducts;i++) {
- addProduct(products, sc);
- }
- break;
- }
- choice = -1;
- System.out.println();
- while(choice!=0){
- choice = displayMenu(sc);
- System.out.println();
- executeMethod(products,sc,choice);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement