호그와트

어느새 Java가 나의 모국어가 되었다

영웅*^%&$ 2024. 1. 24. 05:28
728x90

package cookie;

import java.util.Scanner;

public class RoomBooking {

public static double[] calculateDiscount(int mileage) {

double discountPercentage = 0;

if (mileage >= 100) {

discountPercentage = 0.10;

mileage -= 100;

} else if (mileage >= 50) {

discountPercentage = 0.05;

mileage -= 50;

} else if (mileage >= 10) {

discountPercentage = 0.01;

mileage -= 10;

} else {

System.out.println("Not enough mileage for a discount.");

}

discountPercentage = Math.min(discountPercentage, 0.50);

return new double[]{discountPercentage, mileage};

}

 

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Which room type do you want? (1: Single, 2: Double, 3: Suite)");

int choice = scanner.nextInt();

 

if (choice < 1 || choice > 3) {

System.out.println("Invalid input.");

return;

}

 

System.out.println("How many nights will you stay?");

int numberOfNights = scanner.nextInt();

if (numberOfNights <= 0) {

System.out.println("Invalid number of nights.");

return;

}

 

System.out.println("Do you have any mileage points?");

int mileage = scanner.nextInt();

double cost;

switch (choice) {

case 1:

cost = 10.0 * numberOfNights;

break;

case 2:

cost = 20.0 * numberOfNights;

break;

case 3:

cost = 30.0 * numberOfNights;

break;

default:

System.out.println("Invalid room type.");

return;

}

 

double[] discountInfo = calculateDiscount(mileage);

double discount = discountInfo[0];

double discountedCost = cost - (cost * discount);

mileage = (int) discountInfo[1];

System.out.println("Initial cost: " + cost);

System.out.println("Discount applied: " + (discount * 100) + "%");

System.out.println("Final cost: " + discountedCost);

System.out.println("Remaining mileage points: " + mileage);

 

scanner.close();

}

}

 

package Inheritance;

 

import java.util.Scanner;

 

public class SubscriptionDemo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Please enter your name : ");

String name = scanner.nextLine();

System.out.println("Please enter your address :");

String address = scanner.nextLine();

System.out.println("Please tell me whether you want PhysicalNewspaper or OnlineNewspaper");

System.out.println("If you want PhysicalNewspaper then press 1");

System.out.println("If you want OnlineNewspaper then press any other number");

int choice = scanner.nextInt();

if(choice == 1){

PhysicalNewspaperSubscription paper1 = new PhysicalNewspaperSubscription(name, address, 0);

paper1.setAddress(address);

 

System.out.println("Subscriber name : " + paper1.getName());

System.out.println("Address : " + paper1.getAddress());

System.out.println("Cost: " + paper1.getRate());

} else{

NewsPaperSubscription paper2 = new OnlineNewspaperSubscription(name, address, 0);

paper2.setAddress(address);

 

System.out.println("Subscriber name : " + paper2.getName());

System.out.println("Address : " + paper2.getAddress());

System.out.println("Cost: " + paper2.getRate());

}

}

}

 

package Inheritance;

 

public class MagazineSubscription extends Subscription {

private String address;

private boolean isDigital;

 

public MagazineSubscription(String name, double price, String address, boolean digital) {

super(name, price);

this.address = address;

this.isDigital = digital;

}

 

@Override

public String getDetails() {

String deliveryType = isDigital ? "Digital" : "Physical";

return "Magazine Subscription: " + subscriberName + ", " + deliveryType + ", " + address;

}

}

 

package Inheritance;

 

public class OnlineNewspaperSubscription extends NewsPaperSubscription{

public OnlineNewspaperSubscription(String name, String address, double rate) {

super(name, address, 0);

}

 

@Override

public void setAddress(String address) {

if (containsAt(address)) {

super.setAddress(address);

super.setRate(9);

} else {

System.out.println("Error: Online Address must contain @.");

super.setRate(0);

}

}

 

private boolean containsAt(String address) {

for (char c : address.toCharArray()) {

if (c == '@') {

return true;

}

}

return false;

}

}

 

package Inheritance;

public abstract class NewsPaperSubscription {

private String name;

private String address;

private double rate;

 

public NewsPaperSubscription(String name, String address, double rate){

this.name = name;

this.address = address;

this.rate = rate;

}

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public String getAddress() {return address;}

 

public double getRate() {return rate;}

 

 

public void setAddress(String address) {

this.address = address;

}

 

protected void setRate(double rate) {

this.rate = rate;

}

 

}

 

package Inheritance;

 

public class PhysicalNewspaperSubscription extends NewsPaperSubscription {

 

public PhysicalNewspaperSubscription(String name, String address, double rate) {

super(name, address, 0);

}

 

@Override

public void setAddress(String address) {

if (containsDigit(address)) {

super.setAddress(address);

super.setRate(15);

} else {

System.out.println("Error: Address must contain at least one digit.");

super.setRate(0);

}

}

 

private boolean containsDigit(String address) {

for (char c : address.toCharArray()) {

if (Character.isDigit(c)) {

return true;

}

}

return false;

}

}

 

package Inheritance;

 

public class ElementArray {

public static void main(String[] args) {

Element[] elements = new Element[6];

elements[0] = new MetalElement("Fe", 26, 55.845);

elements[1] = new MetalElement("Ti", 22,47.867);

elements[2] = new MetalElement("Cu", 29, 63.546);

elements[3] = new NonMetalElement("S", 16, 32.065);

elements[4] = new NonMetalElement("O", 8, 15.999);

elements[5] = new NonMetalElement("C", 6, 12.01);

 

for (Element element : elements) {

System.out.println(element.describeElement());

}

}

}

 

package Inheritance;

 

abstract class Element {

private String symbol;

private int atomicNumber;

private double atomicWeight;

 

public Element(String symbol, int atomicNumber, double atomicWeight) {

this.symbol = symbol;

this.atomicNumber = atomicNumber;

this.atomicWeight = atomicWeight;

}

 

public String getSymbol() {

return symbol;

}

 

public int getAtomicNumber() {

return atomicNumber;

}

 

public double getAtomicWeight() {

return atomicWeight;

}

 

abstract String describeElement();

}

 

package Inheritance;

class MetalElement extends Element {

public MetalElement(String symbol, int atomicNumber, double atomicWeight) {

super(symbol, atomicNumber, atomicWeight);

}

 

@Override

String describeElement() {

return "Metal " + getSymbol() + " has atomic number " + getAtomicNumber() + " and atomic weight " + getAtomicWeight() + ". Metals are good conductors of heat and electricity.";

}

}

 

class NonMetalElement extends Element {

public NonMetalElement(String symbol, int atomicNumber, double atomicWeight) {

super(symbol, atomicNumber, atomicWeight);

}

 

@Override

String describeElement() {

return "Non-metal " + getSymbol() + " has atomic number " + getAtomicNumber() + " and atomic weight " + getAtomicWeight() + ". Non-metals are poor conductors of heat and electricity.";

}

}

728x90