Commit c33e2796 authored by Wichit Sombat's avatar Wichit Sombat

add code

parent c5daa623
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="bin/main" path="src/main/java">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/java">
<attributes>
<attribute name="gradle_scope" value="test"/>
<attribute name="gradle_used_by_scope" value="test"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
...@@ -3,3 +3,5 @@ ...@@ -3,3 +3,5 @@
# Ignore Gradle build output directory # Ignore Gradle build output directory
build/ build/
*.db
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>csubuapp</name>
<comment>Project gradle-project created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
connection.project.dir=
eclipse.preferences.version=1
{
"java.configuration.updateBuildConfiguration": "disabled"
}
\ No newline at end of file
...@@ -34,4 +34,6 @@ dependencies { ...@@ -34,4 +34,6 @@ dependencies {
} }
// Define the main class for the application // Define the main class for the application
mainClassName = 'edu.ubu.csapp.App' //mainClassName = 'edu.ubu.csapp.App'
// mainClassName = 'edu.ubu.csapp.DataStreamApp'
mainClassName = 'edu.ubu.csapp.PointOfSaleApp'
package edu.ubu.csapp;
import java.io.*;
import java.util.*;
public class DataStreamApp {
static Product[] products = {
new Product("Shirt", 250.00, 10),
new Product("Jeans", 1550.00, 15),
new Product("Mug", 150.00, 20),
new Product("Jacket", 5499.00, 5),
new Product("Computer", 26490.00, 8)
};
static void save(String fileName) {
try (DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(fileName)
)
)
) {
for (Product p : products) {
out.writeUTF(p.getDescription());
out.writeDouble(p.getPrice());
out.writeInt(p.getUnit());
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
static ArrayList<Product> load(String fileName) {
ArrayList<Product> productList = new ArrayList<Product>();
try (DataInputStream input = new DataInputStream(
new BufferedInputStream(
new FileInputStream(fileName)
)
)
) {
while (true) {
Product p = new Product();
p.setDescription(input.readUTF());
p.setPrice(input.readDouble());
p.setUnit(input.readInt());
System.out.println(p);
productList.add(p);
}
} catch (EOFException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return productList;
}
public static void main(String[] args) throws IOException {
save(args[1]);
ArrayList<Product> productList = load(args[1]);
}
}
\ No newline at end of file
package edu.ubu.csapp;
import java.io.*;
public class ObjectStreamApp {
static Product[] products = {
new Product("Shirt", 250.00, 10),
new Product("Jeans", 1550.00, 15),
new Product("Mug", 150.00, 20),
new Product("Jacket", 5499.00, 5),
new Product("Computer", 26490.00, 8)
};
static void saveObject(String fileName) {
try (ObjectOutputStream out = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(fileName)
)
)
) {
out.writeObject(products);
} catch (IOException ex) {
ex.printStackTrace();
}
}
static Product[] loadObject(String fileName) {
Product[] products = null;
try (ObjectInputStream input = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(fileName)
)
)
) {
products = (Product[])(input.readObject());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (EOFException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return products;
}
public static void main(String[] args) throws IOException {
saveObject(args[1]);
Product[] products = loadObject(args[1]);
}
}
\ No newline at end of file
package edu.ubu.csapp;
import java.util.Scanner;
public class PointOfSaleApp {
// java -cp build/libs/csubuapp.jar edu.ubu.csapp.PointOfSaleApp
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("ระบุจำนวนสินค้า: ");
int n = s.nextInt();
s.nextLine();
for (int i=0; i<n; i++) {
System.out.printf("ชื่อสินค้า #%d: ", i+1);
String desc = s.nextLine();
System.out.printf("ราคาสินค้า #%d: ", i+1);
double price = s.nextDouble();
System.out.printf("จำนวนชิ้นของสินค้า #%d: ", i+1);
int unit = s.nextInt();
s.nextLine();
}
}
}
\ No newline at end of file
package edu.ubu.csapp;
import java.io.Serializable;
public class Product implements Serializable {
private static final long serialVersionUID = 1234567L;
private String description;
private double price;
private int unit;
public Product() {
}
public Product(String description, double price, int unit) {
this.description = description;
this.price = price;
this.unit = unit;
}
public String getDescription() { return description; }
public double getPrice() { return price; }
public int getUnit() { return unit; }
public void setDescription(String description) { this.description = description; }
public void setPrice(double price) { this.price = price; }
public void setUnit(int unit) { this.unit = unit; }
@Override
public String toString() {
return String.format("สั่งสินค้า '%s' จำนวน %d ชิ้น ราคาชิ้นละ %.2f บาท", description, unit, price);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment