TUGAS 4 PEMROGRAMAN BERORIENTASI OBJEK

Karina Soraya P
05111740000003
PBO - B


MEMBUAT MUSIC ORGANIZER


  • Source Code MusicOrganizer
 import java.util.ArrayList;  
 /**  
  * A class to hold details of audio files.  
  *  
  * @author Karina Soraya P  
  * @version 13.0/20181008  
  */  
 public class MusicOrganizer  
 {  
   // An ArrayList for storing the file names of music files.  
   private ArrayList<String> files;  
   /**  
    * Create a MusicOrganizer  
    */  
   public MusicOrganizer()  
   {  
     files = new ArrayList<>();  
   }  
   /**  
    * Add a file to the collection.  
    * @param filename The file to be added.  
    */  
   public void addFile(String filename)  
   {  
     files.add(filename);  
   }  
   /**  
    * Return the number of files in the collection.  
    * @return The number of files in the collection.  
    */  
   public int getNumberOfFiles()  
   {  
     return files.size();  
   }  
   /**  
    * List a file from the collection.  
    * @param index The index of the file to be listed.  
    */  
   public void StartPlaying(int index)  
   {  
     if(index >= 0 && index < files.size()) {  
       String filename = files.get(index);  
       System.out.println("Now Playing : ");  
       System.out.println(filename);  
     }  
   }  
   /**  
    * Remove a file from the collection.  
    * @param index The index of the file to be removed.  
    */  
   public void removeFile(int index)  
   {  
     if(index >= 0 && index < files.size()) {  
       files.remove(index);  
     }  
   }  
   public void StopPlaying(){  
     System.out.println("==============================");  
     System.out.println("Music Has Stopped");  
     System.out.println("==============================");  
   }  
 }  
  • Hasil







MEMBUAT SISTEM PERLELANGAN


  • Source Code Auction
 import java.util.ArrayList;   
  /**   
  * A simple model of an auction   
  *The auction mantains a list of lots of arbitatry length   
  * @author Karina Soraya P   
  * @version 10.0/20181008   
  */   
  public class Auction   
  {   
   private ArrayList<Lot> lots;  
   private int LotNumber;   
   public Auction()  
   //Method membuat Auction baru   
   {   
    lots = new ArrayList<Lot>();  
    //list barang    
    LotNumber = 1;  
    //nomor barang awal adalah 1   
   }   
   public void enterLot(String description)   
   {  
     //Method untuk menambahkan barang baru dalam auction   
    lots.add(new Lot(LotNumber, description));   
    LotNumber++;   
   }   
   public void showLots()   
   {  
     //Method menampilkan seluruh barang yang dilelang   
    for(Lot lot : lots) {   
     System.out.println(lot.detail());   
    }   
   }   
   /**   
   * Method untuk melakukan bid/tawaran.   
   * Jika berhasil melakukan bid akan muncul Success Message.   
   * CurrentlotNumber = id lot yang akan di Bid.   
   * bidder = orang yang akan menawar.   
   * value = harga tawaran.   
   */   
   public void MakeBid(int CurrentlotNumber, Person bidder, long value)   
   {   
    Lot selectedLot = getLot(CurrentlotNumber);   
    if(selectedLot != null) {   
     boolean check = selectedLot.bidFor(new Bid(bidder, value));   
     if(check) {  
       //check dengan bid sebelumnya, apakah lebih besar atau tidak   
      System.out.println("Bid untuk lot nomor " +   
           CurrentlotNumber + " berhasil dilakukan");  
      System.out.println("Bid dilakukan oleh " +bidder.getName()+ " dengan harga " +value+ "$");   
     }   
     else {  
       //apabila bid sebelumnya lebih besar, maka bid ini akan gagal   
      Bid highestBid = selectedLot.getHighestBid();   
      System.out.println("Lot nomor : " + CurrentlotNumber +   
           " dengan bid tertinggi: " +   
           highestBid.getBid());   
     }   
    }   
   }   
   /**   
   * Return id lot jika ada, jika tidak return null   
   * CurrentlotNumber = id lot yang perlu di return   
   */   
   public Lot getLot(int CurrentlotNumber)   
   {   
    if((CurrentlotNumber >= 1) && (CurrentlotNumber < LotNumber)) {  
      //cek apabila lot number ada atau tidak   
     Lot selectedLot = lots.get(CurrentlotNumber - 1);   
     if(selectedLot.getId() != CurrentlotNumber) {   
      System.out.println("Internal error : Lot nomor " +   
           selectedLot.getId() +   
           "telah dikembalikan, bukan lot nomor " +   
           CurrentlotNumber);   
      selectedLot = null;   
     }   
     return selectedLot;   
    }   
    else {   
     System.out.println("Lot nomor : " + CurrentlotNumber +   
          " tidak ada.");   
     return null;   
    }   
   }   
   public void close()    
   {    
   System.out.println("pelelangan berakhir.");    
   for(Lot lot : lots)    
   {    
    System.out.println(lot.getId() + ": " +lot.getDescription());    
    Bid bid = lot.getHighestBid();    
    if (bid==null)    
    {    
    System.out.println("Tidak ada bid untuk lot ini");    
    }    
    else    
    {    
    System.out.println("Item ini terjual kepada" +    
    bid.getBidder().getName() + " dengan harga : "    
    + bid.getBid());    
    }    
   }    
   }    
  }   
  • Source Code Person
 /**   
  * Maintain details of someone who participates in an auction   
  *   
  * @author Karina Soraya P   
  * @version 10.3/20181008   
  */   
  public class Person   
  {   
   private final String name;  
   //variabel string untuk menyimpan nama orang    
   public Person(String newName)   
   {  
    this.name = newName;   
   }   
   public String getName()   
   {  
    return name;   
   }   
  }   
  • Source Code Bid
 /**   
  * A class that models an auction bid   
  *   
  * @author Karina Soraya P   
  * @version 10.1/20181008   
  */   
  public class Bid   
  {   
   private final Person bidder;  
   //orang yang akan melakukan tawaran   
   private final long value;  
   //untuk menyimpan harga tawaran   
   public Bid(Person bidder, long value)   
   {  
     //Method untuk membuat penawar baru dengan nama dan jumlah tawaran nya   
    this.bidder = bidder;   
    this.value = value;   
   }   
   public Person getBidder()   
   {  
     //untuk return nama penawar   
    return bidder;   
   }   
   public long getBid()   
   {  
     //untuk return harga tawaran nya   
    return value;   
   }   
  }   
  • Source Code Lot
 /**   
  * A class to model an item (or set of items) in an    
  *auction: a lot.   
  *   
  * @author Karina Soraya P   
  * @version 10.2/20181008   
  */   
  public class Lot  
  //method untuk membuat Lot(sebuah/ sekumpulan item di sebuah auction)   
  {   
   private final int Id;  
   //id barang(unique)   
   private String description;  
   //deskripsi barang   
   private Bid highestBid;  
   //tawaran tertinggi untuk barang tersebut   
   public Lot(int number, String description)   
   {  
    this.Id = number;   
    this.description = description;   
   }   
   //parameter nya merupakan bid untuk cek harga bid sebelumnya   
   public boolean bidFor(Bid bid)   
   {  
     //Method untuk mengecek apakah tawaran saat ini lebih besar dari tawaran tertinggi   
    if((highestBid == null) ||   
      (bid.getBid() > highestBid.getBid())) {   
     highestBid = bid;  
     //jika ya maka tawaran ini adalah tawaran tertinggi yang baru   
     return true;   
    }   
    else {   
     return false;   
    }   
   }   
   public String detail()   
   {  
     //method untuk return detail lot(id dan tawaran tertinggi)   
    String details = Id + ": " + description;   
    if(highestBid != null) {   
     details += " Bid dengan harga " +    
        highestBid.getBid();   
    }   
    else {   
     details += " (belum ada bid)";   
    }   
    return details;   
   }   
   public int getId()   
   {  
     //Method untuk return id lot   
    return Id;   
   }   
   public String getDescription()   
   {  
     //Method untuk return deskripsi dari lot    
    return description;   
   }   
   public Bid getHighestBid()   
   {  
     //Method untuk return tawaran tertinggi dari lot    
    return highestBid;   
   }   
  }   
  • Hasil




Ketika Lelangnya Close


Komentar

Postingan Populer