Ejercicio De Cafe

Un programa capaz de calcular el precio de un café, contemplando que el cliente pueda pedir extras al mismo.

clase Cuenta

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

*/
/**
*

  • @author Carlos Alejandro

*/
public class Cuenta extends Coffee{
public Cuenta(){
super();
total=0;
}

protected Iterator recorreLista=cuenta.iterator();
protected double total;
protected String elemento;

public void subTotal(int i){
if(cuenta.get(i).equals("americano")){
System.out.println("Cafe americano : $10");
total+=10;
}else if(cuenta.get(i).equals("express")){
System.out.println("Cafe express : $10");
total+=10;
}else if(cuenta.get(i).equals("capucchino")){
System.out.println("Cafe capuchino : $15");
total+=15;
}else if(cuenta.get(i).equals("moka")){
System.out.println("Cafe moka : $18");
total+=18;
}
}

public void cuentatotal(){
for(int i =0; i< cuenta.size();i++){
elemento = cuenta.get(i);
subTotal(i);
if(elemento.equals("conChantilly")){
System.out.println("Chantilly : $3");
total+=3;
}else if(elemento.equals("conDobleChantilly")){
System.out.println("Chantilly : $5");
total+=5;
}else if(elemento.equals("conChispas")){
System.out.println("chispas : $3");
total+=3;
}else if(elemento.equals("conLechera")){
System.out.println("lechera : $2.5");
total+=2.5;
}else if(elemento.equals("conJarabeChocolate")){
System.out.println("Chantilly : $3.5");
total+=3.5;
}
}
System.out.println("Total a pagar : $"+total);
}

}

Clase Coffee

import java.util.ArrayList;

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

*/
import java.util.List;
/**
*

  • @author Carlos Alejandro

*/
public class Coffee{
public Coffee(){}
public List<String> cuenta = new ArrayList<String>();

public void tipo(String tipoCafe){
cuenta.add(tipoCafe);
}

public void chantilly(){
cuenta.add("conChantilly");
}

public void dobleChantilly(){
cuenta.add("conDobleChantilly");
}

public void chispas(){
cuenta.add("conChispas");
}

public void lechera(){
cuenta.add("conLechera");
}

public void jarabeChocolate(){
cuenta.add("conJarabeChocolate");
}

Una vez creadas nuestras clases necesitaremos de nuestro main para poder crear objetos de cada clase y visualizar el resultado

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Cuenta coffee1=new Cuenta();
Cuenta coffee2=new Cuenta();

coffee1.tipo("capucchino");
coffee1.chantilly();
coffee1.chispas();
coffee1.cuentatotal();
System.out.println();
coffee2.tipo("express");
coffee2.lechera();
coffee2.cuentatotal();
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License