Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Họ và tên: Lê Vượng

MSSV: DE160106
Class Main:
public class Main {
    public static void main(String[] args) {
        Store st = new Store();
        Shop sh = new Shop();
        Basket bk = new Basket(st,st.getLs());
        sh = new Shop(st,bk);
        outer:
        while (true) {
            int choice = Manager.menu();
            if (choice == 1){
                while (true) {
                    int choiceA = Manager.menuAdmin();
                    switch (choiceA) {
                        case 1:
                            System.out.println(“---------------------\n- Add
Items –“);
                            st.addItem();
                            break;
                        case 2:
                            System.out.println(“---------------------\n- Update
Items –“);
                            st.updateItem();
                            break;
                        case 3:
                            System.out.println(“---------------------\n- View
Item –“);
                            st.displayListItems();
                            break;
                        case 4:
                            System.out.println(“---------------------\n- Remove
Items –“);
                            st.removeItem();
                            break;
                        case 5:
                            System.out.println(“---------------------\n Exit
admin mod...”);
                            continue outer;
                        }
                }
            }
            if (choice == 2){
                while (true){
                    int 8oolea = Manager.menuCustomer();
                    switch (8oolea){
                        case 1:
                            System.out.println(“---------------------\n- Add
Shopping Basket –“);
                            sh.shopping();
                            break;
                        case 2:
                            System.out.println(“---------------------\n- Display
the Shopping Basket –“);
                            sh.getCart().display();
                            break;
                        case 3:
                            System.out.println(“---------------------\n- Remove
Shopping Basket –“);
                            sh.getCart().removeBasket();
                            break;
                        case 4:
                            System.out.println(“---------------------\n- Print
Invoice(Orders) –“);
                            sh.getCart().printInvoice();
                            break;
                        case 5:
                            System.out.println(“---------------------\nExiting
costumer mode...”);
                            continue outer;
                    }
                }
            }
            if(choice ==3){
                System.out.println(“***THANK YOU!BYE!***”);
                System.out.println(“********************”);
                return;
            }
        }
    }
}

Class Manager:
public class Manager {
    static int menu() {
        System.out.println(“*******************************************”);
        System.out.println(“***********WELCOME TO VIRTUAL SHOP*********”);
        System.out.println(“1. Admin Roles”);
        System.out.println(“2. Customer Roles”);
        System.out.println(“3. Exits”);
        System.out.print(“\n---------------\nEnter you choice:”);
        int choice = Validation.checkInputIntLimit(1, 3);
        return choice;
    }

    static int menuAdmin() {


        System.out.println(“***************\n -> Hello Boss\n”);
        System.out.println(“1. Add Items”);
        System.out.println(“2. Update Items”);
        System.out.println(“3. View Items”);
        System.out.println(“4. Remove Items”);
        System.out.println(“5. Exit”);
        System.out.print(“\n---------------\nEnter your choice: “);
        int choice = Validation.checkInputIntLimit(1, 5);
        return choice;
    }

    static int menuCustomer() {


        System.out.println(“***************\n ->Welcome lovely customer\n”);
        System.out.println(“1. Add to Shopping Basket”);
        System.out.println(“2. Display the Shopping Basket”);
        System.out.println(“3. Remove from Shopping Basket”);
        System.out.println(“4. Print Invoice(Orders)”);
        System.out.println(“5. Exit”);
        System.out.print(“\n---------------\nEnter your choice: “);
        int choice = Validation.checkInputIntLimit(1, 5);
        return choice;
    }
}

Class liItem:
interface liItem {
    double transportingFeeBook(double weight);
    double transportingFeeSoftware(int noCD);
}
Class Book:
public class Book extends ShopItem {
    private double weight;

    public Book() {
    }

    public Book(double weight) {


        this.weight = weight;
    }

    public Book(String id, String iName, double iPrice, int iQtt) {


        super(id, iName, iPrice, iQtt);
    }

    public Book(String id, String iName, double iPrice, int iQtt, double weight)
{
        super(id, iName, iPrice, iQtt);
        this.weight = weight;
    }

    public double getWeight() {


        return weight;
    }

    public void setWeight(double weight) {


        this.weight = weight;
    }

    @Override
    public String toString() {
        return “ Book’s title: “ + super.toString() + “, weight: “ +
(weight/1000) + “kg”;
    }

Class Software:
public class Software extends ShopItem{
    private int noCD;

    public Software() {
    }

    public Software(int noCD) {


        this.noCD = noCD;
    }

    public Software(String id, String iName, double iPrice, int iQtt, int noCD)
{
        super(id, iName, iPrice, iQtt);
        this.noCD = noCD;
    }

    public int getNoCD() {


        return noCD;
    }

    public void setNoCD(int noCD) {


        this.noCD = noCD;
    }

    @Override
    public String toString() {
        return “Software title:” + super.toString() + “, number of cd:” + noCD ;
    }
}

Class ShopItem:
public class ShopItem implements Cloneable,Comparable<ShopItem> {
    private String id;
    private String iName;
    private double iPrice;
    int iQtt;
    public ShopItem() {
    }
    public ShopItem(String id, String iName, double iPrice, int iQtt) {
        this.id = id;
        this.iName = iName;
        this.iPrice = iPrice;
        this.iQtt = iQtt;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getiName() {
        return iName;
    }
    public void setiName(String iName) {
        this.iName = iName;
    }
    public double getiPrice() {
        return iPrice;
    }
    public void setiPrice(double iPrice) {
        this.iPrice = iPrice;
    }
    public int getiQtt() {
        return iQtt;
    }
    public void setiQtt(int iQtt) {
        this.iQtt = iQtt;
    }

    public double total(){


        return 0;
    }
    @Override
    public int compareTo(ShopItem o){
        return this.id.compareTo(o.id);
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    @Override
    public String toString() {
        return  “’”+iName + “’, Price: $” + iPrice + “, quantity: “ + iQtt + “,
id: “ + id;
    }
   
}

Class Store:
import java.util.ArrayList;
import java.util.Collections;

public class Store{

    public Store() {
    }

    ArrayList<ShopItem> ls = new ArrayList<ShopItem>();


   
    public ArrayList<ShopItem> getLs() {
        return ls;
    }
    public void setLs(ArrayList<ShopItem> ls) {
        this.ls = ls;
    }
    public void addItem() {
        String id;
        String name;
        int quantity;
        double price;
        while (true) {
            System.out.println(“------------------");
            System.out.println(“1. Add book”);
            System.out.println(“2. Add software”);
            System.out.println(“3. Exit”);
            System.out.print(“------------------\nEnter your choice:”);
            int choice = Validation.checkInputIntLimit(1, 3);
            switch (choice) {
            case 1:
                System.out.println(“~~Add new Book:”);
                do {
                    System.out.print(“Enter id: “);
                    id = Validation.checkInputString();
                    if (Validation.checkIdExist(ls, id) == 1)
                        System.out.println(“The id enterd is exist, try again”);
                } while (Validation.checkIdExist(ls, id) == 1);
                System.out.print(“Enter name: “);
                name = Validation.checkInputString();
                System.out.print(“Enter quantity: “);
                quantity = Validation.checkInputInt();
                System.out.print(“Enter price($): “);
                price = Validation.checkInputDouble();
                System.out.print(“Enter book’s weight(grams): “);
                double weight = Validation.checkInputDouble();
                ShopItem book = new Book(id, name, price, quantity, weight);
                ls.add(book);
                System.out.println(“A new book is created successfully !”);
                break;
            case 2:
            System.out.println(“~~Add new Software:”);
                do {
                    System.out.print(“Enter id: “);
                    id = Validation.checkInputString();
                    if (Validation.checkIdExist(ls, id) == 1)
                        System.out.println(“The id enterd is exist, try again”);
                } while (Validation.checkIdExist(ls, id) == 1);
                System.out.print(“Enter name: “);
                name = Validation.checkInputString();
                System.out.print(“Enter quantity: “);
                quantity = Validation.checkInputInt();
                System.out.print(“Enter price($): “);
                price = Validation.checkInputDouble();
                System.out.print(“Enter software’s noCD:”);
                int noCD = Validation.checkInputInt();
                ShopItem software  = new Software(id, name, price, quantity,
noCD);
                ls.add(software);
                System.out.println(“A new software is created successfully”);
                break;
            case 3:
                System.out.println(“Exit !”);
                return;
            default:
                System.out.println(“out of range! Try again”);
                break;
            }
        }
    }
    public void displayListItems(){
        System.out.println(“*******************************”);
        System.out.println(“*******LE VUONG’S STORE********”);
        System.out.println(“–There are “+ls.size()+” items in shop –");
        Collections.sort(ls, new SortById());
        int I = 0;
        for (ShopItem shopItem : ls) {
            if (shopItem instanceof Book){
                Book b = (Book)shopItem;
                System.out.println(++I + “- “ +b);
            }else{
                Software s = (Software)shopItem;
                System.out.println(++I + “- “ +s);
            }
        }
        System.out.println(“________________________________________”);
    }
    public void updateItem(){
        System.out.println(“Enter Item you want to update: “);
        String id;
        do {
            System.out.print(“Enter id: “);
            id = Validation.checkInputString();
            if (Validation.checkIdExist(ls, id) == 0)
                System.out.println(“The id enterd does not exist, try again”);
            else{
                for (var shopItem : ls) {
                    if (id.equalsIgnoreCase(shopItem.getId())){
                        System.out.println(“Found the item:”);
                        System.out.println(shopItem);
                        System.out.print(“Want to update?(Y/N): “);
                        oolean k = Validation.checkInputYN();
                        if (k){
                            String 8oolean;
                            int quantity;
                            double newPrice;
                            System.out.print(“Enter new name: “);
                            8oolean = Validation.checkInputString();
                            System.out.print(“Enter new quantity: “);
                            quantity = Validation.checkInputInt();
                            System.out.print(“Enter new price($): “);
                            newPrice = Validation.checkInputDouble();
                            if (shopItem instanceof Book){
                                System.out.print(“Enter new weight(grams): “);
                                double weight = Validation.checkInputDouble();
                                Book book = new Book();
                                book = (Book) shopItem;
                                book.setWeight(weight);
                                book.setiName(8oolean);
                                book.setiPrice(newPrice);
                                book.setiQtt(quantity);
                                shopItem = (Book) book;
                                System.out.println(“Update success!”);
                            }else{
                                System.out.print(“Enter new noCD: “);
                                int noCD = Validation.checkInputInt();
                                Software software = new Software();
                                software = (Software) shopItem;
                                software.setiName(8oolean);
                                software.setiPrice(newPrice);
                                software.setiQtt(quantity);
                                software.setNoCD(noCD);
                                shopItem = (Software) software;
                                System.out.println(“Undate success!”);
                            }
                        }else{
                            return;
                        }
                    }
                }
            }
        } while (Validation.checkIdExist(ls, id) == 0);
    }
    public void removeItem(){
        if (ls.isEmpty()){
            System.out.println(“List if empty!”);
            return;
        }
        displayListItems();
        oolean k = false;
        oolean check;
        do {
            System.out.println(“---------------------");
            System.out.print(“Enter the index of Item you want to remove: “);
            int index = Validation.checkInputInt();
            for (int I = 0; I < ls.size(); i++) {
                if (i==index-1){
                    ls.remove(i);
                    System.out.println(“A item in store was removed!”);
                    k = true;
                }
            }
            if (!k) System.out.println(“The index out of range!\n”);
            System.out.print(“Do you want to continue?(Y/N): “);check =
Validation.checkInputYN();
        } while (!k||check);
    }
}

Class Basket:
import java.util.ArrayList;

public class Basket implements liItem {


    private ArrayList<ShopItem> selectedItems = new ArrayList<ShopItem>();
    Store itemList = new Store();
    ArrayList<ShopItem> iLs = new ArrayList<ShopItem>();
    public Basket() {
    }
    public Basket(Store itemList, ArrayList<ShopItem> iLs) {
        this.itemList = itemList;
        this.iLs = iLs;
    }
    public ArrayList<ShopItem> getSelectedItems() {
        return selectedItems;
    }

    public Basket(ArrayList<ShopItem> selectedItems) {


        this.selectedItems = selectedItems;
    }

    public Store getItemList() {


        return itemList;
    }

    public void setItemList(Store itemList) {


        this.itemList = itemList;
    }

    public ArrayList<ShopItem> getiLs() {


        return iLs;
    }

    public void setiLs(ArrayList<ShopItem> iLs) {


        this.iLs = iLs;
    }
    public double getTotalPrice() {
        double tot = 0;
        for (ShopItem shopItem : selectedItems) {
            tot += shopItem.getiPrice();
        }
        return tot;
    }

    @Override
    public double transportingFeeBook(double totWeight) {
        // TODO Auto-generated method stub
        int d = (int) (Math.round(totWeight)/1000);
        if (totWeight<500) return 5.00;
        else return (d*7.00+9.50);
    }
    @Override
    public double transportingFeeSoftware(int cd) {
        // TODO Auto-generated method stub
        if (cd<=3) return 3.25*cd;
        else return 1.5*cd+3.25*3;
    }

    public double transFeeTotalBook() {


        double w = 0;
        for (ShopItem shopItem : selectedItems) {
            if (shopItem instanceof Book) {
                Book book = (Book) shopItem;
                w = w + book.getWeight();
            }
        }
        return transportingFeeBook(w);
    }
    public double transFeeTotalSoftware(){
        int cd=0;
        for (ShopItem shopItem : selectedItems) {
            if (shopItem instanceof Software) {
                Software software = (Software) shopItem;
                cd = cd + software.getNoCD();
            }
        }
        return transportingFeeSoftware(cd);
    }

    public void printInvoice() {


        System.out.println(“\n\n**YOUR BILL**”);
        System.out.println(“Total price of selection: $” + getTotalPrice());
        double w = 0;
        int d = 0;
        for (ShopItem x : selectedItems) {
            if (x instanceof Book) {
                Book tmp = (Book) x;
                w += tmp.getWeight();
            } else {
                d += 1;
            }
        }
        System.out.println(“Books total weight: “ + (w/1000) + “kg, potage and
packing: $” + transFeeTotalBook());
        System.out.println(“Number of Software titles: “ + d + “, potage and
packing: $” + transFeeTotalSoftware());
        System.out.println(“Total basket cost (including postage and packing):
$”+(getTotalPrice()+transFeeTotalBook()+transFeeTotalSoftware()));
    }

    public void display() {


        System.out.println(“\n**********************”);
        System.out.println(“******Your Basket******”);
        int I = 0;
        for (ShopItem item : selectedItems) {
            if (item instanceof Book) {
                Book bk = (Book) item;
                System.out.println(++I + “- Book title: ‘” + item.getiName() +
“’, Price: $” + item.getiPrice()
                        + “, Weight: “ + bk.getWeight());
            } else {
                System.out.println(++I + “- Software title: ‘” + item.getiName()
+ “’, Price: $” + item.getiPrice());
            }
        }
    }

    public void addItem() {


        itemList.displayListItems();
        oolean k;
        oolean check;
        do {
            k = false;
            System.out.print(“\n----------------\nEnter the index of items you
want to buy:”);
            int index = Validation.checkInputInt();
            for (int I = 0; I < iLs.size(); i++) {
                if (I == index – 1) {
                    int quantityNow = iLs.get(i).getiQtt() – 1;
                    if (quantityNow<0) System.out.println(“Out of product! Try
others.”);
                    else{
                        selectedItems.add(iLs.get(i));
                        iLs.get(i).setiQtt(quantityNow);
                        System.out.println(“A item was added into your
basket!”);
                    }
                    k = true;
                }
            }
            if (!k)
                System.out.println(“The index of item should less than “ +
(iLs.size() + 1));
            System.out.print(“\nDo you want to continue? (Y/N):”);
            check = Validation.checkInputYN();
            if (!check) {
                System.out.println(“Ok, stop!”);
                return;
            }
        } while (!k || check);
    }

    public void removeBasket() {


        if (selectedItems.isEmpty()) {
            System.out.println(“Your basket is empty!”);
            return;
        }
        oolean k;
        oolean check;
        display();
        do {
            k = false;
            System.out.print(“\n----------------\nEnter the index of items you
want to remove:”);
            int index = Validation.checkInputInt();
            for (int I = 0; I < selectedItems.size(); i++) {
                if (I == (index – 1)) {
                    for (ShopItem x : iLs) {
                        if
(x.getId().equalsIgnoreCase(selectedItems.get(i).getId())) {
                            int quantityNow = x.getiQtt() + 1;
                            x.setiQtt(quantityNow);
                        }
                    }
                    selectedItems.remove(i);
                    System.out.println(“A item was removed from your basket!”);
                    k = true;
                }
            }
            if (!k)
                System.out.println(“The index of item should less than “ +
(iLs.size() + 1));
            System.out.print(“\nDo you want to continue? (Y/N):”);
            check = Validation.checkInputYN();
            if (!check) {
                System.out.println(“Ok, stop!”);
                return;
            }
        } while (!k || check);
    }
}

Class SortByID:
import java.util.Comparator;

public class SortById implements Comparator<ShopItem> {


    @Override
    public int compare(ShopItem o1, ShopItem o2) {
        return o1.getId().compareTo(o2.getId());
    }
}

Class Shop:
import java.util.ArrayList;

public class Shop {


    private String customerName;
    Store st = new Store();
    Basket cart = new Basket();
   
    public Shop() {
    }
   
    public Shop(Store st, Basket cart) {
        this.st = st;
        this.cart = cart;
    }

    public String getCustomerName() {


        return customerName;
    }

    public void setCustomerName(String customerName) {


        this.customerName = customerName;
    }
   
    public Shop(Store st) {
        this.st = st;
    }

    public Store getSt() {


        return st;
    }

    public void setSt(Store st) {


        this.st = st;
    }

    public Basket getCart() {


        return cart;
    }

    public void setCart(Basket cart) {


        this.cart = cart;
    }
    public void shopping(){
        if (st.getLs().isEmpty()){
            System.err.println(“----------------------\nNo item in
store !\n-----------------------");
            return;
        }
        System.out.print(“****Start Shopping**** \n\n->Enter your name:”);
        String name = Validation.checkInputString();
        this.customerName = name;
        System.out.println(“ Hey “+this.customerName +” welcome to Le Vuong’s
Shop! \n “);
        this.getCart().addItem();
    }

    public ArrayList<ShopItem> getCartSelectedItems() {


        return cart.getSelectedItems();
    }
}

Class Validation:
import java.util.ArrayList;
import java.util.Scanner;

public class Validation {


    public static Scanner sc = new Scanner(System.in);

    public static int checkInputIntLimit(int min, int max) {


        while (true) {
            try {
                int result = Integer.parseInt(sc.nextLine());
                if (result < min || result > max) {
                    throw new NumberFormatException();
                }
                return result;
            } catch (NumberFormatException ex) {
                System.err.println(“Re-input”);
            }
        }
    }

    public static String checkInputString() {


        while (true) {
            String result = sc.nextLine().trim();
            if (result.length() == 0) {
                System.err.print(“Not empty. Try again: “);
            } else {
                return result;
            }
        }
    }
    public static double checkInputDouble() {
        while (true) {
            try {
                double result = Double.parseDouble(sc.nextLine());
                return result;
            } catch (NumberFormatException e) {
                System.err.println(“Must be input double”);
                System.out.print(“Enter again: “);
            }

        }
    }
    public static int checkInputInt() {
        int data = 0;
        while (true) {
            try {
                data = Integer.parseInt(sc.nextLine());
                return data;
            } catch (Exception e) {
                // TODO: handle exception
                System.err.println(“Re-input”);
            }
        }
    }

    public static oolean checkInputYN() {


        while (true) {
            String result = sc.nextLine().trim();
            if (result.length() == 1 && result.equalsIgnoreCase(“Y”)) {
                return true;
            } else if (result.length() == 1 && result.equalsIgnoreCase(“N”)) {
                return false;
            }
            System.err.println(“Re-input.”);
        }
    }

    public static int checkIdExist(ArrayList<ShopItem> ls, String id) {


        for (ShopItem shopItem : ls) {
            if (id.equalsIgnoreCase(shopItem.getId())){
                return 1;
            }
        }
        return 0;
    }
}

~~Hết~~

You might also like