W ramach ćwiczeń z programowania obiektowego probuje napisać "aplikacje", która umożliwia wyświetlenie dostępnych piosenek (zapisanych w tablicy), dodania do biblioteki (również tablicy) i wszystko jest dobrze, jeśli podaje wartości w kolejności od 1 do n, gdy chce podać np tylko piosenkę nr 4, to wartość zostaje zapisana do tablicy, ale kiedy chce wyświetlić tablice piosenek z biblioteki, to wtedy nic się nie wyświetla.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Menu menu = new Menu();
menu.displayMenu();
}
}
class Menu {
void displayMenu() {
Scanner input = new Scanner(System.in);
System.out.println("Witaj w aplikacji Muzyka!\n-------------------------");
System.out.println("1. Pokaż dostępne piosenki");
System.out.println("2. Pokaż moją bibliotekę");
System.out.print("Twój wybór: ");
int choice = input.nextInt();
switch (choice) {
case 1:
Songs.getSongs();
displaySubMenu();
displayMenu();
break;
case 2:
Songs.getLibrarySongs();
break;
default:
throw new IllegalStateException(STR."Unexpected value: \{choice}");
}
}
private void displaySubMenu() {
int choice;
while (true)
{
Scanner input = new Scanner(System.in);
System.out.println("Czy chcesz dodać piosenke do biblioteki? Podaj jej numer, jeśli nie wprowadź 0");
choice = input.nextInt();
if (choice > 0 && choice <= Songs.songs.length) {
Songs.librarySongs[choice - 1] = Songs.songs[choice - 1];
if (Songs.librarySongs[choice - 1] != null) {
System.out.println(STR."Dodano piosenkę \{Songs.librarySongs[choice - 1]} do biblioteki!");
} else {
System.out.println("Niestety napotkano problem podczas dodawania zmiennej do tablicy!");
}
} else if (choice == 0) {
break;
} else {
throw new IllegalStateException(STR."Unexpected value: \{choice}");
}
}
}
}
class Songs {
static String[] librarySongs = new String[100];
static String[] songs = {
"Bohemian Rhapsody - Queen",
"Hey Jude - The Beatles",
"Imagine - John Lennon",
"Hotel California - Eagles",
"Stairway to Heaven - Led Zeppelin",
"Let It Be - The Beatles",
"Yesterday - The Beatles",
"Like a Rolling Stone - Bob Dylan",
"I Will Always Love You - Whitney Houston",
"Smells Like Teen Spirit - Nirvana",
"Thriller - Michael Jackson",
"Billie Jean - Michael Jackson",
"Hallelujah - Leonard Cohen",
"Sweet Child o' Mine - Guns N' Roses",
"Wonderwall - Oasis",
"Boogie Wonderland - Earth, Wind & Fire",
"Piano Man - Billy Joel",
"Don't Stop Believin' - Journey",
"Purple Rain - Prince",
"Let's Get It On - Marvin Gaye",
"Eye of the Tiger - Survivor",
"Livin' on a Prayer - Bon Jovi",
"I Want to Hold Your Hand - The Beatles",
"Dancing Queen - ABBA",
"My Heart Will Go On - Celine Dion",
"Wonderful Tonight - Eric Clapton",
"Every Breath You Take - The Police",
"Girls Just Want to Have Fun - Cyndi Lauper",
"Stayin' Alive - Bee Gees",
"Every Rose Has Its Thorn - Poison",
"Walk This Way - Aerosmith",
"Another One Bites the Dust - Queen",
"Take Me Home, Country Roads - John Denver",
"I Will Survive - Gloria Gaynor",
"We Are the Champions - Queen",
"Crazy - Gnarls Barkley",
"California Dreamin' - The Mamas & The Papas",
"Let It Go - Idina Menzel",
"Rolling in the Deep - Adele",
"Under Pressure - Queen & David Bowie",
"Dancing in the Dark - Bruce Springsteen",
"Don't Stop 'Til You Get Enough - Michael Jackson",
"Oops!... I Did It Again - Britney Spears",
"Total Eclipse of the Heart - Bonnie Tyler",
"Bridge Over Troubled Water - Simon & Garfunkel",
"Wannabe - Spice Girls",
"Sweet Caroline - Neil Diamond",
"I Wanna Dance with Somebody - Whitney Houston",
"Uptown Funk - Mark Ronson ft. Bruno Mars",
"Achy Breaky Heart - Billy Ray Cyrus",
"I Want to Know What Love Is - Foreigner",
"Man in the Mirror - Michael Jackson",
"Mrs. Robinson - Simon & Garfunkel",
"Ring of Fire - Johnny Cash",
"Boys Don't Cry - The Cure",
"I Believe I Can Fly - R. Kelly",
"Don't Worry, Be Happy - Bobby McFerrin",
"Love Shack - The B-52's",
"With or Without You - U2",
"Stand by Me - Ben E. King",
"Hey Ya! - OutKast",
"Killing Me Softly With His Song - Roberta Flack",
"All You Need Is Love - The Beatles",
"My Girl - The Temptations",
"I Heard It Through the Grapevine - Marvin Gaye",
"I Love Rock 'n' Roll - Joan Jett & The Blackhearts",
"American Pie - Don McLean",
"Suspicious Minds - Elvis Presley",
"Georgia on My Mind - Ray Charles",
"I Can't Help Falling in Love - Elvis Presley",
"Yesterday Once More - Carpenters",
"Let's Stay Together - Al Green",
"Brown Eyed Girl - Van Morrison",
"September - Earth, Wind & Fire",
"Love Me Tender - Elvis Presley",
"Baby Got Back - Sir Mix-A-Lot",
"I Will Always Love You - Dolly Parton",
"You Are the Sunshine of My Life - Stevie Wonder",
"Hello - Adele",
"I Want to Hold Your Hand - The Beatles",
"Great Balls of Fire - Jerry Lee Lewis",
"Proud Mary - Creedence Clearwater Revival",
"We Will Rock You - Queen",
"Sweet Home Alabama - Lynyrd Skynyrd",
"Hey There Delilah - Plain White T's",
"ABC - The Jackson 5",
"Bennie and the Jets - Elton John",
"Born to Run - Bruce Springsteen",
"Brown Sugar - The Rolling Stones",
"Don't Let the Sun Go Down on Me - Elton John",
"Free Fallin' - Tom Petty",
"I Got You (I Feel Good) - James Brown",
"I'm a Believer - The Monkees",
"Just the Way You Are - Billy Joel",
"La Bamba - Ritchie Valens",
"Like a Prayer - Madonna",
"Love Story - Taylor Swift",
"Maggie May - Rod Stewart",
"Margaritaville - Jimmy Buffett",
"Oh, Pretty Woman - Roy Orbison"
};
static public void getSongs() {
int i = 0;
for (String song : songs) {
i++;
System.out.println(STR."\{i}.\{song}");
}
}
static public void getLibrarySongs() {
int i = 0;
System.out.println("Twoja biblioteka: ");
for (String song : librarySongs) {
if (song == null) {
break;
}
i++;
System.out.println(STR."\{i}.\{song}");
}
}
}