Advertisement
YST

MINI PROJECT(WATER ORDER) CSC248

YST
Sep 21st, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.13 KB | None | 0 0
  1. Apps
  2. import java.util.Scanner;
  3. import java.util.ArrayList;
  4. import java.io.*;
  5. import java.util.Stack;
  6. public class application {
  7.     public static void main(String[] args) {
  8.  
  9.         Stack <Double> profit=new Stack<Double>();
  10.  
  11.         double proCal=0.0;//profit calculation
  12.         int autoGenerator = 0;
  13.  
  14.         String username = " ", password = " ";
  15.         Scanner input = new Scanner(System.in);
  16.         System.out.print("USERNAME :");
  17.         username = input.next();
  18.         System.out.print("PASSWORD :");
  19.         password = input.next();
  20.  
  21.         //Arraylist for login
  22.         ArrayList<login> a = new ArrayList<login>();
  23.  
  24.         //adding object to a arraylist
  25.         a.add(new login("yunus", "yst","manager"));
  26.         a.add(new login("farina", "ina","manager"));
  27.         a.add(new login("iskandar", "is","manager"));
  28.         a.add(new login("fathul","press","staff"));
  29.         a.add(new login("iqbal","saurabi","staff"));
  30.         a.add(new login("adam","isyqi","staff"));
  31.         a.add(new login("aiman","muhammad","staff"));
  32.  
  33.         String noti = " ";
  34.         String status=" ";
  35.         String ans=" ";
  36.         //username and password will be search according to the index respectively
  37.         for (int counter = 0; counter < a.size(); counter++) {
  38.             if ((a.get(counter).getUsername().equalsIgnoreCase(username)) && (a.get(counter).getPassword().equalsIgnoreCase(password))) {
  39.  
  40.                 status=a.get(counter).getStatus();
  41.                 noti = "Sucessfully Login";
  42.                 System.out.println(noti + "\n");
  43.                 break;
  44.             }
  45.         }
  46.         String blocker=" ";
  47.         if(status.equals("manager"))
  48.             blocker = "allow";
  49.  
  50.         if (noti.equals("Sucessfully Login")) {
  51.  
  52.             do {
  53.                 String type;
  54.                 int size;
  55.                 double price=0.0;
  56.  
  57.                 Queue Cdetail = new Queue();
  58.  
  59.                 System.out.println("CUSTOMER NAME :");
  60.                 String name = input.next();
  61.                 String name2=input.nextLine();
  62.                 autoGenerator++;
  63.  
  64.  
  65.                 LinkedList item = new LinkedList();
  66.  
  67.                 item.insertAtBack(new item(1, "MINERAL WATER", "BIG     ", 20.00));
  68.                 item.insertAtBack(new item(2, "MINERAL WATER", "SMALL   ", 10.00));
  69.                 item.insertAtBack(new item(3, "DRINKING WATER", "BIG    ", 17.00));
  70.                 item.insertAtBack(new item(4, "DRINKING WATER", "SMALL  ", 7.00));
  71.  
  72.  
  73.                 int counter1 = 0;
  74.                 item data = (item) item.getFirst();
  75.                 while (data != null) {
  76.                     if (counter1 == 0) {
  77.  
  78.                         System.out.println("NO" + "   TYPE        " + "         SIZE " + "            PRICE ");
  79.                         counter1++;
  80.                     }
  81.                     System.out.println(data);
  82.                     data = (item) item.getNext();
  83.                 }
  84.  
  85.                 char ans3=' ';
  86.                 int choice=0,quant=0;
  87.                 double total=0.0;
  88.                 BSTree tree = new BSTree();
  89.                 do {
  90.                     System.out.println("CUSTOMER'S CHOICE :");
  91.                     choice = input.nextInt();
  92.                     System.out.println("QUANTITY : ");
  93.                     quant = input.nextInt();
  94.  
  95.                     if ((choice == 1)) {
  96.                         price = 20.00;
  97.                         tree.insertNode(new calculator(choice, price, quant));
  98.                     } else if ((choice == 2)) {
  99.                         price = 10.00;
  100.                         tree.insertNode(new calculator(choice, price, quant));
  101.                     } else if ((choice == 3)) {
  102.                         price = 17.00;
  103.                         tree.insertNode(new calculator(choice, price, quant));
  104.                     } else if ((choice == 4)) {
  105.                         price = 7.00;
  106.                         tree.insertNode(new calculator(choice, price, quant));
  107.                     }
  108.                     double total2=tree.totalPrice(price,quant);
  109.                     total=total2+total;
  110.                     Cdetail.enqueue(new customerDetails(choice,quant,price,total2));
  111.                     System.out.println("ANYTHING ELSE PRESS Y");
  112.                     String temp=input.next();
  113.                     ans3=temp.charAt(0);
  114.                 }while (ans3=='Y');
  115.                 double totalOverall=total;
  116.                 System.out.println("TOTAL PRICE : RM " + totalOverall+"0");
  117.                 //String tempT=" ",tempS=" ";
  118.                 try{
  119.                     BufferedWriter writer=new BufferedWriter(new FileWriter("receipt.txt"));
  120.                     writer.write("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~IFY H2O~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  121.                     writer.write("CASHIER     : "+"\t"+username+"\n");
  122.                     writer.write("CUSTOMER    : "+"\t"+ name+""+name2+"\n");
  123.                     writer.write("CUSTOMER ID : "+"\t"+autoGenerator+"\n");
  124.                     writer.write("item            SIZE       PRICE       QUANTITY     TOTAL PRICE\n");
  125.                     while(Cdetail.isEmpty()==false){
  126.                         customerDetails obj = (customerDetails) Cdetail.dequeue();
  127.                         if(obj.getChoice()==1)
  128.                             writer.write("MINERAL WATER"+"\t"+"BIG"+"\t       RM "+obj.getPrice()+"0\t      "+obj.getQuantity()+"\t         RM "+obj.getTotal()+"0\n");
  129.                         else if(obj.getChoice()==2)
  130.                             writer.write("MINERAL WATER"+"\t"+"SMALL"+"\t   RM "+obj.getPrice()+"0\t      "+obj.getQuantity()+"\t         RM "+obj.getTotal()+"0\n");
  131.                         else if(obj.getChoice()==3)
  132.                             writer.write("DRINKING WATER"+"\t"+"BIG"+"\t       RM "+obj.getPrice()+"0\t      "+obj.getQuantity()+"\t         RM "+obj.getTotal()+"0\n");
  133.                         else if(obj.getChoice()==4)
  134.                             writer.write("DRINKING WATER"+"\t"+"SMALL"+"\t   RM "+obj.getPrice()+"0\t      "+obj.getQuantity()+"\t         RM "+obj.getTotal()+"0\n");
  135.                     }
  136.                     writer.write("====================================================================\n");
  137.                     writer.write("OVERALL TOTAL PRICE RM "+totalOverall+"0\n");
  138.                     writer.write("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~THANK YOU~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  139.  
  140.                     writer.close();
  141.                 }
  142.                 catch (FileNotFoundException e){
  143.                     System.out.println("Problem "+e.getMessage());
  144.                 }
  145.                 catch (IOException ioe){
  146.                     System.out.println("Problem "+ioe.getMessage());
  147.                 }
  148.  
  149.                 profit.push(total);
  150.  
  151.  
  152.                 if((blocker.equals("allow"))&&(status.equals("manager"))){
  153.                     while (!profit.isEmpty())
  154.                         proCal=profit.pop()+proCal;
  155.                     System.out.println("ENTER Y TO DISPLAY YOUR PROFIT");
  156.                     String temp=input.next();
  157.                     char ans2=temp.charAt(0);
  158.                     if(ans2=='Y')
  159.                         System.out.println("YOUR PROFIT RM "+proCal+"0");
  160.  
  161.                 }
  162.                 System.out.println("Continue? press Y");
  163.                 ans=input.next();
  164.             } while (ans.equals("Y"));
  165.  
  166.             if(status.equals("staff")){
  167.                 System.out.println("IF YOU ARE MANAGER AND WANT TO DISPLAY PROFIT PRESS Y");
  168.                 String temp=input.next();
  169.                 char ans4=temp.charAt(0);
  170.                 if(ans4=='Y'){
  171.                     System.out.println("~~~SECUIRTY VERIFICATION~~~");
  172.                     System.out.println("ENTER MANAGER'S USERNAME :");
  173.                     String username1=input.next();
  174.                     System.out.println("ENTER MANAGER'S PASSWORD");
  175.                     String password1=input.next();
  176.  
  177.                     String status1=" ";
  178.                     for (int counter = 0; counter < a.size(); counter++) {
  179.                         if ((a.get(counter).getUsername().equalsIgnoreCase(username1)) && (a.get(counter).getPassword().equalsIgnoreCase(password1))) {
  180.                             status1=a.get(counter).getStatus();
  181.                             if(status1.equals("manager")){
  182.                                 while (!profit.isEmpty())
  183.                                     proCal=profit.pop()+proCal;
  184.                             }
  185.                             System.out.println("YOUR PROFIT RM "+proCal+"0");
  186.                             break;
  187.                         }
  188.                     }
  189.                 }
  190.             }
  191.  
  192.  
  193.         }
  194.     }
  195.  
  196.  
  197. }
  198. Log In
  199. public class login {
  200.     private String username;
  201.     private String password;
  202.     private String status;
  203.  
  204.     //default constructor
  205.     public login(){
  206.         username=" ";
  207.         password=" ";
  208.         status=" ";
  209.     }
  210.     //normal constructor
  211.     public login(String u,String p,String s){
  212.         username=u;
  213.         password=p;
  214.         status=s;
  215.     }
  216.     //getter
  217.     public String getUsername(){
  218.         return username;
  219.    }
  220.     public String getPassword(){
  221.        return password;
  222.     }
  223.     public String getStatus(){return status; }
  224. }
  225.  
  226. Item
  227.     public class ITEM {
  228.  
  229.     private int no;
  230.     private String type;
  231.     private String size;
  232.     private double price;
  233.  
  234.     //default constructor
  235.     public ITEM(){
  236.         no=0;
  237.         type=" ";
  238.        size=" ";
  239.         price=0.0;
  240.     }
  241.     //normal constructor
  242.     public ITEM (int n,String t,String s,double p){
  243.         no=n;
  244.         type = t;
  245.         size = s;
  246.         price = p;
  247.     }
  248.    //mutator
  249.     public void mutator(int N,String T,String S,double P){
  250.         no=N;
  251.         type=T;
  252.         size=S;
  253.         price=P;
  254.     }
  255.     //getter
  256.     public int getNo(){return no;}
  257.     public String getType(){
  258.         return type;
  259.     }
  260.     public String getSize(){
  261.         return size;
  262.     }
  263.     public double getPrice(){
  264.         return price;
  265.     }
  266.     //toString
  267.     public String toString(){
  268.             return no+"\t"+type + "\t      " + size + "\t       RM " + price+"0";
  269.     }
  270.  
  271.  
  272. }
  273.  
  274. Customer Detail
  275. public class CustomerDetails {
  276.     private int choice;
  277.     private int quantity;
  278.     private double price;
  279.     private double total;
  280.  
  281.  
  282.     //default constructor
  283.     public CustomerDetails(){
  284.         choice=0;
  285.         quantity=0;
  286.         price=0.0;
  287.         total=0.0;
  288.     }
  289.    //normal  constructor
  290.     public CustomerDetails(int C,int Q,double P,double T){
  291.         choice=C;
  292.         quantity=Q;
  293.         price=P;
  294.         total=T;
  295.     }
  296.     //mutator
  297.     public void setter(int c,int q,double p,double t){
  298.         choice=c;
  299.         quantity=q;
  300.         price=p;
  301.         total=t;
  302.     }
  303.     //getter
  304.     public int getChoice(){
  305.        return choice;
  306.     }
  307.     public int getQuantity(){
  308.         return quantity;
  309.     }
  310.     public double getPrice(){
  311.         return price;
  312.     }
  313.     public double getTotal(){
  314.         return total;
  315.     }
  316.     public String toString(){
  317.         return choice +"\t"+ quantity+"\t"+price+"\t"+total;
  318.     }
  319. }
  320.  
  321. Calculator
  322. public class Calculator {
  323.     private int choice;
  324.     private double price;
  325.     private int quantity;
  326.  
  327.     //default constructor
  328.     public Calculator(){
  329.         choice=0;
  330.         price=0.0;
  331.         quantity=0;
  332.     }
  333.     //normal constructor
  334.     public Calculator(int choice,double p,int q){
  335.         price=p;
  336.         quantity=q;
  337.     }
  338.  
  339.     //mutator
  340.     public void Calculator(int c,double P,int Q){
  341.         choice=c;
  342.         price=P;
  343.         quantity=Q;
  344.     }
  345.  
  346.     //getter
  347.     public int getChoice(){
  348.         return choice;
  349.     }
  350.     public double getPrice(){
  351.         return price;
  352.     }
  353.     public int getQuantity(){
  354.         return quantity;
  355.     }
  356. }
  357.  
  358. Linked List
  359. public class LinkedList {
  360.     public Node first = null;
  361.     public Node current = null;
  362.     public Node last = null;
  363.  
  364.     public LinkedList() {
  365.     }
  366.  
  367.     public void insertAtBack(Object item) {
  368.        Node newNode = new Node(item);
  369.        if(this.first == null) {
  370.             this.first = newNode;
  371.             this.last = newNode;
  372.         } else {
  373.            this.last.next = newNode;
  374.             this.last = newNode;
  375.         }
  376.  
  377.     }
  378.  
  379.     public Object removeFromFront() {
  380.         Object removeData = null;
  381.         if(this.first == null) {
  382.             return removeData;
  383.         } else {
  384.             removeData = this.first.data;
  385.             if(this.first == this.last) {
  386.                 this.first = null;
  387.                 this.last = null;
  388.             } else {
  389.                 this.first = this.first.next;
  390.             }
  391.  
  392.             return removeData;
  393.         }
  394.     }
  395.  
  396.     public Object removeFromBack() {
  397.         Object removeData = null;
  398.         if(this.first == null) {
  399.             return removeData;
  400.         } else {
  401.             removeData = this.last.data;
  402.             if(this.first == this.last) {
  403.                 this.first = null;
  404.                 this.last = null;
  405.             } else {
  406.                 for(this.current = this.first; this.current.next != this.last; this.current = this.current.next) {
  407.                     ;
  408.                 }
  409.  
  410.                 this.last = this.current;
  411.                 this.last.next = null;
  412.             }
  413.  
  414.             return removeData;
  415.         }
  416.     }
  417.  
  418.     public Object getFirst() {
  419.         if(this.first == null) {
  420.             return null;
  421.         } else {
  422.             this.current = this.first;
  423.             return this.current.data;
  424.         }
  425.     }
  426.     public Object getNext() {
  427.         if (current == last) {
  428.             return null;
  429.         } else {
  430.             current = current.next;
  431.             return current.data;
  432.        }
  433.     }
  434. }
  435.  
  436. Queue
  437. public class Queue extends LinkedList {
  438.     public Queue() {
  439.     }
  440.  
  441.     public void enqueue(Object elem) {
  442.         this.insertAtBack(elem);
  443.     }
  444.  
  445.     public Object dequeue() {
  446.         return this.removeFromFront();
  447.     }
  448.  
  449.     public boolean isEmpty() {
  450.         return this.getFirst() == null;
  451.     }
  452. }
  453.  
  454. Node
  455. public class Node {
  456.     Object data;
  457.     Node next;
  458.  
  459.     public Node(Object obj) {
  460.         this.data = obj;
  461.     }
  462. }
  463.  
  464. Tree Node
  465. public class TreeNode
  466. {
  467.     TreeNode left;
  468.     TreeNode right;
  469.     Calculator data;
  470.  
  471.     int temp=0;
  472.  
  473.     //normal constructor to initialize data to d and make this a leaf node
  474.     public TreeNode(Calculator d)
  475.     {
  476.         left=right=null;
  477.         data = d;
  478.     }
  479.  
  480.     // Insert a TreeNode into a Tree that contains nodes. Ignore duplicate values.
  481.     public void insert( Calculator d )
  482.     {
  483.         if ( d.getChoice() < temp )
  484.         {
  485.             if ( left == null )
  486.                 left = new TreeNode( d );
  487.             else
  488.                 left.insert( d );
  489.         }
  490.  
  491.         else
  492.         if ( d.getChoice() > temp)
  493.         {
  494.             if ( right == null )
  495.                 right = new TreeNode( d );
  496.             else
  497.                 right.insert( d );
  498.         }
  499.         //to make leaf well defined
  500.         temp=d.getChoice();
  501.     }
  502. }
  503.  
  504. BS Tree
  505. public class BSTree
  506. {
  507.     TreeNode root;
  508.  
  509.     // Construct an empty Tree
  510.     public BSTree()
  511.     { root = null; }
  512.  
  513.     // Insert a new node in the binary search tree.
  514.     // If the root node is null, create the root node here.
  515.     // Otherwise, call the insert method of class TreeNode.
  516.     public void insertNode( Calculator d )
  517.     {
  518.         if ( root == null )
  519.             root = new TreeNode( d );
  520.         else
  521.             root.insert( d );
  522.     }
  523.  
  524.     // Preorder Traversal
  525.     public void PreOrder()
  526.     {
  527.         RecurPreOrder( root );
  528.     }
  529.  
  530.     // Recursive method to perform preorder traversal
  531.     private void RecurPreOrder( TreeNode node )
  532.     {
  533.         if ( node == null )
  534.             return;
  535.  
  536.         System.out.print( node.data + " " );
  537.         RecurPreOrder( node.left );
  538.         RecurPreOrder( node.right );
  539.     }
  540.  
  541.     // Inorder Traversal
  542.     public void InOrder()
  543.     {
  544.         RecurInOrder( root );
  545.     }
  546.  
  547.     // Recursive method to perform inorder traversal
  548.     private void RecurInOrder( TreeNode node )
  549.     {
  550.         if ( node == null )
  551.             return;
  552.  
  553.         RecurInOrder( node.left );
  554.         System.out.print( node.data + " " );
  555.         RecurInOrder( node.right );
  556.     }
  557.  
  558.     // Postorder Traversal
  559.     public void PostOrder()
  560.     {
  561.         RecurPostOrder( root );
  562.     }
  563.  
  564.     // Recursive method to perform postorder traversal
  565.     private void RecurPostOrder( TreeNode node )
  566.     {
  567.         if ( node == null )
  568.             return;
  569.  
  570.         RecurPostOrder( node.left );
  571.         RecurPostOrder( node.right );
  572.         System.out.print( node.data + " " );
  573.     }
  574.    
  575.     //user defined
  576.    public double totalPrice ( double price, int quantity)
  577.    {
  578.     return totPrice( root, price, quantity);
  579.     }
  580.    
  581.     private double totPrice( TreeNode node, double price, int quantity )
  582.     {
  583.      if ( node == null) {
  584.           return 0.0;
  585.       }
  586.  
  587.      double total=0.0;
  588.          
  589.         total = price * quantity;
  590.         return total + totPrice ( node.left, price, quantity ) +totPrice(node.right,price,quantity);
  591.     }
  592. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement