Can anyone change this to polymorphism?
it is a simple program. no calculation at all. but the problem is, everytime i'll try to use polymorphim or extanding the classes, error occurs. actually i wish this program is in polymorphism structure, but im not really good in it(polymorphism).

import java.io.*;
class drinks{
public String name;
public double price;
public int volume;
public drinks(String nama,double harga,int isipadu){ 
name=nama;
price=harga;
volume=isipadu;
}

public String showName(){
return name;
}
public double showPrice(){
return price;
}
public int showVolume(){
return volume;
}

}
class vendingMachine2{
public static void main(String[]args)throws IOException{

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

drinks d[]=new drinks[10];
d[0]=new drinks(" Cola small ",1.50,150);
d[1]=new drinks(" Cola medium ",2.50,550);
d[2]=new drinks(" Cola large ",3.50,800);
d[3]=new drinks(" Pepsi small ",1.40,150);
d[4]=new drinks(" Pepsi medium ",2.40,550);
d[5]=new drinks(" Pepsi large ",3.40,800);
d[6]=new drinks(" Sprite small ",1.30,150);
d[7]=new drinks(" Sprite medium ",2.30,550);
d[8]=new drinks(" Sprite large ",3.30,800);
String masuk;
int pilih;

System.out.println("\n--------------…
System.out.println("|| ||");
System.out.println("|| SELAMAT DATANG KE 'VENDING MACHINE' SAYA ||");
System.out.println("|| ||");
System.out.println("----------------…
System.out.println("|| || || || ||");
System.out.println("|| Nama || RM || ml || Get ||");
System.out.println("|| || || || ||");
System.out.println("----------------…
for(int x=0;x<9;x++){
System.out.println("||"+d[x].showNa… "+d[x].showPrice()+" || "+d[x].showVolume()+" || "+x+" ||");
System.out.println("|| || || || ||");
System.out.println("---------------…
}

String str = "masukan huruf tidak diterima. maaf";
String strUpper = str.toUpperCase();
String str2 = "sila run semula sistem ini";
String str2Upper = str2.toUpperCase();

try
{
System.out.print(" Get ");
masuk = input.readLine();
pilih = Integer.parseInt(masuk);
if(pilih <= 8)
{
System.out.println(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ");
System.out.println(" | | | |");
System.out.println(" |^_^| < Tahniah! anda telah membeli: |");
System.out.println(" |_ _| |"+d[pilih].showName()+" |");
System.out.println(" /|\\ | "+d[pilih].showVolume()+" mL dengan harga |");
System.out.println(" | | RM "+d[pilih].showPrice()+"0 sahaja. gulp! |");
System.out.println(" / \\ |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|");
}
else 
{
System.out.println("Tiada dalam pilihan!");
} 
}
catch(Exception ex)
{
System.out.println(strUpper);
System.out.println(str2Upper);
}
}
}

Edited 5 Years Ago by ~s.o.s~: Added code tags, please learn to use them.

Polymorphism is a side-product of good OO design. It's not something you design.

What part do you want to make polymorphic? The drink? If yes, just create 10 subclasses of drink, one for each type and hardcode teh values you're passing in c'tors on lines 29-37.

This article has been dead for over six months. Start a new discussion instead.