Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Basic Java Bank Teller Help [Huge Post]

    Basically this is a pretty simple program that is supposed to simulate a bank with customers and a circular queue line. While my program "works" it doesn't process the events correctly, the circular queue has a mind of its own and I think my compareTo in events must be off. Also right now they are supposed to wait in line till a teller is free and there are supposed to be 3 of them, not sure how to begin implementing this. Any help and/or feedback would be appreciated greatly! Sorry for the ridiculous amount of information, just figured I'd hopefully give you guys everything you needed.

    Code:
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class Main{
      public static void main(String[] args) throws FileNotFoundException{
        int time=0;
        Scanner in = new Scanner(new FileReader("lab5data.txt"));
        
        Customer a,b,c;
        OrderedList events = new OrderedList(500);
        Queue line = new Queue(500);
        int i=0;
        while (in.hasNext()){
          
          
          i++;
          Customer cust = new Customer(i,in.nextInt(),in.nextInt());
          Event arrival = new Event('A',time,cust);
          time = cust.getArrivalTime();
          events.insert(arrival);
          
          while (!events.isEmpty()){
            Event current = events.give();
            
            
            if (current.getEventType() == 'A'){
              System.out.println("Customer " + current.getCustomer().getCustomer_ID() + " has arrived at "+ current.getCustomer().getArrivalTime());
              boolean atFront = line.isEmpty();
              line.enqueue(current.getCustomer());
             
              
              if (atFront){
                Event dep = new Event('D',((current.getCustomer()).getTransactionTime()+ time),current.getCustomer());
                events.insert(dep);
                
              }
              
              if (!atFront){
                cust = new Customer(i,in.nextInt(),in.nextInt());
                arrival = new Event('A',time,cust);
                events.insert(arrival);
                
              }
            }
           else if (current.getEventType() == 'D'){
              line.dequeue();
              System.out.println("Customer " +current.getCustomer().getCustomer_ID()+ " leaves at " + current.getEventTime());
              events.delete(1);
              events.delete(1);
              
              
              if(!line.isEmpty()){
                current = events.give();
                Event dep = new Event('D',((current.getCustomer()).getTransactionTime()+ time),current.getCustomer());
                events.insert(dep);
                
              }          
            }
            
          }
        }
      }
    }
    Code:
    public class Queue{
      private Customer[] elements;
      private int currentSize;
      private int maxSize;
      private int front=0, end=0;
      
      
      public Queue(int size){
        elements = new Customer[size];
        maxSize = size;
      }
      public boolean isEmpty(){
        return currentSize == 0;
      }
      
      public boolean isFull(){
        return currentSize==maxSize;
      }
      
      public void enqueue(Customer in){
        elements[end] = in;
        end++;
        if (end == maxSize)
          end=0;
      }
      
      public Customer dequeue(){
        Customer ans = elements[front];    
        front++;
        return ans;
      }
    }
    Code:
      public class Event{
      private char eventType;
      private int eventTime;
      private Customer theCustomer;
      
      public Event(char type, int time, Customer in){
        eventType = type;
        eventTime = time;
        theCustomer = in;
      }
      
      public char getEventType(){
        return eventType;
      }
      
      public int getEventTime(){
        return eventTime;
      }
      
      public Customer getCustomer(){
        return theCustomer;
      }
      
      public int compareTo(Event in){
        if (this.getEventTime() > in.getEventTime())
          return 1;
        else if (this.getEventTime() < in.getEventTime())
          return -1;
        else
          return 0;
      }
     }
    Code:
    public class Customer{
      
      
      private int customer_ID, arrivalTime, departureTime, transactionTime;
      
      
      
      public Customer(int id, int arrival, int trans){
        customer_ID = id;
        arrivalTime = arrival;
        transactionTime = trans;
        departureTime = arrival + trans;
        
      }
      
      public int getArrivalTime(){
        return arrivalTime;
      }
      
      public int getTransactionTime(){
        return transactionTime;
      }
      
      public int getCustomer_ID(){
        return customer_ID;
      }
       
      public int getDepartureTime(){
        return departureTime;
      }
      
      public void setDepartureTime(int time){
        departureTime = time;
      }
      
      public int calculateTurnAround(){
        return departureTime-arrivalTime;
      }
      
     
    }
    With the following output:
    Customer 1 has arrived at 1
    Customer 1 leaves at 6
    Customer 2 has arrived at 2
    Customer 2 leaves at 7
    Customer 3 has arrived at 3
    Customer 3 leaves at 9
    Customer 4 has arrived at 4
    Customer 4 leaves at 11
    Customer 5 has arrived at 5
    Customer 5 leaves at 7
    Customer 6 has arrived at 6
    Customer 6 leaves at 16
    Customer 7 has arrived at 7
    Customer 7 leaves at 10
    Customer 8 has arrived at 8
    Customer 8 leaves at 13
    Customer 9 has arrived at 9
    Customer 9 leaves at 17
    Customer 10 has arrived at 10
    Customer 10 leaves at 12
    Customer 11 has arrived at 11
    Customer 11 leaves at 13
    Customer 12 has arrived at 12
    Customer 12 leaves at 17
    Customer 13 has arrived at 13
    Customer 13 leaves at 18
    Customer 14 has arrived at 14
    Customer 14 leaves at 19
    Customer 15 has arrived at 15
    Customer 15 leaves at 20
    Customer 16 has arrived at 16
    Customer 16 leaves at 22
    Customer 17 has arrived at 17
    Customer 17 leaves at 24
    Customer 18 has arrived at 18
    Customer 18 leaves at 20
    Customer 19 has arrived at 25
    Customer 19 leaves at 35
    Customer 20 has arrived at 31
    Customer 20 leaves at 34
    Customer 21 has arrived at 36
    Customer 21 leaves at 41
    Customer 22 has arrived at 37
    Customer 22 leaves at 45
    Customer 23 has arrived at 38
    Customer 23 leaves at 40
    Customer 24 has arrived at 39
    Customer 24 leaves at 41
    Customer 25 has arrived at 40
    Customer 25 leaves at 45
    Customer 26 has arrived at 45
    Customer 26 leaves at 50
    Customer 27 has arrived at 50
    Customer 27 leaves at 53
    Customer 28 has arrived at 55
    Customer 28 leaves at 60
    Customer 29 has arrived at 60
    Customer 29 leaves at 66
    Customer 30 has arrived at 65
    Customer 30 leaves at 72
    Customer 31 has arrived at 70
    Customer 31 leaves at 72
    Customer 32 has arrived at 75
    Customer 32 leaves at 85
    Customer 33 has arrived at 80
    Customer 33 leaves at 83
    Customer 34 has arrived at 85
    Customer 34 leaves at 90
    Customer 35 has arrived at 90
    Customer 35 leaves at 98
    Customer 36 has arrived at 95
    Customer 36 leaves at 97
    Customer 37 has arrived at 100
    Customer 37 leaves at 102
    Customer 38 has arrived at 105
    Customer 38 leaves at 110
    Customer 39 has arrived at 110
    Customer 39 leaves at 115
    Customer 40 has arrived at 115
    Customer 40 leaves at 120
    Customer 41 has arrived at 120
    Customer 41 leaves at 125
    Customer 42 has arrived at 125
    Customer 42 leaves at 131
    Customer 43 has arrived at 130
    Customer 43 leaves at 137
    Customer 44 has arrived at 135
    Customer 44 leaves at 137
    Customer 45 has arrived at 140
    Customer 45 leaves at 143
    Customer 46 has arrived at 145
    Customer 46 leaves at 148
    Customer 47 has arrived at 150
    Customer 47 leaves at 155
    Customer 48 has arrived at 155
    Customer 48 leaves at 163
    Customer 49 has arrived at 160
    Customer 49 leaves at 163
    Customer 50 has arrived at 165
    Customer 50 leaves at 167
    Customer 51 has arrived at 170
    Customer 51 leaves at 175
    Customer 52 has arrived at 175
    Customer 52 leaves at 180
    Customer 53 has arrived at 180
    Customer 53 leaves at 185
    Customer 54 has arrived at 185
    Customer 54 leaves at 190
    Customer 55 has arrived at 190
    Customer 55 leaves at 196
    Customer 56 has arrived at 195
    Customer 56 leaves at 202
    Customer 57 has arrived at 200
    Customer 57 leaves at 202
    Customer 58 has arrived at 205
    Customer 58 leaves at 215
    Customer 59 has arrived at 210
    Customer 59 leaves at 213
    Customer 60 has arrived at 215
    Customer 60 leaves at 220
    Customer 61 has arrived at 220
    Customer 61 leaves at 228
    Customer 62 has arrived at 225
    Customer 62 leaves at 227
    Customer 63 has arrived at 230
    Customer 63 leaves at 232
    Customer 64 has arrived at 235
    Customer 64 leaves at 238
    Customer 65 has arrived at 240
    Customer 65 leaves at 245
    Customer 66 has arrived at 245
    Customer 66 leaves at 250
    Customer 67 has arrived at 250
    Customer 67 leaves at 255
    Customer 68 has arrived at 255
    Customer 68 leaves at 261
    Customer 69 has arrived at 260
    Customer 69 leaves at 267
    Customer 70 has arrived at 265
    Customer 70 leaves at 267
    Customer 71 has arrived at 270
    Customer 71 leaves at 280
    Customer 72 has arrived at 275
    Customer 72 leaves at 278
    Customer 73 has arrived at 280
    Customer 73 leaves at 285
    Customer 74 has arrived at 285
    Customer 74 leaves at 293
    Customer 75 has arrived at 290
    Customer 75 leaves at 292
    Customer 76 has arrived at 295
    Customer 76 leaves at 297
    Customer 77 has arrived at 300
    Customer 77 leaves at 305
    Customer 78 has arrived at 305
    Customer 78 leaves at 310
    Customer 79 has arrived at 310
    Customer 79 leaves at 315
    Customer 80 has arrived at 315
    Customer 80 leaves at 320
    Customer 81 has arrived at 320
    Customer 81 leaves at 326
    Customer 82 has arrived at 325
    Customer 82 leaves at 332
    Customer 83 has arrived at 330
    Customer 83 leaves at 332
    Customer 84 has arrived at 335
    Customer 84 leaves at 345
    Customer 85 has arrived at 340
    Customer 85 leaves at 343
    Customer 86 has arrived at 345
    Customer 86 leaves at 350
    Customer 87 has arrived at 350
    Customer 87 leaves at 358
    Customer 88 has arrived at 355
    Customer 88 leaves at 357
    Customer 89 has arrived at 360
    Customer 89 leaves at 362
    Customer 90 has arrived at 365
    Customer 90 leaves at 370
    Customer 91 has arrived at 370
    Customer 91 leaves at 375
    Customer 92 has arrived at 375
    Customer 92 leaves at 378
    Customer 93 has arrived at 380
    Customer 93 leaves at 385
    Customer 94 has arrived at 385
    Customer 94 leaves at 391
    Customer 95 has arrived at 390
    Customer 95 leaves at 397
    Customer 96 has arrived at 395
    Customer 96 leaves at 397
    Customer 97 has arrived at 400
    Customer 97 leaves at 410
    Customer 98 has arrived at 405
    Customer 98 leaves at 408
    Customer 99 has arrived at 410
    Customer 99 leaves at 415
    Customer 100 has arrived at 415
    Customer 100 leaves at 423
    Customer 101 has arrived at 420
    Customer 101 leaves at 422
    Customer 102 has arrived at 425
    Customer 102 leaves at 427
    Customer 103 has arrived at 430
    Customer 103 leaves at 435
    Customer 104 has arrived at 435
    Customer 104 leaves at 440
    Customer 105 has arrived at 440
    Customer 105 leaves at 445
    Customer 106 has arrived at 445
    Customer 106 leaves at 450
    Customer 107 has arrived at 450
    Customer 107 leaves at 456
    Customer 108 has arrived at 455
    Customer 108 leaves at 462
    Customer 109 has arrived at 460
    Customer 109 leaves at 462
    Customer 110 has arrived at 465
    Customer 110 leaves at 475
    Customer 111 has arrived at 470
    Customer 111 leaves at 473
    Customer 112 has arrived at 471
    Customer 112 leaves at 476
    Customer 113 has arrived at 473
    Customer 113 leaves at 481
    Customer 114 has arrived at 474
    Customer 114 leaves at 476
    Customer 115 has arrived at 479
    Customer 115 leaves at 481

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,211
    Thanks
    80
    Thanked 4,571 Times in 4,535 Posts
    Questions:
    What does the "lab5data.txt" file look like? A few sample lines, at least, would help.

    Why do you call it a "circular queue"??? A bank line is a simple FIFO queue. Nothing circular about it. And I would implement a FIFO as a simple singly-linked list. Keep it simple. And efficient. You Queue class has the horrible limitation that it has a maximum size. To no advantage that I can see.

    To implement the three Tellers, you obviously need a Teller class with 3 instances thereof. Without that, I don't see how you can write the simulation at all. Start with one instance, if you wish, but you need that instance.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,211
    Thanks
    80
    Thanked 4,571 Times in 4,535 Posts
    I also can't figure out why you need the Event class.

    Maybe that's your equivalent of the Teller class, but I wouldn't have implemented it that way.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •