Online Shopping Cart (E-commerce System)

AIRLINE TICKETING SYSTEM

§ Business Requirements

Develop a online E-commerce system capable of below, 
a) Customer Registration
b) Customer Maintenance 
c) Authentication
d) Purchase Item
e) Online payment and confirmation


§  Domain Model
§  Requirements Analysis

1.    Ticket operator can enter total number of sectors to be processed

2.    System will display list of sectors,

1: Tokyo -> New York, 2: New York -> Tokyo, 3: Tokyo -> London, 4: London -> Tokyo

3.    System will prepare invoice and print it. The invoice includes all the tickets information along with total price and discount. The tickets includes selected sector information along with individual price and discount.

4.    File specification for campaign file : campaign.txt
ID,FROM,TO,PRICE,DISCOUNT

Sample data,

1,TOKYO,NEW YORK,30000,15
2,NEW YORK,TOKYO,50000,10
3,TOKYO,LONDON,80000,15
4,LONDON,TOKYO,80000,5

§  Scenarios

# 1: Processing of 3 sectors






















# 2: Processing of 4 different sectors


# 3: Processing 2 sectors




















§  Source


import model.Invoice;
import model.Ticket;
import controller.DataHandler;
import controller.InvoiceManager;


public class AIRApp {
    static DataHandler dataHandler = new DataHandler();
    
    /**
     * @param args
     */
    public static void main(String[] args) {
    System.out.println("\n########## HANABI TRAVELS AIRLINE TICKETING ##########\n");
        //1. Show choices to operator
        Invoice invoice = InvoiceManager.createInvoice();
        
        System.out.println("\n************** INVOICE (" + invoice.getTickets()[0].getPrice().getCurrency() + ") *******");
        System.out.println("Sectors [From -> To] ");

        for (Ticket ticket : invoice.getTickets()) {
        System.out.println(ticket);
        }
        
        System.out.println("-------------------------------------");
        
        System.out.println("TOTAL PRICE   [A]\t" + invoice.getTotalPrice() );
        System.out.println("TOTAL DISCOUNT[B]\t" + invoice.getTotalDiscount());
        
        double discountedPrice = invoice.getTotalPrice() - invoice.getTotalDiscount();
        System.out.println("FINAL PRICE [A-B]\t" + discountedPrice);
        System.out.println("*************************************");
        
        System.out.println("-->THANK YOU FOR USING HANABI TRAVELS !!!");
    }

}
-----------------------------------------------------------------------
package controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;

import model.Campaign;
import model.Price;
import model.Sector;

public class DataHandler {
    
    public static HashMap<String, Campaign> loadFile() {
        
        HashMap<String, Campaign> campaignMap = new HashMap<String, Campaign>();
        
        //read file and load data into HashMap
        File file = new File("campaign.txt");
        try {
            Scanner scanner = new Scanner(file);
            while(scanner.hasNext()) {
                String line = scanner.nextLine();
                //1,tok,ny,30000,15
                String[] cells = line.split(",");
                Campaign campaign = new Campaign();
                campaign.setId(cells[0]);
                
                //Sector
                Sector sector = new Sector();
                sector.setFrom(cells[1]);
                sector.setTo(cells[2]);
                campaign.setSector(sector);
                
                //Price
                Price price = new Price();
                price.setAmount(Double.parseDouble(cells[3]));
                price.setCurrency("JPY");
                
                campaign.setPrice(price);
                campaign.setDiscountPercentage(Double.parseDouble(cells[4]));
                
                //campaign.set
                //KEY = Cells[0] = 1
                //VALUE = line = 1,tok,ny,30000,15
                
                campaignMap.put(cells[0], campaign); 
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return campaignMap;
    }
}
-----------------------------------------------------------------------
package controller;

import java.util.Scanner;

import model.Invoice;
import model.Ticket;

public class InvoiceManager {
    
public static Invoice createInvoice() {
double totalPrice = 0;
double totalDiscount = 0;
Scanner scanner = new Scanner(System.in);

System.out.println("Enter number of sectors : ");
int SECTOR_COUNT = scanner.nextInt();
Ticket[] tickets = new Ticket[SECTOR_COUNT];
int ticketCount = 0;
System.out.println("\nSelect from below SECTORS \n");

System.out.println("----------------------------------------------------------------------------------");
System.out.println("1: Tokyo -> New York, 2: New York -> Tokyo, 3: Tokyo -> London, 4: London -> Tokyo");
int choice = 0;

for (int i = 0; i < SECTOR_COUNT; i++) {
System.out.println("Sector # : ");
choice = scanner.nextInt();

Ticket ticket = TicketManager.createTicket(choice);
tickets[ticketCount] = ticket;
totalPrice = totalPrice + ticket.getPrice().getAmount();
totalDiscount = totalDiscount + ticket.getDiscount();
ticketCount++;
Invoice invoice = new Invoice();
invoice.setTickets(tickets);
invoice.setTotalPrice(totalPrice);
invoice.setTotalDiscount(totalDiscount);
return invoice;
}


}
-----------------------------------------------------------------------
package controller;

import java.util.HashMap;

import model.Campaign;
import model.Ticket;

public class TicketManager {
    
static HashMap<String, Campaign> campaignMap = null;
    
    public static double calculateDiscount(Ticket tkt, Campaign campaign) {
    //Sector
    //Price
    //Discount
        
        //local variable 
        double discount = 0;
        double amount = tkt.getPrice().getAmount();
        double perDiscount = campaign.getDiscountPercentage() / 100.0;
        discount =  amount * perDiscount;
        
        return discount;
    }
    

public static Ticket createTicket(int choice) {
      //Load the campaign file

       if (campaignMap == null) {
       campaignMap = DataHandler.loadFile();
       }
  
        Campaign campaign = campaignMap.get(String.valueOf(choice));
Ticket ticket = new Ticket();
ticket.setSector(campaign.getSector());
System.out.println("SELECTED : " + campaign.getSector());
ticket.setPrice(campaign.getPrice());
double discountAmount = TicketManager.calculateDiscount(ticket, campaign);//campaign.getPrice().getAmount() * campaign.getDiscountPercentage() / 100;
ticket.setDiscount(discountAmount);
return ticket;
}


}
-----------------------------------------------------------------------
package model;

public class Campaign {
private String id;
private Sector sector;
private Price price;
private double discountPercentage;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Sector getSector() {
return sector;
}
public void setSector(Sector sector) {
this.sector = sector;
}
public Price getPrice() {
return price;
}
public void setPrice(Price price) {
this.price = price;
}
public double getDiscountPercentage() {
return discountPercentage;
}
public void setDiscountPercentage(double discountPercentage) {
this.discountPercentage = discountPercentage;
}

}
-----------------------------------------------------------------------
package model;

public class Invoice {
    
    private Ticket[] tickets;
    private double totalPrice;
    private double totalDiscount;
    
public Ticket[] getTickets() {
return tickets;
}
public void setTickets(Ticket[] tickets) {
this.tickets = tickets;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public double getTotalDiscount() {
return totalDiscount;
}
public void setTotalDiscount(double totalDiscount) {
this.totalDiscount = totalDiscount;
}
}
-----------------------------------------------------------------------
package model;

public class Price {
    private double amount;
    private String currency;
    
    public double getAmount() {
        return amount;
    }
    public void setAmount(double amount) {
        this.amount = amount;
    }
    public String getCurrency() {
        return currency;
    }
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    
    public String toString() {
    return 
    "PRICE : " + amount + " " +
    currency
   
    ;
    }
}
-----------------------------------------------------------------------
package model;

public class Sector {
    
    private String from;
    private String to;
    
    public String getFrom() {
        return from;
    }
    public void setFrom(String from) {
        this.from = from;
    }
    public String getTo() {
        return to;
    }
    public void setTo(String to) {
        this.to = to;
    }
    
    public String toString() {
    return 
    from + " -> " +
    to
    ;
    }

}
-----------------------------------------------------------------------
package model;

public class Ticket {
    
    private Sector sector;
    private double discount
    private Price price;
    
    public Sector getSector() {
        return sector;
    }
    public void setSector(Sector sector) {
        this.sector = sector;
    }
    public double getDiscount() {
        return discount;
    }
    public void setDiscount(double discount) {
        this.discount = discount;
    }
    public Price getPrice() {
        return price;
    }
    public void setPrice(Price price) {
        this.price = price;
    }
    
    public String toString() {
    return 
    sector + ", " +
    price + ", " +
    " DISCOUNT : " +
    discount
    ;
    }
}
----------------------------------------------------------------------- campaign.txt
1,TOKYO,NEW YORK,30000,15
2,NEW YORK,TOKYO,50000,10
3,TOKYO,LONDON,80000,15
4,LONDON,TOKYO,80000,5

The java main() method and an Object creation process (#4)


----------Object creation by JVM-----------

Description:

The object car1 is an instance of Car class created by JVM.As shown below the new keyword is use to create car1 object from the Car class.

Car car1 = new Car();

Car class Reference -> http://namastejava.blogspot.jp/2015/01/java-source-translation-to-class-file-3.html

Each object is isolated from each other and maintains their own state (value for each property). e.g. car1 object’s price = 1000000, where as car2 object’s price = 2000.

-----------Source [CarApp.java]------------
public class CarApp {

public static void main(String[] args) {
//car1 instance
Car car1 = new Car();
car1.setPrice(1000000);
car1.setCurrency("JPY");
double p1 = car1.getPrice();
String c1 = car1.getCurrency();
System.out.println("Price1 = " + p1);
System.out.println("Currency1 = " + c1);
//car2 instance
Car car2 = new Car();
car2.setPrice(2000);
car2.setCurrency("USD");

//car3 instance
Car car3 = new Car();
car3.setPrice(5000);
car3.setCurrency("EUR");
}

}

Description:

The main() method
  •  is an entry point to the Application
  •  is a special method, which is already known to JVM
  •  should exist for each Java application
  •  is static, hence JVM can access it without Instance of the target class
  •  returns void
  •  takes parameters of type String array, these are use to pass values at the time of application start up.

Java source translation to the Class File (#3)

-----------1. Create “Source File” [Car.java] ---------

public class Car {

private double price;
private String currency;
public String getCurrency() {
return currency;
}
public void setCurrency(String c) {
currency = c;
}
public double getPrice() {
return price;
}
public void setPrice(double p) {
price = p;
}
}

Description:
Use “Eclipse” or any other editor to create source file, On Unix like OS “vi editor can be used.


-----------2. Do source [Car.java] compilation-------

On Windows: C:/>javac Car.java
On Unix like:  $javac Car.java


Description:
Compile the “Source File” to translate it into the “Class file” (byte code). Use “javac” command to compile the source. 

Syntax: javac <SOURCE_FILE> 


-----------3. Check for the class file [Car.class]-----

Car.class

Description:
Java compiler translate Car.java source file into Car.class. This class file will be use by JVM to create instance of class as necessary.