NRP : 05111740000054
Kelas : PBO - B
Tugas minggu ini adalah membuat program "Music Organizer dan Auction (Lelang)".
1. Music Organizer
1. Music Organizer
- Class yang dibutuhkan :
- Source code :
- MusicOrganizer
/**
* Music Organizer
* Yasinta Yusniawati
* 14 Oktober 2018
*/
import java.util.ArrayList;
public class MusicOrganizer
{
private ArrayList<String> files;
private MusicPlayer player;
// membuat music organizer
public MusicOrganizer()
{
files = new ArrayList<String>();
player = new MusicPlayer();
}
//nambah file
public void tambahFile(String namafile)
{
files.add(namafile);
}
// input jumlah file pada koleksi
public int jumlahFile()
{
return files.size();
}
// list file pada koleksi
public void listFile()
{
if(files.size()>0)
{
for(int i=0;i<files.size();i++){
String namafile = files.get(i);
System.out.println("Daftar putar " + (i+1) + ": " + namafile);
}
System.out.println("\n");
}
}
// hapus file dari koleksi
public void hapusFile(int index)
{
if(index >= 0 && index < files.size())
{
files.remove(index-1);
}
}
public void startPlaying(int index)
{
String namafile = files.get(index-1);
player.startPlaying(namafile);
}
public void stopPlaying()
{
player.stopPlaying();
}
}
- MusicPlayer
/**
*MusicPlayer
* Yasinta Yusniawati
* 14 Oktober 2018
*/
public class MusicPlayer
{
private String music;
public MusicPlayer()
{
- public void stopPlaying()
music = null;
}
public void startPlaying(String namafile)
{
music = namafile;
System.out.println(music+ "---[Playing]");
}
{
System.out.println("(Stop)");
}
}
- Cara menggunan :
- Daftar lagu sebelum dimasukkan adalah kosong untuk memutar musiknya kita harus memasukkan nama lagu lerlebih dahulu sebanyak jumlah musik yang di inginkan
- Jika ingin memutar lagu yang telah di masukkan daftar putar maka ketikan urutan lagu yang di inginkan
2. Auction
- Class Auction
import java.util.ArrayList;
/**
* Auction
* Yasinta Yusniawati
* 14 Okt 2018
*/
public class Auction
{
// The list of Lots in this auction.
private ArrayList<Lot> lots;
// The number that will be given to the next lot entered
// into this auction.
private int nextLotNumber;
public Auction()
{
lots = new ArrayList<>();
nextLotNumber = 1;
}
public void enterLot(String description)
{
lots.add(new Lot(nextLotNumber, description));
nextLotNumber++;
}
public void showLots()
{
System.out.println("Barang yang dilelang:");
for(Lot lot : lots) {
System.out.println(lot.toString());
}
}
public void makeABid(int lotNumber, Person bidder, long value)
{
Lot selectedLot = getLot(lotNumber);
if(selectedLot != null) {
Bid bid = new Bid(bidder, value);
boolean successful = selectedLot.bidFor(bid);
if(successful) {
System.out.println("Penawaran untuk barang nomor " +
lotNumber + " berhasil.");
}
else {
// Report which bid is higher.
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Barang nomor" + lotNumber +
" sudah dilelang dengan harga: " +
highestBid.getValue());
}
}
}
public Lot getLot(int lotNumber)
{
if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
// The number seems to be reasonable.
Lot selectedLot = lots.get(lotNumber - 1);
// Include a confidence check to be sure we have the
// right lot.
if(selectedLot.getNumber() != lotNumber) {
System.out.println("Internal error: Lot number " +
selectedLot.getNumber() +
" was returned instead of " +
lotNumber);
// Don't return an invalid lot.
selectedLot = null;
}
return selectedLot;
}
else {
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
}
- Class Lot
/**
* Auction - Lot
* Yasinta Yusniawati
* 14 Okt 2018
*/
public class Lot
{
// A unique identifying number.
private final int number;
// A description of the lot.
private String description;
// The current highest bid for this lot.
private Bid highestBid;
public Lot(int number, String description)
{
this.number = number;
this.description = description;
this.highestBid = null;
}
public boolean bidFor(Bid bid)
{
if(highestBid == null) {
// There is no previous bid.
highestBid = bid;
return true;
}
else if(bid.getValue() > highestBid.getValue()) {
// The bid is better than the previous one.
highestBid = bid;
return true;
}
else {
// The bid is not better.
return false;
}
}
public String toString()
{
String details = number + ": " + description;
if(highestBid != null) {
details += " Dengan harga: " +
highestBid.getValue();
}
else {
details += " (No bid)";
}
return details;
}
public int getNumber()
{
return number;
}
public String getDescription()
{
return description;
}
public Bid getHighestBid()
{
return highestBid;
}
}
- Class Person
/**
* Auction - Person
* Yasinta Yusniawati
* 14 Okt 2018
*/
public class Person
{
// The name of this person.
private final String name;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
- Class Bid
/**
* Auction - Bid
* Yasinta Yusniawati
* 14 Okt 2018
*/
public class Bid
{
// The person making the bid.
private final Person bidder;
private final long value;
public Bid(Person bidder, long value)
{
this.bidder = bidder;
this.value = value;
}
public Person getBidder()
{
return bidder;
}
public long getValue()
{
return value;
}
}
- Hasil :
Tidak ada komentar:
Posting Komentar