Skip to content

Instantly share code, notes, and snippets.

/Webshop.java Secret

Created June 27, 2017 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/c29334ac54abdafea99702554072cd0a to your computer and use it in GitHub Desktop.
Save anonymous/c29334ac54abdafea99702554072cd0a to your computer and use it in GitHub Desktop.
class Article {
int artCode;
float price;
int stock;
void buy (int n) {
stock += n;
}
void sell (int n) {
stock -= n;
}
boolean available () {
return stock > 0;
}
String getDescription () {
return "ArtNr. " + artCode + " €" + price;
}
Article (int ac, float p) {
artCode = ac;
price = p;
stock = 0;
}
}
class Book extends Article {
String author;
String title;
String getDescription () {
return getKind() + " '" + title + "' von " +
author + "; " + super.getDescription();
}
String getKind () {
return "Buch";
}
Book (int ac, float p, String a, String t) {
super(ac, p);
author = a;
title = t;
}
Book (int ac, String a, String t) { // construct a free book
this(ac, 0, a, t);
}
}
class PrintedBook extends Book {
String getKind () {
return "Print-Ausgabe";
}
PrintedBook (int ac, float p, String a, String t) {
super(ac, p, a, t);
}
}
class EBook extends Book {
String getKind () {
return "E-Book";
}
EBook (int ac, float p, String a, String t) {
super(ac, p, a, t);
}
}
class Audio extends Article{
String albumTitle;
String artist;
int trackNum;
int duration;
String getDescription(){
return getKind() + " '"+ albumTitle + "' von " + artist +
"; "+ trackNum + " Lieder, " + duration +" Gesamtlänge. "
+ super.getDescription();
}
String getKind(){
return "Audio";
}
Audio(String aT, String a, int tN, int d, float p, int ac){
super(ac, p);
albumTitle = aT;
artist = a;
trackNum = tN;
}
}
class MP3Album extends Audio{
int bitrate;
String getKind(){
return "MP3-Album";
}
MP3Album(String aT, String a, int tN, int d, int br, float p, int ac){
super(aT, a, tN,d,p,ac);
bitrate = br;
}
}
class CDAlbum extends Audio{
String cover;
String getKind(){
return "CD-Album";
}
CDAlbum(String aT,String a, int tN, int d, String c,float p, int ac){
super(aT, a, tN, d,p,ac);
cover = c;
}
}
// brauche eventuell eine WArenkorbklasse, oder eine Methode. Muss noch überlegen.
public class WebShop {
static void printInvoice (Article[] cart) {
float total = 0;
Out.println("Rechnung");
Out.println("--------");
for (Article a: cart) {
if (a != null) {
String s = a.getDescription();
Out.println(s);
total += a.price;
}
}
Out.println("===============");
Out.printf("Summe: €%7.2f\n", total);
}
public static void main (String[] args) {
Article[] cart = new Article[10];
cart[0] = new Book(123, 7.95f, "ky", "Sühne");
cart[1] = new PrintedBook(124, 14.95f, "ky", "Leben");
cart[2] = new EBook(125, 5.95f, "ky", "Schuld");
cart[3] = new CDAlbum(
"Forever", "Scorpions",15, 40, "Wooden Cover",13.87f,342 );
printInvoice(cart);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment