Online-Academy

Look, Read, Understand, Apply

Menu

OOAD - Object Oriented Analysis and Design

Creating sales Object

import java.util.*;
class Product{
    int pid, price;
    String name;
    public void set(int pid,String name,int price){
        this.pid = pid;
        this.price = price;
        this.name = name;
    }
    public int getId(){
        return pid;
    }
    public int getPrice(){
        return price;
    }
    public String getName(){
        return name;
    }
}
class SalesLineItem{
    Product p;
    int qty;
    int subtotal;
    public void setLineItem(Product p,int qty){
        this.p = p;
        this.qty = qty;
    }
    public Product getProduct(){
        return p;
    }
    public int getQty(){
        return qty;
    }
    public int getSubtotal(){
        this.subtotal = p.getPrice() * qty;
        return subtotal;
    }
}
class Sales{
    Product[] p;
    int i;
    SalesLineItem [] sli;
    int total =0;
    public Sales(){
        p = new Product[5];
        for(i=0;i<5;i++){
            p[i] = new Product();
        }
        p[0].set(1,"A",22);
        p[1].set(2,"B",12);
        p[2].set(3,"C",27);
        p[3].set(4,"D",23);
        p[4].set(5,"E",82);
        sli = new SalesLineItem[10];
        for(i=0;i<10;i++){
            sli[i] = new SalesLineItem();
        }
        Scanner sc = new Scanner(System.in);
        int j=0;
        
        while(true){
            System.out.print("Item ID: (1,2,3,4 or 5)");
            int id = sc.nextInt();
            if(id==9)break; 
            System.out.print("Quantity: ");
            int qty = sc.nextInt();
            for(i=0;i<p.length;i++){
                if(id == p[i].getId()){
                    sli[j].setLineItem(p[i], qty);
                    break; 
                }
            }
            ++j;
        }
        System.out.println("Sno  Product  Qty  Price  STotal");
        for(i=0;i<j;i++){
            System.out.println((i+1)+"       "+sli[i].getProduct().getName()+"     "+sli[i].getQty()+
            "     "+sli[i].getProduct().getPrice()+"    "+sli[i].getSubtotal());
            total += sli[i].getSubtotal();
        }
        System.out.println("Total: "+total);
    }
}
class Sales_bill{
    public static void main(String[] op){
        Sales s = new Sales();
    }
}