add lab11
This commit is contained in:
49 files changed
+1993
No files matched your search
@@ -0,0 +1,36 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.lang.NumberFormatException;
|
||||
|
||||
/**
|
||||
* Average with command line args.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Average {
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args from command line.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0) {
|
||||
System.out.println("No arguments.");
|
||||
return;
|
||||
}
|
||||
|
||||
double total = 0.0;
|
||||
int numbers = 0;
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
try {
|
||||
total += Double.parseDouble(args[i]);
|
||||
numbers++;
|
||||
} catch (NumberFormatException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Average: " + total / numbers);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Represents a two-sided coin that can be flipped. Modified over class
|
||||
* example to use enum.
|
||||
*
|
||||
* @author Lewis & Loftus 9e
|
||||
* @author Bruce Link
|
||||
* @author BCIT
|
||||
* @version 2023
|
||||
*/
|
||||
public class Coin implements Lockable {
|
||||
/** Enumeration type representing sides of a coin. */
|
||||
public enum CoinFace {
|
||||
/** the side of a coin considered tails. */
|
||||
Tails,
|
||||
/** the side of a coin considered heads. */
|
||||
Heads
|
||||
}
|
||||
|
||||
/** Coin's current face showing. */
|
||||
private CoinFace face;
|
||||
|
||||
/** key to lock and unlock methods. */
|
||||
private int key;
|
||||
|
||||
/** locked indicates if functions can be used. */
|
||||
private boolean locked;
|
||||
|
||||
/**
|
||||
* Constructs a Coin object and flips it to give it a starting value.
|
||||
*/
|
||||
public Coin() {
|
||||
this.key = 0;
|
||||
this.locked = false;
|
||||
flip();
|
||||
}
|
||||
|
||||
/**
|
||||
* setKey value.
|
||||
* @param key to set
|
||||
*/
|
||||
public void setKey(int key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
/**
|
||||
* lock Object with key.
|
||||
* @param keyIn to lock.
|
||||
* @return success
|
||||
*/
|
||||
public boolean lock(int keyIn) {
|
||||
this.locked = this.key == keyIn ? true : this.locked;
|
||||
return this.key == keyIn;
|
||||
}
|
||||
|
||||
/**
|
||||
* unlock Object with key.
|
||||
* @param keyIn to unlock.
|
||||
* @return success
|
||||
*/
|
||||
public boolean unlock(int keyIn) {
|
||||
this.locked = this.key == keyIn ? false : this.locked;
|
||||
return this.key == keyIn;
|
||||
}
|
||||
|
||||
/**
|
||||
* locked Object state.
|
||||
* @return locked
|
||||
*/
|
||||
public boolean locked() {
|
||||
return this.locked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flips this Coin by randomly choosing a face value.
|
||||
*/
|
||||
public void flip() {
|
||||
if (this.locked) {
|
||||
return;
|
||||
}
|
||||
final double half = 0.5;
|
||||
if (Math.random() < half) {
|
||||
face = CoinFace.Heads;
|
||||
} else {
|
||||
face = CoinFace.Tails;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns side of coin as CoinFace type.
|
||||
* @return current showing face of this coin.
|
||||
*/
|
||||
public CoinFace getFace() {
|
||||
return face;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the coin face explicitly. No required check on values as enum
|
||||
* CoinFace allows no illegal values to be passed.
|
||||
* @param newFace the new face to show.
|
||||
*/
|
||||
public void setFace(CoinFace newFace) {
|
||||
face = newFace;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the current face of the Coin is heads.
|
||||
* @return true if current face is heads, else false.
|
||||
*/
|
||||
public boolean isHeads() {
|
||||
return face == CoinFace.Heads;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current face value of this Coin as a String.
|
||||
* @return toString description
|
||||
*/
|
||||
public String toString() {
|
||||
return face.name();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
/** name of item. */
|
||||
private final String name;
|
||||
|
||||
/** price of item. */
|
||||
private final double price;
|
||||
|
||||
/** quanitity of item. */
|
||||
private final int quantity;
|
||||
|
||||
/**
|
||||
* Item constructor.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
* @param quantity of item
|
||||
*/
|
||||
public Item(String name, double price, int quantity) {
|
||||
this.name = name;
|
||||
this.quantity = quantity;
|
||||
DecimalFormat format = new DecimalFormat("#0.00");
|
||||
this.price = Double.parseDouble(format.format(price));
|
||||
}
|
||||
|
||||
/**
|
||||
* Item constructor with default quantity.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
*/
|
||||
public Item(String name, double price) {
|
||||
this.name = name;
|
||||
DecimalFormat format = new DecimalFormat("#0.00");
|
||||
this.price = Double.parseDouble(format.format(price));
|
||||
this.quantity = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* getName of time.
|
||||
* @return name of item
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* getPrice of item.
|
||||
* @return price of item
|
||||
*/
|
||||
public double getPrice() {
|
||||
return this.price;
|
||||
}
|
||||
|
||||
/**
|
||||
* getQuantity of item.
|
||||
* @return quantity of item
|
||||
*/
|
||||
public int getQuantity() {
|
||||
return this.quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* toString gives string representation of item.
|
||||
* @return string of item.
|
||||
*/
|
||||
public String toString() {
|
||||
return this.name + " $" + this.price
|
||||
+ " x" + this.quantity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
public class Item {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
private final String name;
|
||||
|
||||
private
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final double price;
|
||||
|
||||
private final int quantity;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
/** name of item. */
|
||||
private final String name;
|
||||
|
||||
private final double price;
|
||||
|
||||
private final int quantity;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
/** name of item. */
|
||||
private final String name;
|
||||
|
||||
/** price of item. */
|
||||
private final double price;
|
||||
|
||||
/** quanitity of item */
|
||||
private final int quantity;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.NumberFormat;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
/** name of item. */
|
||||
private final String name;
|
||||
|
||||
/** price of item. */
|
||||
private final double price;
|
||||
|
||||
/** quanitity of item. */
|
||||
private final int quantity;
|
||||
|
||||
/**
|
||||
* Item constructor.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
* @param quantity of item
|
||||
*/
|
||||
public Item(String name, double price, int quantity) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.quantity = quantity;
|
||||
|
||||
|
||||
DecimalFormat format = new DecimalFormat("#0.00");
|
||||
return Double.parseDouble(format.format(a / this.students.size()));
|
||||
|
||||
this.price =
|
||||
}
|
||||
|
||||
/**
|
||||
* Item constructor with default quantity.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
*/
|
||||
public Item(String name, double price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.quantity = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* getName of time.
|
||||
* @return name of item
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* getPrice of item.
|
||||
* @return price of item
|
||||
*/
|
||||
public double getPrice() {
|
||||
return this.price;
|
||||
}
|
||||
|
||||
/**
|
||||
* getQuantity of item.
|
||||
* @return quantity of item
|
||||
*/
|
||||
public int getQuantity() {
|
||||
return this.quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* toString gives string representation of item.
|
||||
* @return string of item.
|
||||
*/
|
||||
public String toString() {
|
||||
return this.name + " $" + this.price
|
||||
+ " x" + this.quantity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Lockable does stuff.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public interface Lockable {
|
||||
|
||||
/**
|
||||
* setKey value.
|
||||
* @param key to set
|
||||
*/
|
||||
void setKey(int key);
|
||||
|
||||
/**
|
||||
* lock Object with key.
|
||||
* @param key to lock.
|
||||
* @return success
|
||||
*/
|
||||
boolean lock(int key);
|
||||
|
||||
/**
|
||||
* unlock Object with key.
|
||||
* @param key to unlock.
|
||||
* @return success
|
||||
*/
|
||||
boolean unlock(int key);
|
||||
|
||||
/**
|
||||
* locked Object state.
|
||||
* @return locked
|
||||
*/
|
||||
boolean locked();
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* LockableDriver drives Lockable Coin.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class LockableDriver {
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Coin coin = new Coin();
|
||||
|
||||
System.out.println("Coin is "
|
||||
+ (coin.locked() ? "locked" : "unlocked"));
|
||||
|
||||
System.out.println("Face: " + coin.getFace());
|
||||
System.out.println("Flipping...");
|
||||
coin.flip();
|
||||
System.out.println("Face: " + coin.getFace());
|
||||
|
||||
System.out.println("Locking Coin...");
|
||||
final int key = 984757;
|
||||
coin.setKey(key);
|
||||
coin.lock(key);
|
||||
|
||||
System.out.println("Flipping...");
|
||||
coin.flip();
|
||||
System.out.println("Face: " + coin.getFace());
|
||||
|
||||
System.out.println("No change in face value because "
|
||||
+ "coin is locked and cannot be flipped.");
|
||||
|
||||
System.out.println("Unlocking Coin...");
|
||||
coin.unlock(key);
|
||||
|
||||
System.out.println("Flipping...");
|
||||
coin.flip();
|
||||
System.out.println("Face: " + coin.getFace());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* LockableDriver drives Lockable Coin.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class LockableDriver {
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Coin coin = new Coin();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* LockableImplementation is a demo of
|
||||
* implementing Lockable interface.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class LockableImplementation implements Lockable {
|
||||
|
||||
/** key of class. */
|
||||
private int key;
|
||||
|
||||
/** locked state of class. */
|
||||
private boolean locked;
|
||||
|
||||
/** LockableImplementation empty constructor. */
|
||||
public LockableImplementation() {
|
||||
this.key = 0;
|
||||
this.locked = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* setKey value.
|
||||
* @param key to set
|
||||
*/
|
||||
public void setKey(int key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
/**
|
||||
* lock Object with key.
|
||||
* @param keyIn to lock.
|
||||
* @return success
|
||||
*/
|
||||
public boolean lock(int keyIn) {
|
||||
this.locked = this.key == keyIn ? true : this.locked;
|
||||
return this.key == keyIn;
|
||||
}
|
||||
|
||||
/**
|
||||
* unlock Object with key.
|
||||
* @param keyIn to unlock.
|
||||
* @return success
|
||||
*/
|
||||
public boolean unlock(int keyIn) {
|
||||
this.locked = this.key == keyIn ? false : this.locked;
|
||||
return this.key == keyIn;
|
||||
}
|
||||
|
||||
/**
|
||||
* locked Object state.
|
||||
* @return locked
|
||||
*/
|
||||
public boolean locked() {
|
||||
return this.locked;
|
||||
}
|
||||
|
||||
/**
|
||||
* doSomething as examplet to demo locked.
|
||||
* @param data string
|
||||
* */
|
||||
public void doSomething(String data) {
|
||||
if (this.locked) {
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Doing something with " + data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
/**
|
||||
* readValidInt reads and validates an integer.
|
||||
* @param msg to print;
|
||||
* @param scan to read input
|
||||
* @return validated int
|
||||
*/
|
||||
private static int readValidInt(String msg, Scanner scan) {
|
||||
int number = 0;
|
||||
boolean valid = false;
|
||||
|
||||
do {
|
||||
System.out.print(msg);
|
||||
|
||||
if (!scan.hasNextInt()) {
|
||||
System.out.println(scan.next() + " is not a valid number.\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
number = scan.nextInt();
|
||||
valid = true;
|
||||
|
||||
} while (!valid);
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
/**
|
||||
* reverseIntArray reverses an array of ints.
|
||||
* @param arr of ints.
|
||||
* @return reversed array of ints.
|
||||
*/
|
||||
private static int[] reverseIntArray(int[] arr) {
|
||||
int n;
|
||||
for (int i = 0; i < arr.length / 2; i++) {
|
||||
n = arr[arr.length - 1 - i];
|
||||
arr[arr.length - 1 - i] = arr[i];
|
||||
arr[i] = n;
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
int length = readValidInt(
|
||||
"Please enter the length for the array: ",
|
||||
scan
|
||||
);
|
||||
|
||||
int[] arr = new int[length];
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int num = readValidInt(
|
||||
"Please enter a number: ",
|
||||
scan
|
||||
);
|
||||
arr[i] = num;
|
||||
}
|
||||
|
||||
System.out.println(Arrays.toString(arr));
|
||||
|
||||
arr = reverseIntArray(arr);
|
||||
|
||||
System.out.println(Arrays.toString(arr));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
/**
|
||||
* readValidInt reads and validates an integer.
|
||||
* @param msg to print;
|
||||
* @param scan to read input
|
||||
* @return validated int
|
||||
*/
|
||||
private static int readValidInt(String msg, Scanner scan) {
|
||||
int number = 0;
|
||||
boolean valid = false;
|
||||
|
||||
do {
|
||||
System.out.print(msg);
|
||||
|
||||
if (!scan.hasNextInt()) {
|
||||
System.out.println(scan.next() + " is not a valid number.\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
number = scan.nextInt();
|
||||
valid = true;
|
||||
|
||||
} while (!valid);
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
int length = readValidInt(
|
||||
"Please enter the length for the array: ",
|
||||
scan
|
||||
);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int num = readValidInt(
|
||||
"Please enter a number: ",
|
||||
scan
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
/**
|
||||
* readValidInt reads and validates an integer.
|
||||
* @param msg to print;
|
||||
* @param scan to read input
|
||||
* @return validated int
|
||||
*/
|
||||
private static int readValidInt(String msg, Scanner scan) {
|
||||
int number = 0;
|
||||
boolean valid = false;
|
||||
|
||||
do {
|
||||
System.out.print(msg);
|
||||
|
||||
if (!scan.hasNextInt()) {
|
||||
System.out.println(scan.next() + " is not a valid number.\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
number = scan.nextInt();
|
||||
valid = true;
|
||||
|
||||
} while (!valid);
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
/**
|
||||
* reverseIntArray reverses an array of ints.
|
||||
* @param arr of ints.
|
||||
* @return reversed array of ints.
|
||||
*/
|
||||
private static int[] reverseIntArray(int[] arr) {
|
||||
int[] reversed = new int[arr.length];
|
||||
|
||||
int n;
|
||||
for (int i = 0; i < arr.length / 2; i++) {
|
||||
n = arr[arr.length - i];
|
||||
arr[arr.length - i] = arr[i];
|
||||
arr[i] = n;
|
||||
}
|
||||
|
||||
return reversed;
|
||||
}
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
int length = readValidInt(
|
||||
"Please enter the length for the array: ",
|
||||
scan
|
||||
);
|
||||
|
||||
int[] arr = new int[length];
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int num = readValidInt(
|
||||
"Please enter a number: ",
|
||||
scan
|
||||
);
|
||||
arr[i] = num;
|
||||
}
|
||||
|
||||
System.out.println(Arrays.toString(arr));
|
||||
|
||||
arr = reverseIntArray(arr);
|
||||
|
||||
System.out.println(Arrays.toString(arr));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Shopping cart driver.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Shopping {
|
||||
|
||||
/**
|
||||
* readValidInt reads and validates an integer.
|
||||
* @param msg to print;
|
||||
* @param scan to read input
|
||||
* @return validated int
|
||||
*/
|
||||
private static int readValidInt(String msg, Scanner scan) {
|
||||
int number = 0;
|
||||
boolean valid = false;
|
||||
|
||||
do {
|
||||
System.out.print(msg);
|
||||
|
||||
if (!scan.hasNextInt()) {
|
||||
System.out.println(scan.next() + " is not a valid number.\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
number = scan.nextInt();
|
||||
valid = true;
|
||||
|
||||
} while (!valid);
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
|
||||
final double burgerPrice = 8.16;
|
||||
final double friesPrice = 3.25;
|
||||
final double cokePrice = 2.75;
|
||||
|
||||
int burgers = 0;
|
||||
int fries = 0;
|
||||
int cokes = 0;
|
||||
|
||||
final int finish = 4;
|
||||
int input = 0;
|
||||
|
||||
final String msg = "Select an item to add to cart:\n"
|
||||
+ "(1). Burger $8.16\n"
|
||||
+ "(2). Fries $3.25\n"
|
||||
+ "(3). Coke $2.75\n"
|
||||
+ "(4). Proceed to Checkout\n"
|
||||
+ "> ";
|
||||
|
||||
final int startSize = 3;
|
||||
Transaction cart = new Transaction(startSize);
|
||||
|
||||
final int t = 3;
|
||||
final int f = 4;
|
||||
|
||||
do {
|
||||
input = readValidInt(msg, scan);
|
||||
|
||||
switch (input) {
|
||||
case 1:
|
||||
cart.addToCart("Burger", burgerPrice, 1);
|
||||
break;
|
||||
case 2:
|
||||
cart.addToCart("fries", fries, 1);
|
||||
break;
|
||||
case t:
|
||||
cart.addToCart(new Item("Coke", cokePrice, 1));
|
||||
break;
|
||||
case f:
|
||||
System.out.println("Checking Out...");
|
||||
System.out.println(cart.getCount() + " items comes to "
|
||||
+ cart.getTotalPrice());
|
||||
break;
|
||||
default:
|
||||
System.out.println(input + " is not a valid input.");
|
||||
break;
|
||||
}
|
||||
} while (input != finish);
|
||||
|
||||
scan.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
public class Shopping {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Shopping cart driver.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Shopping {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Shopping cart driver.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Shopping {
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
final int burgerPrice = 3.16;
|
||||
Item burger = new Item("Burger", 8.16, 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Transaction of items.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Transaction {
|
||||
|
||||
/** items in transaction. */
|
||||
private Item[] items;
|
||||
|
||||
/** totalPrice of transaction. */
|
||||
private double totalPrice;
|
||||
|
||||
/** itemCount of transaction. */
|
||||
private int itemCount;
|
||||
|
||||
/**
|
||||
* Tramsaction constructor.
|
||||
* @param size of items;
|
||||
*/
|
||||
public Transaction(int size) {
|
||||
this.items = new Item[size];
|
||||
this.totalPrice = 0;
|
||||
this.itemCount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart creates an item to add to cart.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
* @param quantity of item
|
||||
*/
|
||||
public void addToCart(String name, double price, int quantity) {
|
||||
Item item = new Item(name, price, quantity);
|
||||
|
||||
if (this.itemCount == this.items.length) {
|
||||
increaseSize();
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += price * quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart adds existing item to cart.
|
||||
* @param item to add
|
||||
*/
|
||||
public void addToCart(Item item) {
|
||||
if (this.itemCount == this.items.length) {
|
||||
increaseSize();
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += item.getPrice() * item.getQuantity();
|
||||
}
|
||||
|
||||
/** increaseSize of cart. */
|
||||
public void increaseSize() {
|
||||
final int up = 3;
|
||||
Item[] updatedItems = new Item[this.items.length + up];
|
||||
|
||||
for (int i = 0; i < this.itemCount; i++) {
|
||||
updatedItems[i] = this.items[i];
|
||||
}
|
||||
|
||||
this.items = updatedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* getTotalPrice gets total price.
|
||||
* @return total price
|
||||
*/
|
||||
public double getTotalPrice() {
|
||||
return this.totalPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* getCount of items.
|
||||
* @return number of items.
|
||||
*/
|
||||
public int getCount() {
|
||||
return this.itemCount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Transaction of items.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Transaction {
|
||||
|
||||
/** items in transaction. */
|
||||
private Item[] items;
|
||||
|
||||
/** totalPrice of transaction. */
|
||||
private double totalPrice;
|
||||
|
||||
/** itemCount of transaction. */
|
||||
private int itemCount;
|
||||
|
||||
/**
|
||||
* Tramsaction constructor.
|
||||
* @param size of items;
|
||||
*/
|
||||
public Transaction(int size) {
|
||||
this.items = new Item[size];
|
||||
this.totalPrice = 0;
|
||||
this.itemCount = 0;
|
||||
}
|
||||
|
||||
public void addToCart(String name, double price, int quantity) {
|
||||
Item item = new Item(name, price, quantity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Transaction of items.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Transaction {
|
||||
|
||||
/** items in transaction. */
|
||||
private Item[] items;
|
||||
|
||||
/** totalPrice of transaction. */
|
||||
private double totalPrice;
|
||||
|
||||
/** itemCount of transaction. */
|
||||
private int itemCount;
|
||||
|
||||
/**
|
||||
* Tramsaction constructor.
|
||||
* @param size of items;
|
||||
*/
|
||||
public Transaction(int size) {
|
||||
this.items = new Item[size];
|
||||
this.totalPrice = 0;
|
||||
this.itemCount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart creates an item to add to cart.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
* @param quantity of item
|
||||
*/
|
||||
public void addToCart(String name, double price, int quantity) {
|
||||
Item item = new Item(name, price, quantity);
|
||||
|
||||
if (this.itemCount == this.items.length) {
|
||||
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += price * quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart adds existing item to cart.
|
||||
* @param item to add
|
||||
*/
|
||||
public void addToCart(Item item) {
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += item.getPrice() * item.getQuantity();
|
||||
}
|
||||
|
||||
/** increaseSize of cart. */
|
||||
public void increaseSize() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Transaction of items.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Transaction {
|
||||
|
||||
/** items in transaction. */
|
||||
private Item[] items;
|
||||
|
||||
/** totalPrice of transaction. */
|
||||
private double totalPrice;
|
||||
|
||||
/** itemCount of transaction. */
|
||||
private int itemCount;
|
||||
|
||||
/**
|
||||
* Tramsaction constructor.
|
||||
* @param size of items;
|
||||
*/
|
||||
public Transaction(int size) {
|
||||
this.items = new Item[size];
|
||||
this.totalPrice = 0;
|
||||
this.itemCount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart creates an item to add to cart.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
* @param quantity of item
|
||||
*/
|
||||
public void addToCart(String name, double price, int quantity) {
|
||||
Item item = new Item(name, price, quantity);
|
||||
|
||||
if (this.itemCount == this.items.length) {
|
||||
increaseSize();
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += price * quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart adds existing item to cart.
|
||||
* @param item to add
|
||||
*/
|
||||
public void addToCart(Item item) {
|
||||
if (this.itemCount == this.items.length) {
|
||||
increaseSize();
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += item.getPrice() * item.getQuantity();
|
||||
}
|
||||
|
||||
/** increaseSize of cart. */
|
||||
public void increaseSize() {
|
||||
final int up = 3;
|
||||
Item[] updatedItems = new Item[this.items.length + up];
|
||||
|
||||
for (int i = 0; i < this.itemCount; i++) {
|
||||
updatedItems[i] = this.items[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Transaction of items.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Transaction {
|
||||
|
||||
/** items in transaction. */
|
||||
private Item[] items;
|
||||
|
||||
/** totalPrice of transaction. */
|
||||
private double totalPrice;
|
||||
|
||||
/** itemCount of transaction. */
|
||||
private int itemCount;
|
||||
|
||||
/**
|
||||
* Tramsaction constructor.
|
||||
* @param size of items;
|
||||
*/
|
||||
public Transaction(int size) {
|
||||
this.items = new Item[size];
|
||||
this.totalPrice = 0;
|
||||
this.itemCount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart creates an item to add to cart.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
* @param quantity of item
|
||||
*/
|
||||
public void addToCart(String name, double price, int quantity) {
|
||||
Item item = new Item(name, price, quantity);
|
||||
|
||||
if (this.itemCount == this.items.length) {
|
||||
increaseSize();
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += price * quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart adds existing item to cart.
|
||||
* @param item to add
|
||||
*/
|
||||
public void addToCart(Item item) {
|
||||
if (this.itemCount == this.items.length) {
|
||||
increaseSize();
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += item.getPrice() * item.getQuantity();
|
||||
}
|
||||
|
||||
/** increaseSize of cart. */
|
||||
public void increaseSize() {
|
||||
final int up = 3;
|
||||
Item[] updatedItems = new Item[this.items.length + up];
|
||||
|
||||
for (int i = 0; i < this.itemCount; i++) {
|
||||
updatedItems[i] = this.items[i];
|
||||
}
|
||||
|
||||
this.items = updatedItems;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
/** name of item. */
|
||||
private final String name;
|
||||
|
||||
/** price of item. */
|
||||
private final double price;
|
||||
|
||||
/** quanitity of item. */
|
||||
private final int quantity;
|
||||
|
||||
/**
|
||||
* Item constructor.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
* @param quantity of item
|
||||
*/
|
||||
public Item(String name, double price, int quantity) {
|
||||
this.name = name;
|
||||
this.quantity = quantity;
|
||||
DecimalFormat format = new DecimalFormat("#0.00");
|
||||
this.price = Double.parseDouble(format.format(price));
|
||||
}
|
||||
|
||||
/**
|
||||
* Item constructor with default quantity.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
*/
|
||||
public Item(String name, double price) {
|
||||
this.name = name;
|
||||
DecimalFormat format = new DecimalFormat("#0.00");
|
||||
this.price = Double.parseDouble(format.format(price));
|
||||
this.quantity = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* getName of time.
|
||||
* @return name of item
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* getPrice of item.
|
||||
* @return price of item
|
||||
*/
|
||||
public double getPrice() {
|
||||
return this.price;
|
||||
}
|
||||
|
||||
/**
|
||||
* getQuantity of item.
|
||||
* @return quantity of item
|
||||
*/
|
||||
public int getQuantity() {
|
||||
return this.quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* toString gives string representation of item.
|
||||
* @return string of item.
|
||||
*/
|
||||
public String toString() {
|
||||
return this.name + " $" + this.price
|
||||
+ " x" + this.quantity;
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
public class Item {
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
private final String name;
|
||||
|
||||
private
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final double price;
|
||||
|
||||
private final int quantity;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
/** name of item. */
|
||||
private final String name;
|
||||
|
||||
private final double price;
|
||||
|
||||
private final int quantity;
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
/** name of item. */
|
||||
private final String name;
|
||||
|
||||
/** price of item. */
|
||||
private final double price;
|
||||
|
||||
/** quanitity of item */
|
||||
private final int quantity;
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.NumberFormat;
|
||||
|
||||
/**
|
||||
* Item class for POS System.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
/** name of item. */
|
||||
private final String name;
|
||||
|
||||
/** price of item. */
|
||||
private final double price;
|
||||
|
||||
/** quanitity of item. */
|
||||
private final int quantity;
|
||||
|
||||
/**
|
||||
* Item constructor.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
* @param quantity of item
|
||||
*/
|
||||
public Item(String name, double price, int quantity) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.quantity = quantity;
|
||||
|
||||
|
||||
DecimalFormat format = new DecimalFormat("#0.00");
|
||||
return Double.parseDouble(format.format(a / this.students.size()));
|
||||
|
||||
this.price =
|
||||
}
|
||||
|
||||
/**
|
||||
* Item constructor with default quantity.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
*/
|
||||
public Item(String name, double price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.quantity = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* getName of time.
|
||||
* @return name of item
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* getPrice of item.
|
||||
* @return price of item
|
||||
*/
|
||||
public double getPrice() {
|
||||
return this.price;
|
||||
}
|
||||
|
||||
/**
|
||||
* getQuantity of item.
|
||||
* @return quantity of item
|
||||
*/
|
||||
public int getQuantity() {
|
||||
return this.quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* toString gives string representation of item.
|
||||
* @return string of item.
|
||||
*/
|
||||
public String toString() {
|
||||
return this.name + " $" + this.price
|
||||
+ " x" + this.quantity;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* LockableDriver drives Lockable Coin.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class LockableDriver {
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Coin coin = new Coin();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
/**
|
||||
* readValidInt reads and validates an integer.
|
||||
* @param msg to print;
|
||||
* @param scan to read input
|
||||
* @return validated int
|
||||
*/
|
||||
private static int readValidInt(String msg, Scanner scan) {
|
||||
int number = 0;
|
||||
boolean valid = false;
|
||||
|
||||
do {
|
||||
System.out.print(msg);
|
||||
|
||||
if (!scan.hasNextInt()) {
|
||||
System.out.println(scan.next() + " is not a valid number.\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
number = scan.nextInt();
|
||||
valid = true;
|
||||
|
||||
} while (!valid);
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
/**
|
||||
* reverseIntArray reverses an array of ints.
|
||||
* @param arr of ints.
|
||||
* @return reversed array of ints.
|
||||
*/
|
||||
private static int[] reverseIntArray(int[] arr) {
|
||||
int[] reversed = new int[arr.length];
|
||||
|
||||
int n;
|
||||
for (int i = 0; i < arr.length / 2; i++) {
|
||||
n = arr[arr.length - 1 - i];
|
||||
arr[arr.length - 1 - i] = arr[i];
|
||||
arr[i] = n;
|
||||
}
|
||||
|
||||
return reversed;
|
||||
}
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
int length = readValidInt(
|
||||
"Please enter the length for the array: ",
|
||||
scan
|
||||
);
|
||||
|
||||
int[] arr = new int[length];
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int num = readValidInt(
|
||||
"Please enter a number: ",
|
||||
scan
|
||||
);
|
||||
arr[i] = num;
|
||||
}
|
||||
|
||||
System.out.println(Arrays.toString(arr));
|
||||
|
||||
arr = reverseIntArray(arr);
|
||||
|
||||
System.out.println(Arrays.toString(arr));
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
/**
|
||||
* readValidInt reads and validates an integer.
|
||||
* @param msg to print;
|
||||
* @param scan to read input
|
||||
* @return validated int
|
||||
*/
|
||||
private static int readValidInt(String msg, Scanner scan) {
|
||||
int number = 0;
|
||||
boolean valid = false;
|
||||
|
||||
do {
|
||||
System.out.print(msg);
|
||||
|
||||
if (!scan.hasNextInt()) {
|
||||
System.out.println(scan.next() + " is not a valid number.\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
number = scan.nextInt();
|
||||
valid = true;
|
||||
|
||||
} while (!valid);
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
int length = readValidInt(
|
||||
"Please enter the length for the array: ",
|
||||
scan
|
||||
);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int num = readValidInt(
|
||||
"Please enter a number: ",
|
||||
scan
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* ReverseArray is self explanitory.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class ReverseArray {
|
||||
|
||||
/**
|
||||
* readValidInt reads and validates an integer.
|
||||
* @param msg to print;
|
||||
* @param scan to read input
|
||||
* @return validated int
|
||||
*/
|
||||
private static int readValidInt(String msg, Scanner scan) {
|
||||
int number = 0;
|
||||
boolean valid = false;
|
||||
|
||||
do {
|
||||
System.out.print(msg);
|
||||
|
||||
if (!scan.hasNextInt()) {
|
||||
System.out.println(scan.next() + " is not a valid number.\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
number = scan.nextInt();
|
||||
valid = true;
|
||||
|
||||
} while (!valid);
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
/**
|
||||
* reverseIntArray reverses an array of ints.
|
||||
* @param arr of ints.
|
||||
* @return reversed array of ints.
|
||||
*/
|
||||
private static int[] reverseIntArray(int[] arr) {
|
||||
int[] reversed = new int[arr.length];
|
||||
|
||||
int n;
|
||||
for (int i = 0; i < arr.length / 2; i++) {
|
||||
n = arr[arr.length - i];
|
||||
arr[arr.length - i] = arr[i];
|
||||
arr[i] = n;
|
||||
}
|
||||
|
||||
return reversed;
|
||||
}
|
||||
|
||||
/**
|
||||
* main program entry.
|
||||
* @param args unused
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
int length = readValidInt(
|
||||
"Please enter the length for the array: ",
|
||||
scan
|
||||
);
|
||||
|
||||
int[] arr = new int[length];
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int num = readValidInt(
|
||||
"Please enter a number: ",
|
||||
scan
|
||||
);
|
||||
arr[i] = num;
|
||||
}
|
||||
|
||||
System.out.println(Arrays.toString(arr));
|
||||
|
||||
arr = reverseIntArray(arr);
|
||||
|
||||
System.out.println(Arrays.toString(arr));
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Shopping cart driver.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Shopping {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Transaction of items.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Transaction {
|
||||
|
||||
/** items in transaction. */
|
||||
private Item[] items;
|
||||
|
||||
/** totalPrice of transaction. */
|
||||
private double totalPrice;
|
||||
|
||||
/** itemCount of transaction. */
|
||||
private int itemCount;
|
||||
|
||||
/**
|
||||
* Tramsaction constructor.
|
||||
* @param size of items;
|
||||
*/
|
||||
public Transaction(int size) {
|
||||
this.items = new Item[size];
|
||||
this.totalPrice = 0;
|
||||
this.itemCount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart creates an item to add to cart.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
* @param quantity of item
|
||||
*/
|
||||
public void addToCart(String name, double price, int quantity) {
|
||||
Item item = new Item(name, price, quantity);
|
||||
|
||||
if (this.itemCount == this.items.length) {
|
||||
increaseSize();
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += price * quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart adds existing item to cart.
|
||||
* @param item to add
|
||||
*/
|
||||
public void addToCart(Item item) {
|
||||
if (this.itemCount == this.items.length) {
|
||||
increaseSize();
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += item.getPrice() * item.getQuantity();
|
||||
}
|
||||
|
||||
/** increaseSize of cart. */
|
||||
public void increaseSize() {
|
||||
final int up = 3;
|
||||
Item[] updatedItems = new Item[this.items.length + up];
|
||||
|
||||
for (int i = 0; i < this.itemCount; i++) {
|
||||
updatedItems[i] = this.items[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Transaction of items.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Transaction {
|
||||
|
||||
/** items in transaction. */
|
||||
private Item[] items;
|
||||
|
||||
/** totalPrice of transaction. */
|
||||
private double totalPrice;
|
||||
|
||||
/** itemCount of transaction. */
|
||||
private int itemCount;
|
||||
|
||||
/**
|
||||
* Tramsaction constructor.
|
||||
* @param size of items;
|
||||
*/
|
||||
public Transaction(int size) {
|
||||
this.items = new Item[size];
|
||||
this.totalPrice = 0;
|
||||
this.itemCount = 0;
|
||||
}
|
||||
|
||||
public void addToCart(String name, double price, int quantity) {
|
||||
Item item = new Item(name, price, quantity);
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package ca.bcit.comp1510.lab11;
|
||||
|
||||
/**
|
||||
* Transaction of items.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Transaction {
|
||||
|
||||
/** items in transaction. */
|
||||
private Item[] items;
|
||||
|
||||
/** totalPrice of transaction. */
|
||||
private double totalPrice;
|
||||
|
||||
/** itemCount of transaction. */
|
||||
private int itemCount;
|
||||
|
||||
/**
|
||||
* Tramsaction constructor.
|
||||
* @param size of items;
|
||||
*/
|
||||
public Transaction(int size) {
|
||||
this.items = new Item[size];
|
||||
this.totalPrice = 0;
|
||||
this.itemCount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart creates an item to add to cart.
|
||||
* @param name of item
|
||||
* @param price of item
|
||||
* @param quantity of item
|
||||
*/
|
||||
public void addToCart(String name, double price, int quantity) {
|
||||
Item item = new Item(name, price, quantity);
|
||||
|
||||
if (this.itemCount == this.items.length) {
|
||||
increaseSize();
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += price * quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* addToCart adds existing item to cart.
|
||||
* @param item to add
|
||||
*/
|
||||
public void addToCart(Item item) {
|
||||
if (this.itemCount == this.items.length) {
|
||||
increaseSize();
|
||||
}
|
||||
|
||||
this.items[itemCount] = item;
|
||||
this.itemCount++;
|
||||
|
||||
this.totalPrice += item.getPrice() * item.getQuantity();
|
||||
}
|
||||
|
||||
/** increaseSize of cart. */
|
||||
public void increaseSize() {
|
||||
final int up = 3;
|
||||
Item[] updatedItems = new Item[this.items.length + up];
|
||||
|
||||
for (int i = 0; i < this.itemCount; i++) {
|
||||
updatedItems[i] = this.items[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user