Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

se agregan ejercicios de 8 al 14 #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

package rosario.exercises.exercise_08_methods.ex08_1_exercise;

public class Item {
char color;
public boolean setColor(char colorCode){
if(colorCode == ' '){
return false;
}else{
this.color = colorCode;
return true;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

package rosario.exercises.exercise_08_methods.ex08_1_exercise;

public class ShoppingCart {
public static void main(String[] args){
Item item1 = new Item();
if(/*item1.setColor(' ')*/item1.setColor('U')){
System.out.println(item1.color);
}else{
System.out.println("Invalid color.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

package rosario.exercises.exercise_08_methods.ex08_2_exercise;


public class Item {
String desc;
int quantity;
double price;
char colorCode = 'U'; //'U' = Undetermined

public void displayItem() {
System.out.println("Item: " + desc + ", " + quantity + ", "
+ price + ", "+colorCode);
}

public void setItemFields(String desc, int quantity, double price) {
this.desc = desc;
this.quantity = quantity;
this.price = price;
}

public int setItemFields(String desc, int quantity, double price, char colorCode) {
if(colorCode == ' '){
return -1;
}else{
this.setItemFields(desc, quantity, price);
this.colorCode = colorCode;
return 1;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

package rosario.exercises.exercise_08_methods.ex08_2_exercise;


public class ShoppingCart {
public static void main(String[] args) {
Item item1 = new Item();
item1.setItemFields("hola",2,50.0);
item1.displayItem();

// case valid
if(item1.setItemFields("adios", 3, 100.0,'A') < 0){
System.out.println("Invalid Color");
}else{
item1.displayItem();
}

// Case invalid
//if(item1.setItemFields("adios", 3, 100.0,' ') < 0){
//System.out.println("Invalid Color");
//}else{
// item1.displayItem();
//}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package rosario.exercises.exercise_09_encapconstructors.ex09_1_exercise;

/**
* Created by rosario
*/
public class ShoppingCart {
private String name;
private String ssn;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSsn() {
return ssn;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package rosario.exercises.exercise_09_encapconstructors.ex09_2_exercise;

/**
* Created by rosario
*/
public class Customer {
private String name;
private String ssn;

public Customer(String name, String ssn){
this.name = name;
this.ssn = ssn;
}

public String getName(){
return name;
}
public void setName(String n){
name = n;
}

public String getSSN(){
return ssn;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package rosario.exercises.exercise_09_encapconstructors.ex09_2_exercise;

/**
* Created by rosario
*/
public class ShoppingCart {
public static void main(String args[]) {
Customer customer = new Customer("Rosario", "CUER931007MTLRSS08");
System.out.println(customer.getName());
System.out.println(customer.getSSN());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

package rosario.exercises.exercise_10_conditions.ex10_1_exercise;


public class TestClass {

public static void main(String args[]) {
int x = 4, y = 9;
if (y / x < 3) {
x += y;
} else {
x *= y;
}
System.out.println("After if stmt, x = " + x);

//Operator ternary
x = y/x<3?(x += y):(x *= y);
System.out.println("Operator ternary, x = " + x);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

package rosario.exercises.exercise_10_conditions.ex10_2_exercise;

public class Order {

static final char CORP = 'C';
static final char PRIVATE = 'P';
static final char NONPROFIT = 'N';
String name;
double total;
String stateCode;
double discount;
char custType;

public Order(String name, double total, String state, char custType) {
this.name = name;
this.total = total;
this.stateCode = state;
this.custType = custType;
calcDiscount();
}

public String getDiscount() {
return Double.toString(discount) + "%";
}

// Code the calcDiscount method.
public void calcDiscount() {
if (total > 900) {
if (NONPROFIT == custType) {
discount = 10.0;
} else if (PRIVATE == custType) {
discount = 7.0;
}
} else if (CORP == custType && total < 500) {
discount = 8.0;
} else if (total < 900) {
if (NONPROFIT == custType) {
discount = 5.0;
} else if (PRIVATE == custType) {
discount = 0.0;
} else if (CORP == custType && total > 500) {
discount = 5.0;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

package rosario.exercises.exercise_10_conditions.ex10_2_exercise;

public class ShoppingCart {
public static void main(String args[]){
Order order = new Order("Rick Wilson", 1000, "VA", Order.CORP);
System.out.println("Discount is: "+ order.getDiscount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

package rosario.exercises.exercise_10_conditions.ex10_3_exercise;

public class Order {
static final char CORP = 'C';
static final char PRIVATE = 'P';
static final char NONPROFIT = 'N';
String name;
double total;
String stateCode;
double discount;
char custType;

public Order(String name, double total, String state, char custType) {
this.name = name;
this.total = total;
this.stateCode = state;
this.custType = custType;
calcDiscount();
}

public String getDiscount(){
return Double.toString(discount) + "%";
}

public void calcDiscount() {
// Replace the if logic with a switch statement.
switch (custType){
case NONPROFIT:
if (total > 900) {
discount = 10.00;
} else {
discount = 5.00;
}
break;
case PRIVATE:
if (total > 900) {
discount = 7.00;
} else {
discount = 0.00;
}
break;
case CORP:
if (total < 500) {
discount = 8.00;
} else {
discount = 5.00;
}
break;
default:
discount = 0.0;
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

package rosario.exercises.exercise_10_conditions.ex10_3_exercise;

public class ShoppingCart {
public static void main(String args[]){
Order order = new Order("Rick Wilson", 910.00, "VA", Order.NONPROFIT);
System.out.println("Discount is: "+ order.getDiscount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package rosario.exercises.exercise_11_arraysloopsdates.ex11_1_exercise;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class ShoppingCart {
public static void main(String[] args) {
// Declare a LocalDateTime object, orderDate
LocalDateTime orderDate;
// Initialize the orderDate to the current date and time. Print it.
orderDate = LocalDateTime.now();
System.out.println(orderDate);
// Format orderDate using ISO_LOCAL_DATE; Print it.
System.out.print(orderDate.format(DateTimeFormatter.ISO_LOCAL_DATE));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

package rosario.exercises.exercise_11_arraysloopsdates.ex11_2_exercise;

public class ShoppingCart {
public static void main(String[] args) {
String name;
int age;
if(args.length <2){
System.out.println("2 arguments are required.");
}else{
name = args[0];
age = Integer.parseInt(args[1]);
System.out.println("name: "+name);
System.out.println("age: "+age);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package rosario.exercises.exercise_11_arraysloopsdates.ex11_3_exercise;

public class Item {

private int id;
private String desc;
private double price;
private boolean isBackOrdered;
private static int nextId = 1;

public Item(String desc, double price) {
setId();
this.desc = desc;
this.price = price;
checkStock();
}

private void checkStock(){ // typically would query a database here

// Negative random numbers will result in isBackOrdered = true
double num = Math.random() * 1000;

if ((int)num % 2 == 1 ){
this.isBackOrdered = true;
System.out.println(getDesc() +" is back ordered.");
}
else this.isBackOrdered();
}

private void setId() {
id = Item.nextId++;
}

public int getId() {
return id;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public boolean isBackOrdered() {
return isBackOrdered;
}
}
Loading